@communecter/cocolight-api-client 1.0.46 → 1.0.47

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@communecter/cocolight-api-client",
3
- "version": "1.0.46",
3
+ "version": "1.0.47",
4
4
  "description": "Client Axios simplifié pour l'API cocolight",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1080,6 +1080,57 @@ export class BaseEntity {
1080
1080
  return Object.keys(changed).length > 0 ? changed : null;
1081
1081
  }
1082
1082
 
1083
+ /**
1084
+ * Extrait tous les champs valides selon le schéma, et retourne uniquement ceux qui ont changé par rapport au draft initial.
1085
+ * Contrairement à `_extractChangedFieldsFromSchema`, cette méthode retourne l'ensemble des champs valides (`updated`)
1086
+ * uniquement s'il y a au moins un champ modifié (`changed`).
1087
+ *
1088
+ * ⚠️ Les champs sont filtrés en fonction :
1089
+ * - des champs définis comme modifiables dans le schéma (`writeable`)
1090
+ * - des champs non exclus dans `removeFields`
1091
+ * - des champs définis dans `data` (les `undefined` sont ignorés)
1092
+ *
1093
+ * @param {ApiClient} apiClient - L’instance de client API contenant les schémas.
1094
+ * @param {string} constant - Le nom de la constante de schéma (ex: "ADD_EVENT").
1095
+ * @param {Object} data - Les nouvelles données à comparer avec le draft initial.
1096
+ * @param {() => Object} getInitialDraft - Fonction qui retourne le draft initial (souvent `this.initialDraftData`).
1097
+ * @param {string[]} [removeFields=[]] - Champs à ignorer même s’ils sont valides.
1098
+ * @returns {Object|null} - Un objet `updated` avec les champs valides si au moins un champ a changé, sinon `null`.
1099
+ * @private
1100
+ */
1101
+ _extractAllValidFieldsFromSchema(apiClient, constant, data = {}, getInitialDraft, removeFields = []) {
1102
+ const schema = apiClient.getRequestSchema(constant);
1103
+ let allowed = this._extractWritableFields(schema, data);
1104
+ const changed = {};
1105
+ const updated = {};
1106
+ const initialDraft = getInitialDraft?.() || {};
1107
+
1108
+ // on enlève les champs qui ne sont pas dans le draft
1109
+ // ou qui sont dans removeFields
1110
+ allowed = allowed.filter(k => !removeFields.includes(k));
1111
+
1112
+ for (const key of allowed) {
1113
+ // on verifie que le champ existe dans le draft
1114
+ // sinon on ne le prend pas en compte
1115
+
1116
+ if (data[key] === undefined) continue;
1117
+
1118
+ const current = data[key];
1119
+ const initial = initialDraft[key];
1120
+
1121
+ const changedValue =
1122
+ JSON.stringify(current) !== JSON.stringify(initial);
1123
+
1124
+ if (changedValue) {
1125
+ changed[key] = current;
1126
+ }
1127
+
1128
+ updated[key] = current;
1129
+ }
1130
+
1131
+ return Object.keys(changed).length > 0 ? updated : null;
1132
+ }
1133
+
1083
1134
  /**
1084
1135
  * ───────────────────────────────
1085
1136
  * MutualEntityMixin
@@ -131,9 +131,9 @@ export interface UpdateSettingsData {
131
131
  type: string;
132
132
  value: unknown;
133
133
  /**
134
- * Type d'élément (citoyens, projects, organizations)
134
+ * Type d'élément (citoyens, projects, organizations, events)
135
135
  */
136
- typeEntity: "citoyens" | "projects" | "organizations";
136
+ typeEntity: "citoyens" | "projects" | "organizations" | "events";
137
137
  /**
138
138
  * Id de l'élément
139
139
  */
@@ -148,9 +148,9 @@ export interface UpdateBlockDescriptionData {
148
148
  */
149
149
  block: "descriptions";
150
150
  /**
151
- * Type d'élément (citoyens, projects, organizations, poi)
151
+ * Type d'élément (citoyens, projects, organizations, poi, events)
152
152
  */
153
- typeElement: "citoyens" | "projects" | "organizations" | "poi";
153
+ typeElement: "citoyens" | "projects" | "organizations" | "poi" | "events";
154
154
  /**
155
155
  * ID de l'élément concerné
156
156
  */
@@ -181,9 +181,9 @@ export interface UpdateBlockInfoData {
181
181
  */
182
182
  block: "info";
183
183
  /**
184
- * Type d'élément (citoyens, projects, organizations, poi)
184
+ * Type d'élément (citoyens, projects, organizations, poi, events)
185
185
  */
186
- typeElement: "citoyens" | "projects" | "organizations" | "poi";
186
+ typeElement: "citoyens" | "projects" | "organizations" | "poi" | "events";
187
187
  /**
188
188
  * ID de l'élément concerné
189
189
  */
@@ -274,7 +274,7 @@ export interface UpdateBlockLocalityData {
274
274
  /**
275
275
  * Type d'élément
276
276
  */
277
- typeElement: "citoyens" | "projects" | "organizations" | "poi";
277
+ typeElement: "citoyens" | "projects" | "organizations" | "poi" | "events";
278
278
  /**
279
279
  * ID de l'élément concerné
280
280
  */
@@ -349,9 +349,9 @@ export interface UpdateBlockSlugData {
349
349
  */
350
350
  block: "info";
351
351
  /**
352
- * Type d'élément (citoyens, projects, organizations, poi)
352
+ * Type d'élément (citoyens, projects, organizations, poi, events)
353
353
  */
354
- typeElement: "citoyens" | "projects" | "organizations" | "poi";
354
+ typeElement: "citoyens" | "projects" | "organizations" | "poi" | "events";
355
355
  /**
356
356
  * ID de l'élément concerné
357
357
  */
@@ -377,7 +377,7 @@ export interface CheckData {
377
377
  /**
378
378
  * Type d'élément
379
379
  */
380
- type: "citoyens" | "projects" | "organizations";
380
+ type: "citoyens" | "projects" | "organizations" | "events";
381
381
  /**
382
382
  * Slug à vérifier
383
383
  */
package/src/api/Event.js CHANGED
@@ -8,35 +8,55 @@ export class Event extends BaseEntity {
8
8
 
9
9
  static SCHEMA_CONSTANTS = [
10
10
  "ADD_EVENT",
11
- // "UPDATE_BLOCK_DESCRIPTION",
12
- // "UPDATE_BLOCK_INFO",
13
- // "UPDATE_BLOCK_SOCIAL",
14
- // "UPDATE_BLOCK_LOCALITY",
15
- // "UPDATE_BLOCK_SLUG",
16
- // "PROFIL_IMAGE"
11
+ "UPDATE_BLOCK_SLUG",
12
+ "PROFIL_IMAGE"
17
13
  ];
18
14
 
19
15
  static ADD_BLOCKS = new Map([
20
- ["ADD_EVENT", "addEvent"]
16
+ ["ADD_EVENT", "addEvent"],
17
+ ["PROFIL_IMAGE", "updateImageProfil"]
21
18
  ]);
22
19
 
23
20
  static UPDATE_BLOCKS = new Map([
24
- // ["UPDATE_BLOCK_DESCRIPTION", "updateDescription"],
25
- // ["UPDATE_BLOCK_INFO", "updateInfo"],
26
- // ["UPDATE_BLOCK_SOCIAL", "updateSocial"],
27
- // ["UPDATE_BLOCK_LOCALITY", "updateLocality"],
28
- // ["UPDATE_BLOCK_SLUG", "updateSlug"],
29
- // ["PROFIL_IMAGE", "updateImageProfil"]
21
+ ["UPDATE_BLOCK_SLUG", "updateSlug"],
22
+ ["PROFIL_IMAGE", "updateImageProfil"]
30
23
  ]);
31
24
 
32
25
  defaultFields = {
33
-
26
+ typeElement: this.getEntityType()
34
27
  };
35
28
 
36
- removeFields = ["typeElement"];
29
+ removeFields = [
30
+ "typeElement"
31
+ ];
37
32
 
38
33
  transforms = {
39
-
34
+ parent: (val) => {
35
+ if (!val || typeof val !== "object") return null;
36
+
37
+ return Object.fromEntries(
38
+ Object.entries(val).map(([key, obj]) => [
39
+ key,
40
+ {
41
+ type: obj?.type ?? null,
42
+ name: obj?.name ?? null
43
+ }
44
+ ])
45
+ );
46
+ },
47
+ organizer: (val) => {
48
+ if (!val || typeof val !== "object") return null;
49
+
50
+ return Object.fromEntries(
51
+ Object.entries(val).map(([key, obj]) => [
52
+ key,
53
+ {
54
+ type: obj?.type ?? null,
55
+ name: obj?.name ?? null
56
+ }
57
+ ])
58
+ );
59
+ }
40
60
  };
41
61
 
42
62
  async _add(payload) {
@@ -73,9 +93,22 @@ export class Event extends BaseEntity {
73
93
  throw new ApiError("utilisation invalide de _update, utilisez save");
74
94
  }
75
95
 
76
- if (payload.id) delete payload.id;
77
96
  let hasChanged = false;
78
97
 
98
+ const blockData = this._extractAllValidFieldsFromSchema(
99
+ this.apiClient,
100
+ "ADD_EVENT",
101
+ { ...payload, ...this.defaultFields },
102
+ () => this.initialDraftData,
103
+ this.removeFields
104
+ );
105
+ if (blockData && Object.keys(blockData).length > 0) {
106
+ await this["addEvent"](blockData);
107
+ hasChanged = true;
108
+ }
109
+
110
+ if (payload.id) delete payload.id;
111
+
79
112
  for (const [constant, methodName] of Event.UPDATE_BLOCKS) {
80
113
  const blockData = this._extractChangedFieldsFromSchema(
81
114
  this.apiClient,