@communecter/cocolight-api-client 1.0.23 → 1.0.25

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/src/api/Poi.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { ApiError } from "../error.js";
1
2
  import BaseEntity from "./BaseEntity.js";
2
3
 
3
4
  export class Poi extends BaseEntity {
@@ -39,7 +40,7 @@ export class Poi extends BaseEntity {
39
40
 
40
41
  async _add(payload) {
41
42
  if (!this._calledFromSave) {
42
- throw new Error("utilisation invalide de _add, utilisez save");
43
+ throw new ApiError("utilisation invalide de _add, utilisez save");
43
44
  }
44
45
 
45
46
  payload.id = this._newId?.();
@@ -63,8 +64,11 @@ export class Poi extends BaseEntity {
63
64
  }
64
65
 
65
66
  async _update(payload) {
67
+ if(!this.isAuthor()){
68
+ throw new ApiError("Vous n'avez pas les droits pour modifier ce POI", 403);
69
+ }
66
70
  if (!this._calledFromSave) {
67
- throw new Error("utilisation invalide de _update, utilisez save");
71
+ throw new ApiError("utilisation invalide de _update, utilisez save");
68
72
  }
69
73
 
70
74
  if (payload.id) delete payload.id;
@@ -101,23 +105,23 @@ export class Poi extends BaseEntity {
101
105
  }
102
106
 
103
107
  async getOrganizations() {
104
- throw new Error(`getOrganizations n'existe pas dans ${this.constructor.name}`);
108
+ throw new ApiError(`getOrganizations n'existe pas dans ${this.constructor.name}`);
105
109
  }
106
110
 
107
111
  async getProjects() {
108
- throw new Error(`getProjects n'existe pas dans ${this.constructor.name}`);
112
+ throw new ApiError(`getProjects n'existe pas dans ${this.constructor.name}`);
109
113
  }
110
114
 
111
115
  async getEvents() {
112
- throw new Error(`getEvents n'existe pas dans${this.constructor.name}`);
116
+ throw new ApiError(`getEvents n'existe pas dans${this.constructor.name}`);
113
117
  }
114
118
 
115
119
  async getPois() {
116
- throw new Error(`getPois n'existe pas dans ${this.constructor.name}`);
120
+ throw new ApiError(`getPois n'existe pas dans ${this.constructor.name}`);
117
121
  }
118
122
 
119
123
  async getBadgesIssuer() {
120
- throw new Error(`getBadgesIssuer n'existe pas dans ${this.constructor.name}`);
124
+ throw new ApiError(`getBadgesIssuer n'existe pas dans ${this.constructor.name}`);
121
125
  }
122
126
 
123
127
  async getNews(data = {}) {
@@ -129,19 +133,19 @@ export class Poi extends BaseEntity {
129
133
  }
130
134
 
131
135
  async project() {
132
- throw new Error(`project n'existe pas dans ${this.constructor.name}`);
136
+ throw new ApiError(`project n'existe pas dans ${this.constructor.name}`);
133
137
  }
134
138
 
135
139
  async poi() {
136
- throw new Error(`poi n'existe pas dans ${this.constructor.name}`);
140
+ throw new ApiError(`poi n'existe pas dans ${this.constructor.name}`);
137
141
  }
138
142
 
139
143
  async event() {
140
- throw new Error(`event n'existe pas dans ${this.constructor.name}`);
144
+ throw new ApiError(`event n'existe pas dans ${this.constructor.name}`);
141
145
  }
142
146
 
143
147
  async badge() {
144
- throw new Error(`badge n'existe pas dans ${this.constructor.name}`);
148
+ throw new ApiError(`badge n'existe pas dans ${this.constructor.name}`);
145
149
  }
146
150
 
147
151
  /**
@@ -149,7 +153,7 @@ export class Poi extends BaseEntity {
149
153
  *
150
154
  * @param {Object} newsData - Les données nécessaires pour initialiser la news.
151
155
  * @returns {Promise<News>} Une promesse qui résout l'objet News créé.
152
- * @throws {Error} Si une erreur se produit lors de la création de la news.
156
+ * @throws {ApiError} Si une erreur se produit lors de la création de la news.
153
157
  */
154
158
  async news(newsData = {}) {
155
159
  return super.news(newsData);
@@ -1,4 +1,4 @@
1
- import { ApiResponseError } from "../error.js";
1
+ import { ApiError, ApiResponseError } from "../error.js";
2
2
  import BaseEntity from "./BaseEntity.js";
3
3
 
4
4
  export class Project extends BaseEntity {
@@ -64,7 +64,7 @@ export class Project extends BaseEntity {
64
64
 
65
65
  async _add(payload) {
66
66
  if (!this._calledFromSave) {
67
- throw new Error("utilisation invalide de _add, utilisez save");
67
+ throw new ApiError("utilisation invalide de _add, utilisez save");
68
68
  }
69
69
 
70
70
  payload.id = this._newId?.();
@@ -88,8 +88,12 @@ export class Project extends BaseEntity {
88
88
  }
89
89
 
90
90
  async _update(payload) {
91
+ if(!this.isAdmin()){
92
+ throw new ApiError("Vous n'avez pas les droits pour modifier ce projet", 403);
93
+ }
94
+
91
95
  if (!this._calledFromSave) {
92
- throw new Error("utilisation invalide de _update, utilisez save");
96
+ throw new ApiError("utilisation invalide de _update, utilisez save");
93
97
  }
94
98
 
95
99
  if (payload.id) delete payload.id;
@@ -127,7 +131,7 @@ export class Project extends BaseEntity {
127
131
  }
128
132
 
129
133
  async getOrganizations() {
130
- throw new Error(`getOrganizations n'existe pas dans ${this.constructor.name}`);
134
+ throw new ApiError(`getOrganizations n'existe pas dans ${this.constructor.name}`);
131
135
  }
132
136
 
133
137
  async getProjects(data = {}) {
@@ -135,7 +139,7 @@ export class Project extends BaseEntity {
135
139
  }
136
140
 
137
141
  async getEvents() {
138
- throw new Error(`getEvents pas encore implémenté dans ${this.constructor.name}`);
142
+ throw new ApiError(`getEvents pas encore implémenté dans ${this.constructor.name}`);
139
143
  }
140
144
 
141
145
  async getPois(data = {}) {
@@ -226,7 +230,9 @@ export class Project extends BaseEntity {
226
230
  * @throws {Error} Si une erreur se produit lors de la création du projet.
227
231
  */
228
232
  async project(projectData = {}) {
229
- // TODO: Vérifier si l'utilisateur est admin du projet
233
+ if(!this.isAdmin()){
234
+ throw new ApiError("Vous n'avez pas les droits pour créer un projet dans ce projet", 403);
235
+ }
230
236
  return super.project(projectData);
231
237
  }
232
238
 
@@ -238,7 +244,9 @@ export class Project extends BaseEntity {
238
244
  * @throws {Error} Si une erreur se produit lors de la création du POI.
239
245
  */
240
246
  async poi(poiData = {}) {
241
- // TODO: Vérifier si l'utilisateur est admin de l'organisation
247
+ if(!this.isAdmin()){
248
+ throw new ApiError("Vous n'avez pas les droits pour créer un POI dans ce projet", 403);
249
+ }
242
250
  return super.poi(poiData);
243
251
  }
244
252
 
@@ -250,7 +258,9 @@ export class Project extends BaseEntity {
250
258
  * @throws {Error} Si une erreur se produit lors de la création de l'événement.
251
259
  */
252
260
  async event(eventData = {}) {
253
- // TODO: Vérifier si l'utilisateur est admin de l'organisation
261
+ if(!this.isAdmin()){
262
+ throw new ApiError("Vous n'avez pas les droits pour créer un événement dans ce projet", 403);
263
+ }
254
264
  return super.event(eventData);
255
265
  }
256
266
 
@@ -262,7 +272,9 @@ export class Project extends BaseEntity {
262
272
  * @throws {Error} Si une erreur se produit lors de la création du badge.
263
273
  */
264
274
  async badge(badgeData = {}) {
265
- // TODO: Vérifier si l'utilisateur est admin de l'organisation
275
+ if(!this.isAdmin()){
276
+ throw new ApiError("Vous n'avez pas les droits pour créer un badge dans ce projet", 403);
277
+ }
266
278
  return super.badge(badgeData);
267
279
  }
268
280
 
@@ -274,6 +286,7 @@ export class Project extends BaseEntity {
274
286
  * @throws {Error} Si une erreur se produit lors de la création de la news.
275
287
  */
276
288
  async news(newsData = {}) {
289
+ // TODO: qui peut créer une news sur le projet ?
277
290
  return super.news(newsData);
278
291
  }
279
292
 
package/src/api/User.js CHANGED
@@ -574,7 +574,7 @@ export class User extends BaseEntity {
574
574
  };
575
575
  const retour = await this.endpointApi.connect(data);
576
576
  // TODO : reflechier au moyen de remplir parent.serverData et this.serverData avec les data de retour pour eviter un refresh
577
- await this.parent.refresh();
577
+ await this.userContext.refresh();
578
578
  return retour;
579
579
  }
580
580
 
@@ -616,7 +616,7 @@ export class User extends BaseEntity {
616
616
  };
617
617
  const retour = await this.endpointApi.linkValidate(data);
618
618
  // TODO : reflechier au moyen de remplir parent.serverData et this.serverData avec les data de retour pour eviter un refresh
619
- await this.parent.refresh();
619
+ await this.userContext.refresh();
620
620
  return retour;
621
621
  }
622
622
 
@@ -654,7 +654,7 @@ export class User extends BaseEntity {
654
654
  };
655
655
  const retour = await this.endpointApi.disconnect(data);
656
656
  // TODO : reflechier au moyen de remplir parent.serverData et this.serverData avec les data de retour pour eviter un refresh
657
- await this.parent.refresh();
657
+ await this.userContext.refresh();
658
658
  return retour;
659
659
  }
660
660
 
@@ -691,7 +691,7 @@ export class User extends BaseEntity {
691
691
  throw new ApiError(`${this.constructor.name} non enregistrée.`);
692
692
  }
693
693
 
694
- const userLink = this.parent?.serverData?.links?.["follows"]?.[this.id] || null;
694
+ const userLink = this.userContext?.serverData?.links?.["follows"]?.[this.id] || null;
695
695
 
696
696
  if (!userLink) {
697
697
  const data = {
@@ -700,7 +700,7 @@ export class User extends BaseEntity {
700
700
  };
701
701
  const retour = await this.endpointApi.follow(data);
702
702
  // TODO : reflechier au moyen de remplir parent.serverData et this.serverData avec les data de retour pour eviter un refresh
703
- await this.parent.refresh();
703
+ await this.userContext.refresh();
704
704
  return retour;
705
705
  }
706
706
 
@@ -723,7 +723,7 @@ export class User extends BaseEntity {
723
723
  throw new ApiError(`${this.constructor.name} non enregistrée.`);
724
724
  }
725
725
 
726
- const userLink = this.parent?.serverData?.links?.["follows"]?.[this.id] || null;
726
+ const userLink = this.userContext?.serverData?.links?.["follows"]?.[this.id] || null;
727
727
 
728
728
  if (userLink) {
729
729
  const data = {
@@ -733,13 +733,61 @@ export class User extends BaseEntity {
733
733
  };
734
734
  const retour = await this.endpointApi.disconnect(data);
735
735
  // TODO : reflechir au moyen de remplir parent.serverData et this.serverData avec les data de retour pour eviter un refresh
736
- await this.parent.refresh();
736
+ await this.userContext.refresh();
737
737
  return retour;
738
738
  }
739
739
 
740
740
  throw new ApiError("Vous n'êtes pas abonné à cet utilisateur.");
741
741
  }
742
742
 
743
+ /**
744
+ * Vérifie si l'utilisateur connecté est ami avec cet utilisateur.
745
+ *
746
+ * @returns {boolean} - True si l'utilisateur connecté est ami, sinon false.
747
+ * @throws {ApiError} - Si l'utilisateur n'est pas connecté.
748
+ */
749
+ isFriend() {
750
+ if (!this.isActingUser) {
751
+ throw new ApiError("Vous devez être connecté pour vérifier si vous êtes ami.");
752
+ }
753
+ this._assertEntityType("citoyens");
754
+ const userLink = this._getLinkFromConnectedUser();
755
+ return this._validateUserLink(userLink);
756
+ }
757
+
758
+ /**
759
+ * Vérifie si l'utilisateur suit l'entité.
760
+ *
761
+ * @returns {boolean} - `true` si l'utilisateur suit l'entité, `false` sinon.
762
+ * @throws {ApiError}
763
+ */
764
+ isFollower() {
765
+ if (!this.isActingUser) {
766
+ throw new ApiError("Vous devez être connecté pour vérifier si il vous suit.");
767
+ }
768
+ if (!this.id) {
769
+ throw new ApiError(`${this.constructor.name} non enregistrée.`);
770
+ }
771
+ this._assertEntityType("citoyens");
772
+ return this._isLinked("followers");
773
+ }
774
+
775
+ /**
776
+ * Vérifie si l'utilisateur est abonné à l'entité.
777
+ *
778
+ * @returns {boolean} - `true` si l'utilisateur est abonné, `false` sinon.
779
+ * @throws {ApiError}
780
+ */
781
+ isFollowing() {
782
+ if (!this.isActingUser) {
783
+ throw new ApiError("Vous devez être connecté pour vérifier si vous le suivez.");
784
+ }
785
+ if (!this.id) {
786
+ throw new ApiError(`${this.constructor.name} non enregistrée.`);
787
+ }
788
+ this._assertEntityType("citoyens");
789
+ return this._isLinked("follows");
790
+ }
743
791
 
744
792
 
745
793
  }