@communecter/cocolight-api-client 1.0.19 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cocolight-api-client.browser.js +2 -2
- package/dist/cocolight-api-client.cjs +1 -1
- package/dist/cocolight-api-client.mjs.js +1 -1
- package/package.json +2 -1
- package/src/api/BaseEntity.js +1420 -34
- package/src/api/EndpointApi.js +443 -924
- package/src/api/EndpointApi.types.d.ts +3463 -0
- package/src/api/Organization.js +77 -0
- package/src/api/User.js +91 -327
- package/src/mixin/DraftStateMixin.js +0 -176
- package/src/mixin/EntityMixin.js +0 -428
- package/src/mixin/MutualEntityMixin.js +0 -269
- package/src/mixin/UtilMixin.js +0 -300
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
// DraftStateMixin.js
|
|
2
|
-
import { ApiError, ApiValidationError } from "../error.js";
|
|
3
|
-
|
|
4
|
-
export const DraftStateMixin = {
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Crée un proxy combinant draft + serveur, avec transformations facultatives.
|
|
8
|
-
* @param {Object} server
|
|
9
|
-
* @param {Object} draft
|
|
10
|
-
* @param {Array} allowedFields - champs autorisés dans le draft
|
|
11
|
-
* @param {Object} [transforms={}] - transformateurs de lecture
|
|
12
|
-
* @param {Object} [options={}] - options
|
|
13
|
-
* @returns {Proxy}
|
|
14
|
-
*/
|
|
15
|
-
_createDraftProxy(apiClient, server = {}, draft = {}, allowedFields = [], transforms = {}, options = {}) {
|
|
16
|
-
return new Proxy({}, {
|
|
17
|
-
get: (_, prop) => {
|
|
18
|
-
const val = prop in draft ? draft[prop] : server[prop];
|
|
19
|
-
const transformer = transforms[prop];
|
|
20
|
-
return typeof transformer === "function" ? transformer(val) : val;
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
set: (_, prop, value) => {
|
|
24
|
-
if (!allowedFields.includes(prop)) {
|
|
25
|
-
const message = `[DraftProxy] Le champ "${prop}" n'est pas autorisé.`;
|
|
26
|
-
if (options.throwOnError) {
|
|
27
|
-
throw new ApiValidationError(message, 400, null, {
|
|
28
|
-
field: prop,
|
|
29
|
-
value,
|
|
30
|
-
allowedFields
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
apiClient._logger.warn(message);
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
draft[prop] = value;
|
|
37
|
-
return true;
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
deleteProperty: (_, prop) => {
|
|
41
|
-
if (!allowedFields.includes(prop)) return false;
|
|
42
|
-
delete draft[prop];
|
|
43
|
-
return true;
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
has: (_, prop) => prop in draft || prop in server,
|
|
47
|
-
ownKeys: () => [...new Set([...Object.keys(server), ...Object.keys(draft)])],
|
|
48
|
-
getOwnPropertyDescriptor: () => ({ enumerable: true, configurable: true })
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
_extractWritableFields(schema = {}, data = {}, ctx = { defs: {}, visited: new Set() }) {
|
|
53
|
-
if (!schema || typeof schema !== "object") return [];
|
|
54
|
-
if (schema.$id && ctx.visited.has(schema.$id)) return [];
|
|
55
|
-
if (schema.$id) ctx.visited.add(schema.$id);
|
|
56
|
-
ctx.defs = schema.$defs || schema.definitions || ctx.defs;
|
|
57
|
-
|
|
58
|
-
const fields = [];
|
|
59
|
-
|
|
60
|
-
if (schema.$ref) {
|
|
61
|
-
const refKey = schema.$ref.replace(/^#\/?(\$defs|definitions)\//, "");
|
|
62
|
-
const resolved = ctx.defs?.[refKey];
|
|
63
|
-
if (resolved) fields.push(...this._extractWritableFields(resolved, data, ctx));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (schema.allOf) {
|
|
67
|
-
schema.allOf.forEach(s => fields.push(...this._extractWritableFields(s, data, ctx)));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (schema.if && schema.then) {
|
|
71
|
-
const condition = schema.if?.properties;
|
|
72
|
-
let matches = true;
|
|
73
|
-
if (condition) {
|
|
74
|
-
for (const key in condition) {
|
|
75
|
-
const expected = condition[key]?.const;
|
|
76
|
-
if (data[key] !== expected) {
|
|
77
|
-
matches = false;
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (matches && schema.then) {
|
|
83
|
-
fields.push(...this._extractWritableFields(schema.then, data, ctx));
|
|
84
|
-
} else if (!matches && schema.else) {
|
|
85
|
-
fields.push(...this._extractWritableFields(schema.else, data, ctx));
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (schema.properties) {
|
|
90
|
-
fields.push(...Object.entries(schema.properties)
|
|
91
|
-
// eslint-disable-next-line no-unused-vars
|
|
92
|
-
.filter(([_, def]) => def.readOnly !== true && def.const === undefined)
|
|
93
|
-
.map(([key]) => key));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return [...new Set(fields)];
|
|
97
|
-
},
|
|
98
|
-
|
|
99
|
-
_buildDraftAndProxy({ data = {}, serverData = null, constant, apiClient, transforms = {}, throwOnError = true, removeFields = [] }) {
|
|
100
|
-
const constants = Array.isArray(constant) ? constant : [constant];
|
|
101
|
-
const combinedSchema = {
|
|
102
|
-
allOf: [],
|
|
103
|
-
$defs: {}
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
for (const key of constants) {
|
|
107
|
-
const sch = apiClient.getRequestSchema(key);
|
|
108
|
-
if (!sch) throw new ApiError(`Unable to find schema for ${key}.`);
|
|
109
|
-
|
|
110
|
-
// Extraire et fusionner les $defs
|
|
111
|
-
if (sch.$defs) {
|
|
112
|
-
for (const [defKey, defVal] of Object.entries(sch.$defs)) {
|
|
113
|
-
if (combinedSchema.$defs[defKey]) {
|
|
114
|
-
apiClient._logger.warn(`Duplicate $defs key '${defKey}' from schema '${key}'`);
|
|
115
|
-
} else {
|
|
116
|
-
combinedSchema.$defs[defKey] = defVal;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
combinedSchema.allOf.push(sch);
|
|
122
|
-
}
|
|
123
|
-
const draft = {};
|
|
124
|
-
let allowed = this._extractWritableFields(combinedSchema, data);
|
|
125
|
-
|
|
126
|
-
if (data.id && allowed.indexOf("id") === -1) {
|
|
127
|
-
allowed.push("id");
|
|
128
|
-
}
|
|
129
|
-
if (data.slug && allowed.indexOf("slug") === -1) {
|
|
130
|
-
allowed.push("slug");
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
allowed = allowed.filter(k => !removeFields.includes(k));
|
|
134
|
-
|
|
135
|
-
for (const key of allowed) {
|
|
136
|
-
const raw = data[key];
|
|
137
|
-
const transformed = typeof transforms[key] === "function" ? transforms[key](raw, data) : raw;
|
|
138
|
-
if (transformed !== undefined) draft[key] = transformed;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const proxy = this._createDraftProxy(apiClient, serverData, draft, allowed, transforms, { throwOnError });
|
|
142
|
-
|
|
143
|
-
return { draft, proxy };
|
|
144
|
-
},
|
|
145
|
-
|
|
146
|
-
_extractChangedFieldsFromSchema(apiClient, constant, data = {}, getInitialDraft, removeFields = []) {
|
|
147
|
-
const schema = apiClient.getRequestSchema(constant);
|
|
148
|
-
let allowed = this._extractWritableFields(schema, data);
|
|
149
|
-
const changed = {};
|
|
150
|
-
const initialDraft = getInitialDraft?.() || {};
|
|
151
|
-
|
|
152
|
-
// on enlève les champs qui ne sont pas dans le draft
|
|
153
|
-
// ou qui sont dans removeFields
|
|
154
|
-
allowed = allowed.filter(k => !removeFields.includes(k));
|
|
155
|
-
|
|
156
|
-
for (const key of allowed) {
|
|
157
|
-
// on verifie que le champ existe dans le draft
|
|
158
|
-
// sinon on ne le prend pas en compte
|
|
159
|
-
|
|
160
|
-
if (data[key] === undefined) continue;
|
|
161
|
-
|
|
162
|
-
const current = data[key];
|
|
163
|
-
const initial = initialDraft[key];
|
|
164
|
-
|
|
165
|
-
const changedValue =
|
|
166
|
-
JSON.stringify(current) !== JSON.stringify(initial);
|
|
167
|
-
|
|
168
|
-
if (changedValue) {
|
|
169
|
-
changed[key] = current;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return Object.keys(changed).length > 0 ? changed : null;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
};
|
package/src/mixin/EntityMixin.js
DELETED
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
import { ApiError, ApiResponseError } from "../error.js";
|
|
2
|
-
|
|
3
|
-
// EntityMixin.js
|
|
4
|
-
export const EntityMixin = {
|
|
5
|
-
/**
|
|
6
|
-
* Récupère le profil complet de l'organisation.
|
|
7
|
-
*
|
|
8
|
-
* @returns {Promise<Object>} Le profil complet.
|
|
9
|
-
*/
|
|
10
|
-
async get() {
|
|
11
|
-
return this.apiClient.safeCall(async () => {
|
|
12
|
-
const data = await this.getPublicProfile();
|
|
13
|
-
this._setData(data);
|
|
14
|
-
return data;
|
|
15
|
-
});
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Mettre à jour les paramètres d'un élément : Mise à jour des paramètres spécifiques d'un élément.
|
|
20
|
-
* Constant : UPDATE_SETTINGS
|
|
21
|
-
* @param {Object} data - Les données à envoyer.
|
|
22
|
-
* @param {string} data.type - data.type
|
|
23
|
-
* @param {undefined} data.value - data.value
|
|
24
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
25
|
-
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
26
|
-
* @throws {ApiAuthenticationError} - En cas d'erreur d'authentification.
|
|
27
|
-
* @throws {Error} - En cas d'erreur inattendue.
|
|
28
|
-
*/
|
|
29
|
-
async updateSettings(data = {}) {
|
|
30
|
-
data.idEntity = this.id;
|
|
31
|
-
data.typeEntity = this.getEntityType();
|
|
32
|
-
return this.callIsConnected(() => this.endpointApi.updateSettings(data));
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Mettre à jour la description d'un élément : Permet de mettre à jour la description courte et complète d'un élément.
|
|
37
|
-
* Constant : UPDATE_BLOCK_DESCRIPTION
|
|
38
|
-
* @param {Object} data - Les données à envoyer.
|
|
39
|
-
* @param {string} data.descMentions - Mentions dans la description (default: "")
|
|
40
|
-
* @param {string} data.shortDescription - Courte description
|
|
41
|
-
* @param {string} data.description - Description complète
|
|
42
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
43
|
-
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
44
|
-
* @throws {ApiAuthenticationError} - En cas d'erreur d'authentification.
|
|
45
|
-
* @throws {Error} - En cas d'erreur inattendue.
|
|
46
|
-
*/
|
|
47
|
-
async updateDescription(data = {}) {
|
|
48
|
-
data.typeElement = this.getEntityType();
|
|
49
|
-
data.id = this.id;
|
|
50
|
-
return this.callIsConnected(() => this.endpointApi.updateBlockDescription(data));
|
|
51
|
-
},
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Mettre à jour les informations d'un élément : Permet de mettre à jour les informations générales d'un élément (nom, contacts, etc.).
|
|
55
|
-
* Constant : UPDATE_BLOCK_INFO
|
|
56
|
-
*/
|
|
57
|
-
async updateInfo(data = {}) {
|
|
58
|
-
data.typeElement = this.getEntityType();
|
|
59
|
-
data.id = this.id;
|
|
60
|
-
return this.callIsConnected(() => this.endpointApi.updateBlockInfo(data));
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Mettre à jour les réseaux sociaux d'un élément : Permet de mettre à jour les liens vers les réseaux sociaux d'un élément.
|
|
65
|
-
* Constant : UPDATE_BLOCK_SOCIAL
|
|
66
|
-
*/
|
|
67
|
-
async updateSocial(data = {}) {
|
|
68
|
-
data.typeElement = this.getEntityType();
|
|
69
|
-
data.id = this.id;
|
|
70
|
-
return this.callIsConnected(() => this.endpointApi.updateBlockSocial(data));
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Mettre à jour les localités d'un élément : Permet de mettre à jour l'adresse et les informations géographiques d'un élément.
|
|
75
|
-
* Constant : UPDATE_BLOCK_LOCALITY
|
|
76
|
-
*/
|
|
77
|
-
async updateLocality(data = {}) {
|
|
78
|
-
data.typeElement = this.getEntityType();
|
|
79
|
-
data.id = this.id;
|
|
80
|
-
return this.callIsConnected(() => this.endpointApi.updateBlockLocality(data));
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Mettre à jour le slug d'un élément : Permet de mettre à jour le slug pour une URL simplifiée.
|
|
85
|
-
* Constant : UPDATE_BLOCK_SLUG
|
|
86
|
-
*/
|
|
87
|
-
async updateSlug({ slug }) {
|
|
88
|
-
try {
|
|
89
|
-
await this.endpointApi.check({ type: this.getEntityType(), id: this.id, slug });
|
|
90
|
-
} catch (error) {
|
|
91
|
-
if(error instanceof ApiResponseError) {
|
|
92
|
-
throw new ApiResponseError("Erreur lors de la vérification du slug.", error.status, error.data);
|
|
93
|
-
}
|
|
94
|
-
throw error;
|
|
95
|
-
}
|
|
96
|
-
return this.callIsConnected(() => this.endpointApi.updateBlockSlug({ typeElement: this.getEntityType(), id: this.id, slug }));
|
|
97
|
-
},
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Mettre à jour l'image de profil : Permet de mettre à jour l'image de profil d'un utilisateur ou d'une entité.
|
|
101
|
-
* Constant : PROFIL_IMAGE
|
|
102
|
-
*/
|
|
103
|
-
async updateImageProfil({ profil_avatar: image }) {
|
|
104
|
-
image = await this._validateImage(image);
|
|
105
|
-
const data = { pathParams: { folder: this.getEntityType(), ownerId: this.id }, profil_avatar: image };
|
|
106
|
-
return this.callIsConnected(() => this.endpointApi.profilImage(data));
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Crée une instance d'organisation et récupère son profil si nécessaire.
|
|
111
|
-
*
|
|
112
|
-
* @param {Object} organizationData - Les données nécessaires pour initialiser l'organisation.
|
|
113
|
-
* @returns {Promise<Organization>} Une promesse qui résout l'objet Organisation créé.
|
|
114
|
-
* @throws {Error} Si une erreur se produit lors de la création de l'organisation.
|
|
115
|
-
*/
|
|
116
|
-
async organization(organizationData = {}) {
|
|
117
|
-
if(!this.isMe){
|
|
118
|
-
throw new ApiError("Vous devez être connecté et être l'utilisateur pour créer une organisation.");
|
|
119
|
-
}
|
|
120
|
-
return this.entity("organizations", organizationData);
|
|
121
|
-
},
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Crée une instance de projet et récupère son profil si nécessaire.
|
|
125
|
-
*
|
|
126
|
-
* @param {Object} projectData - Les données nécessaires pour initialiser le projet.
|
|
127
|
-
* @returns {Promise<Project>} Une promesse qui résout l'objet Projet créé.
|
|
128
|
-
* @throws {Error} Si une erreur se produit lors de la création du projet.
|
|
129
|
-
*/
|
|
130
|
-
async project(projectData = {}) {
|
|
131
|
-
// TODO: Vérifier si l'utilisateur est admin de l'organisation
|
|
132
|
-
if(!this.isConnected){
|
|
133
|
-
throw new ApiError("Vous devez être connecté pour créer un projet.");
|
|
134
|
-
}
|
|
135
|
-
return this.entity("projects", projectData);
|
|
136
|
-
},
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Crée une instance de POI et la récupère si nécessaire.
|
|
140
|
-
*
|
|
141
|
-
* @param {Object} poiData - Les données nécessaires pour initialiser le POI.
|
|
142
|
-
* @returns {Promise<Poi>} Une promesse qui résout l'objet POI créé.
|
|
143
|
-
* @throws {Error} Si une erreur se produit lors de la création du POI.
|
|
144
|
-
*/
|
|
145
|
-
async poi(poiData = {}) {
|
|
146
|
-
// TODO: Vérifier si l'utilisateur est admin de l'organisation
|
|
147
|
-
if(!this.isConnected){
|
|
148
|
-
throw new ApiError("Vous devez être connecté pour créer un POI.");
|
|
149
|
-
}
|
|
150
|
-
return this.entity("poi", poiData);
|
|
151
|
-
},
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Crée une instance d'événement et la récupère si nécessaire.
|
|
155
|
-
*
|
|
156
|
-
* @param {Object} eventData - Les données nécessaires pour initialiser l'événement.
|
|
157
|
-
* @returns {Promise<Event>} Une promesse qui résout l'objet Événement créé.
|
|
158
|
-
* @throws {Error} Si une erreur se produit lors de la création de l'événement.
|
|
159
|
-
*/
|
|
160
|
-
async event(eventData = {}) {
|
|
161
|
-
// TODO: Vérifier si l'utilisateur est admin de l'organisation
|
|
162
|
-
if(!this.isConnected){
|
|
163
|
-
throw new ApiError("Vous devez être connecté pour créer un événement.");
|
|
164
|
-
}
|
|
165
|
-
return this.entity("events", eventData);
|
|
166
|
-
},
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Crée une instance de badge et la récupère si nécessaire.
|
|
170
|
-
*
|
|
171
|
-
* @param {Object} badgeData - Les données nécessaires pour initialiser le badge.
|
|
172
|
-
* @returns {Promise<Badge>} Une promesse qui résout l'objet Badge créé.
|
|
173
|
-
* @throws {Error} Si une erreur se produit lors de la création du badge.
|
|
174
|
-
*/
|
|
175
|
-
async badge(badgeData = {}) {
|
|
176
|
-
// TODO: Vérifier si l'utilisateur est admin de l'organisation
|
|
177
|
-
if(!this.isConnected){
|
|
178
|
-
throw new ApiError("Vous devez être connecté pour créer un badge.");
|
|
179
|
-
}
|
|
180
|
-
return this.entity("badges", badgeData);
|
|
181
|
-
},
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Crée une instance de news et la récupère si nécessaire.
|
|
185
|
-
*
|
|
186
|
-
* @param {Object} newsData - Les données nécessaires pour initialiser la news.
|
|
187
|
-
* @returns {Promise<News>} Une promesse qui résout l'objet News créé.
|
|
188
|
-
* @throws {Error} Si une erreur se produit lors de la création de la news.
|
|
189
|
-
*/
|
|
190
|
-
async news(newsData = {}) {
|
|
191
|
-
if(!this.isConnected){
|
|
192
|
-
throw new ApiError("Vous devez être connecté.");
|
|
193
|
-
}
|
|
194
|
-
return this.entity("news", newsData);
|
|
195
|
-
},
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Récupérer les organisations d'une entitée : la liste des organisations dont l'entité est membre ou admin valide.
|
|
199
|
-
* Constant : GET_ORGANIZATIONS_ADMIN | GET_ORGANIZATIONS_NO_ADMIN
|
|
200
|
-
* @param {Object} data - Les données à envoyer.
|
|
201
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
202
|
-
*/
|
|
203
|
-
async getOrganizations(data = {}) {
|
|
204
|
-
|
|
205
|
-
if(this.isMe){
|
|
206
|
-
data.pathParams = { type: this.getEntityType(), id: this.id };
|
|
207
|
-
// NOTE : dans le schema je crois que si pas de data.filters alors le default ce fait avec data.pathParams
|
|
208
|
-
// data.filters = {
|
|
209
|
-
// [`links.members.${this.id}`]: { "$exists": true },
|
|
210
|
-
// [`links.members.${this.id}.toBeValidated`]: { "$exists": false },
|
|
211
|
-
// [`links.members.${this.id}.isInviting`]: { "$exists": false }
|
|
212
|
-
// };
|
|
213
|
-
} else {
|
|
214
|
-
delete data?.pathParams;
|
|
215
|
-
data.filters = {
|
|
216
|
-
[`links.members.${this.id}`]: { "$exists": true },
|
|
217
|
-
[`links.members.${this.id}.toBeValidated`]: { "$exists": false },
|
|
218
|
-
[`links.members.${this.id}.isInviting`]: { "$exists": false }
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const fetchFn = this.isMe
|
|
223
|
-
? () => this.callIsMe(() => this.endpointApi.getOrganizationsAdmin(data))
|
|
224
|
-
: () => this.endpointApi.getOrganizationsNoAdmin(data);
|
|
225
|
-
|
|
226
|
-
const arrayObjetOrganizations = await fetchFn();
|
|
227
|
-
|
|
228
|
-
if (!Array.isArray(arrayObjetOrganizations.results)) {
|
|
229
|
-
throw new ApiResponseError("Erreur lors de la récupération des organisations.", 500, arrayObjetOrganizations.results);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// nettoyage du count
|
|
233
|
-
delete arrayObjetOrganizations?.count?.spam;
|
|
234
|
-
|
|
235
|
-
// calcul du total
|
|
236
|
-
const totalCount = Object.values(arrayObjetOrganizations.count || {}).reduce((acc, val) => acc + val, 0);
|
|
237
|
-
arrayObjetOrganizations.count.total = totalCount;
|
|
238
|
-
|
|
239
|
-
const rawOrganizationsList = arrayObjetOrganizations.results.map(
|
|
240
|
-
(d) => this.linkEntity?.(d.collection, d) ?? d
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
return {
|
|
244
|
-
count: arrayObjetOrganizations.count,
|
|
245
|
-
results: rawOrganizationsList
|
|
246
|
-
};
|
|
247
|
-
},
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Récupérer les projets d'une entitée : liste des projets de l'entité ou elle est "parent" ou "contributeur".
|
|
251
|
-
* Constant : GET_PROJECTS_ADMIN | GET_PROJECTS_NO_ADMIN
|
|
252
|
-
* @param {Object} data - Les données à envoyer.
|
|
253
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
254
|
-
*/
|
|
255
|
-
async getProjects(data = {}) {
|
|
256
|
-
|
|
257
|
-
if(this.isMe){
|
|
258
|
-
data.pathParams = { type: this.getEntityType(), id: this.id };
|
|
259
|
-
// NOTE : dans le schema je crois que si pas de data.filters alors le default ce fait avec data.pathParams
|
|
260
|
-
// data.filters = {
|
|
261
|
-
// "$or": {
|
|
262
|
-
// [`links.contributors.${this.id}`]: { "$exists": true },
|
|
263
|
-
// [`parent.${this.id}`]: { "$exists": true }
|
|
264
|
-
// },
|
|
265
|
-
// [`links.contributors.${this.id}`]: { "$exists": true }
|
|
266
|
-
// };
|
|
267
|
-
} else {
|
|
268
|
-
delete data?.pathParams;
|
|
269
|
-
data.filters = {
|
|
270
|
-
"$or": {
|
|
271
|
-
[`links.contributors.${this.id}`]: { "$exists": true },
|
|
272
|
-
[`parent.${this.id}`]: { "$exists": true }
|
|
273
|
-
},
|
|
274
|
-
[`links.contributors.${this.id}`]: { "$exists": true }
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const fetchFn = this.isMe
|
|
279
|
-
? () => this.callIsMe(() => this.endpointApi.getProjectsAdmin(data))
|
|
280
|
-
: () => this.endpointApi.getProjectsNoAdmin(data);
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const arrayObjetProjects = await fetchFn();
|
|
284
|
-
|
|
285
|
-
if (!Array.isArray(arrayObjetProjects.results)) {
|
|
286
|
-
throw new ApiResponseError("Erreur lors de la récupération des projets.", 500, arrayObjetProjects.results);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const rawProjectsList = arrayObjetProjects.results.map(
|
|
290
|
-
(d) => this.linkEntity?.(d.collection, d) ?? d
|
|
291
|
-
);
|
|
292
|
-
|
|
293
|
-
return {
|
|
294
|
-
count: arrayObjetProjects?.count?.projects ?? 0,
|
|
295
|
-
results: rawProjectsList
|
|
296
|
-
};
|
|
297
|
-
},
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Récupérer les POIs d'une entité : liste des POIs de l'entité ou elle est "parent".
|
|
301
|
-
* Constant : GET_POIS_NO_ADMIN / GET_POIS_ADMIN
|
|
302
|
-
* @param {Object} data - Les données à envoyer.
|
|
303
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
304
|
-
*/
|
|
305
|
-
async getPois(data = {}) {
|
|
306
|
-
|
|
307
|
-
if(this.isMe){
|
|
308
|
-
data.pathParams = { type: this.getEntityType(), id: this.id };
|
|
309
|
-
// NOTE : dans le schema je crois que si pas de data.filters alors le default ce fait avec data.pathParams
|
|
310
|
-
// data.filters = {
|
|
311
|
-
// [`parent.${this.id}`]: { "$exists": true },
|
|
312
|
-
// };
|
|
313
|
-
} else {
|
|
314
|
-
delete data?.pathParams;
|
|
315
|
-
data.filters = {
|
|
316
|
-
[`parent.${this.id}`]: { "$exists": true },
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
const fetchFn = this.isMe
|
|
321
|
-
? () => this.callIsMe(() => this.endpointApi.getPoisAdmin(data))
|
|
322
|
-
: () => this.endpointApi.getPoisNoAdmin(data);
|
|
323
|
-
|
|
324
|
-
const arrayObjetPois = await fetchFn();
|
|
325
|
-
if (!Array.isArray(arrayObjetPois.results)) {
|
|
326
|
-
throw new ApiResponseError("Erreur lors de la récupération des POIs.", 500, arrayObjetPois.results);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// lier les entités au objets
|
|
330
|
-
const rawPoisList = arrayObjetPois.results.map(
|
|
331
|
-
(d) => this.linkEntity?.(d.collection, d) ?? d
|
|
332
|
-
);
|
|
333
|
-
|
|
334
|
-
return {
|
|
335
|
-
count: arrayObjetPois.count?.poi ?? 0,
|
|
336
|
-
results: rawPoisList
|
|
337
|
-
};
|
|
338
|
-
},
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Récupérer les abonnés d'une entité
|
|
342
|
-
* Constant : GET_SUBSCRIBERS
|
|
343
|
-
* @param {Object} data - Les données à envoyer.
|
|
344
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
345
|
-
*/
|
|
346
|
-
async getSubscribers(data = {}) {
|
|
347
|
-
delete data?.pathParams;
|
|
348
|
-
|
|
349
|
-
data.filters = {
|
|
350
|
-
[`links.follows.${this.id}`]: { "$exists": true },
|
|
351
|
-
[`links.follows.${this.id}.toBeValidated`]: { "$exists": false },
|
|
352
|
-
[`links.follows.${this.id}.isInviting`]: { "$exists": false }
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
const arrayObjetSubscribers = await this.endpointApi.getSubscribers(data);
|
|
356
|
-
if (!Array.isArray(arrayObjetSubscribers.results)) {
|
|
357
|
-
throw new ApiResponseError("Erreur lors de la récupération des abonnés.", 500, arrayObjetSubscribers.results);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// lier les entités au objets
|
|
361
|
-
const rawSubscribersList = arrayObjetSubscribers.results.map(
|
|
362
|
-
(d) => this.linkEntity?.(d.collection, d) ?? d
|
|
363
|
-
);
|
|
364
|
-
|
|
365
|
-
return {
|
|
366
|
-
count: arrayObjetSubscribers.count,
|
|
367
|
-
results: rawSubscribersList
|
|
368
|
-
};
|
|
369
|
-
},
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Liste des badges créés par l'entité
|
|
373
|
-
* Constant : GET_BADGES
|
|
374
|
-
* @param {Object} data - Les données à envoyer.
|
|
375
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
376
|
-
*/
|
|
377
|
-
async getBadgesIssuer(data = {}) {
|
|
378
|
-
delete data?.pathParams;
|
|
379
|
-
|
|
380
|
-
data.filters = data.filters || {};
|
|
381
|
-
data.filters["$or"] = {};
|
|
382
|
-
data.filters["$or"][`issuer.${this.id}`] = { "$exists": true };
|
|
383
|
-
|
|
384
|
-
const arrayObjetBadges = await this.endpointApi.getBadges(data);
|
|
385
|
-
if (!Array.isArray(arrayObjetBadges.results)) {
|
|
386
|
-
throw new ApiResponseError("Erreur lors de la récupération des badges.", 500, arrayObjetBadges.results);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// lier les entités au objets
|
|
390
|
-
const rawBadgesList = arrayObjetBadges.results.map(
|
|
391
|
-
(d) => this.linkEntity?.(d.collection, d) ?? d
|
|
392
|
-
);
|
|
393
|
-
|
|
394
|
-
return {
|
|
395
|
-
count: arrayObjetBadges.count?.badges ?? 0,
|
|
396
|
-
results: rawBadgesList
|
|
397
|
-
};
|
|
398
|
-
},
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* Récupérer les actualités : Récupère la liste d’actualités selon plusieurs critères.
|
|
402
|
-
* Constant : GET_NEWS
|
|
403
|
-
* @param {Object} data - Les données à envoyer.
|
|
404
|
-
* @param {number} data.dateLimit - Limite de date timestamp ou 0 (default: 0)
|
|
405
|
-
* @param {object} data.search - data.search
|
|
406
|
-
* @param {string} data.search.name - Nom ou terme recherché (default: "")
|
|
407
|
-
* @param {number} data.indexStep - Nombre de résultats par page (default: 12)
|
|
408
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
409
|
-
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
410
|
-
* @throws {Error} - En cas d'erreur inattendue.
|
|
411
|
-
*/
|
|
412
|
-
async getNews(data = {}) {
|
|
413
|
-
data.pathParams = { type: this.getEntityType(), id: this.id };
|
|
414
|
-
const arrayObjetNews = await this.endpointApi.getNews(data);
|
|
415
|
-
if(!Array.isArray(arrayObjetNews)){
|
|
416
|
-
throw new ApiResponseError("Erreur lors de la récupération des actualités.", 500, arrayObjetNews);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
const rawNewsList = arrayObjetNews.map(
|
|
420
|
-
(d) => this.linkEntity?.(d.collection, d) ?? d
|
|
421
|
-
);
|
|
422
|
-
|
|
423
|
-
return this._createFilteredProxy(rawNewsList);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
};
|
|
428
|
-
|