@communecter/cocolight-api-client 1.0.54 → 1.0.56
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/401.cocolight-api-client.browser.js +1 -0
- package/dist/401.cocolight-api-client.cjs +1 -0
- package/dist/401.cocolight-api-client.mjs.js +1 -0
- package/dist/588.cocolight-api-client.browser.js +1 -0
- package/dist/588.cocolight-api-client.cjs +1 -0
- package/dist/588.cocolight-api-client.mjs.js +1 -0
- package/dist/593.cocolight-api-client.browser.js +1 -0
- package/dist/593.cocolight-api-client.cjs +1 -0
- package/dist/593.cocolight-api-client.mjs.js +1 -0
- package/dist/839.cocolight-api-client.browser.js +1 -0
- package/dist/839.cocolight-api-client.cjs +1 -0
- package/dist/839.cocolight-api-client.mjs.js +1 -0
- package/dist/cocolight-api-client.browser.js +3 -3
- package/dist/cocolight-api-client.cjs +1 -1
- package/dist/cocolight-api-client.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js.map +1 -1
- package/package.json +29 -17
- package/src/{Api.js → Api.ts} +85 -95
- package/src/{ApiClient.js → ApiClient.ts} +436 -247
- package/src/EJSONType.ts +103 -0
- package/src/api/{Badge.js → Badge.ts} +56 -45
- package/src/api/BaseEntity.ts +3890 -0
- package/src/api/Comment.ts +200 -0
- package/src/api/{EndpointApi.js → EndpointApi.ts} +363 -297
- package/src/api/EndpointApi.types.ts +4609 -0
- package/src/api/EntityRegistry.ts +203 -0
- package/src/api/Event.ts +332 -0
- package/src/api/News.ts +331 -0
- package/src/api/{Organization.js → Organization.ts} +155 -119
- package/src/api/{Poi.js → Poi.ts} +68 -60
- package/src/api/{Project.js → Project.ts} +150 -127
- package/src/api/{User.js → User.ts} +321 -256
- package/src/api/UserApi.ts +148 -0
- package/src/api/serverDataType/Comment.ts +88 -0
- package/src/api/serverDataType/Event.ts +80 -0
- package/src/api/serverDataType/News.ts +138 -0
- package/src/api/serverDataType/Organization.ts +80 -0
- package/src/api/serverDataType/Project.ts +71 -0
- package/src/api/serverDataType/User.ts +103 -0
- package/src/api/serverDataType/common.ts +80 -0
- package/src/endpoints.module.ts +2621 -0
- package/src/error.ts +86 -0
- package/src/index.ts +86 -0
- package/src/mixin/UserMixin.ts +4 -0
- package/src/types/api-responses.ts +217 -0
- package/src/types/entities.ts +22 -0
- package/src/types/error-guards.ts +230 -0
- package/src/types/index.ts +39 -0
- package/src/types/payloads.ts +21 -0
- package/src/types/transforms.ts +110 -0
- package/src/utils/{FileOfflineStorageStrategy.node.js → FileOfflineStorageStrategy.node.ts} +15 -12
- package/src/utils/{FileStorageStrategy.node.js → FileStorageStrategy.node.ts} +16 -39
- package/src/utils/MultiServerFileStorageStrategy.node.ts +67 -0
- package/src/utils/MultiServerTokenStorageStrategy.ts +139 -0
- package/src/utils/{OfflineClientManager.js → OfflineClientManager.ts} +82 -86
- package/src/utils/OfflineQueueStorageStrategy.ts +47 -0
- package/src/utils/TokenStorage.ts +77 -0
- package/src/utils/compat.ts +12 -0
- package/src/utils/createDefaultMultiServerTokenStorageStrategy.ts +35 -0
- package/src/utils/{createDefaultOfflineStrategy.js → createDefaultOfflineStrategy.ts} +8 -3
- package/src/utils/createDefaultTokenStorageStrategy.ts +33 -0
- package/src/utils/{reactive.js → reactive.ts} +49 -40
- package/src/utils/stream-utils.node.ts +12 -0
- package/types/Api.d.ts +38 -82
- package/types/Api.d.ts.map +1 -0
- package/types/ApiClient.d.ts +244 -184
- package/types/ApiClient.d.ts.map +1 -0
- package/types/EJSONType.d.ts +48 -22
- package/types/EJSONType.d.ts.map +1 -0
- package/types/api/Badge.d.ts +20 -20
- package/types/api/Badge.d.ts.map +1 -0
- package/types/api/BaseEntity.d.ts +751 -446
- package/types/api/BaseEntity.d.ts.map +1 -0
- package/types/api/Comment.d.ts +36 -0
- package/types/api/EndpointApi.d.ts +347 -295
- package/types/api/EndpointApi.d.ts.map +1 -0
- package/types/api/EndpointApi.types.d.ts +3914 -4133
- package/types/api/EntityRegistry.d.ts +18 -16
- package/types/api/EntityRegistry.d.ts.map +1 -0
- package/types/api/Event.d.ts +119 -35
- package/types/api/Event.d.ts.map +1 -0
- package/types/api/News.d.ts +52 -20
- package/types/api/News.d.ts.map +1 -0
- package/types/api/Organization.d.ts +165 -49
- package/types/api/Organization.d.ts.map +1 -0
- package/types/api/Poi.d.ts +51 -22
- package/types/api/Poi.d.ts.map +1 -0
- package/types/api/Project.d.ts +151 -52
- package/types/api/Project.d.ts.map +1 -0
- package/types/api/User.d.ts +222 -93
- package/types/api/User.d.ts.map +1 -0
- package/types/api/UserApi.d.ts +60 -9
- package/types/api/UserApi.d.ts.map +1 -0
- package/types/api/serverDataType/Comment.d.ts +83 -0
- package/types/api/serverDataType/Event.d.ts +67 -0
- package/types/api/serverDataType/News.d.ts +130 -0
- package/types/api/serverDataType/Organization.d.ts +65 -0
- package/types/api/serverDataType/Organization.d.ts.map +1 -0
- package/types/api/serverDataType/Project.d.ts +58 -0
- package/types/api/serverDataType/Project.d.ts.map +1 -0
- package/types/api/serverDataType/User.d.ts +86 -0
- package/types/api/serverDataType/User.d.ts.map +1 -0
- package/types/api/serverDataType/common.d.ts +71 -0
- package/types/api/serverDataType/common.d.ts.map +1 -0
- package/types/endpoints.module.d.ts +6922 -1215
- package/types/endpoints.module.d.ts.map +1 -0
- package/types/error.d.ts +25 -51
- package/types/error.d.ts.map +1 -0
- package/types/index.d.ts +55 -48
- package/types/index.d.ts.map +1 -0
- package/types/mixin/UserMixin.d.ts +1 -1
- package/types/mixin/UserMixin.d.ts.map +1 -0
- package/types/types/api-responses.d.ts +190 -0
- package/types/types/api-responses.d.ts.map +1 -0
- package/types/types/entities.d.ts +17 -0
- package/types/types/entities.d.ts.map +1 -0
- package/types/types/error-guards.d.ts +99 -0
- package/types/types/error-guards.d.ts.map +1 -0
- package/types/types/index.d.ts +7 -0
- package/types/types/payloads.d.ts +17 -0
- package/types/types/payloads.d.ts.map +1 -0
- package/types/types/transforms.d.ts +79 -0
- package/types/types/transforms.d.ts.map +1 -0
- package/types/utils/FileOfflineStorageStrategy.node.d.ts +10 -9
- package/types/utils/FileOfflineStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/FileStorageStrategy.node.d.ts +9 -20
- package/types/utils/FileStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/MultiServerFileStorageStrategy.node.d.ts +13 -18
- package/types/utils/MultiServerFileStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/MultiServerTokenStorageStrategy.d.ts +30 -51
- package/types/utils/MultiServerTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/OfflineClientManager.d.ts +52 -88
- package/types/utils/OfflineClientManager.d.ts.map +1 -0
- package/types/utils/OfflineQueueStorageStrategy.d.ts +12 -9
- package/types/utils/OfflineQueueStorageStrategy.d.ts.map +1 -0
- package/types/utils/TokenStorage.d.ts +20 -70
- package/types/utils/TokenStorage.d.ts.map +1 -0
- package/types/utils/compat.d.ts +4 -0
- package/types/utils/compat.d.ts.map +1 -0
- package/types/utils/createDefaultMultiServerTokenStorageStrategy.d.ts +2 -11
- package/types/utils/createDefaultMultiServerTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/createDefaultOfflineStrategy.d.ts +2 -3
- package/types/utils/createDefaultOfflineStrategy.d.ts.map +1 -0
- package/types/utils/createDefaultTokenStorageStrategy.d.ts +2 -12
- package/types/utils/createDefaultTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/reactive.d.ts +10 -16
- package/types/utils/reactive.d.ts.map +1 -0
- package/types/utils/stream-utils.node.d.ts +3 -2
- package/types/utils/stream-utils.node.d.ts.map +1 -0
- package/dist/123.cocolight-api-client.browser.js +0 -1
- package/dist/123.cocolight-api-client.cjs +0 -1
- package/dist/22.cocolight-api-client.mjs.js +0 -1
- package/dist/339.cocolight-api-client.mjs.js +0 -1
- package/dist/394.cocolight-api-client.browser.js +0 -1
- package/dist/394.cocolight-api-client.cjs +0 -1
- package/dist/405.cocolight-api-client.browser.js +0 -1
- package/dist/405.cocolight-api-client.cjs +0 -1
- package/dist/774.cocolight-api-client.mjs.js +0 -1
- package/dist/790.cocolight-api-client.mjs.js +0 -1
- package/dist/931.cocolight-api-client.browser.js +0 -1
- package/dist/931.cocolight-api-client.cjs +0 -1
- package/src/EJSONType.js +0 -53
- package/src/api/BaseEntity.js +0 -2828
- package/src/api/EntityRegistry.js +0 -152
- package/src/api/Event.js +0 -226
- package/src/api/News.js +0 -244
- package/src/api/UserApi.js +0 -81
- package/src/endpoints.module.js +0 -5
- package/src/error.js +0 -121
- package/src/index.js +0 -97
- package/src/mixin/UserMixin.js +0 -8
- package/src/utils/MultiServerFileStorageStrategy.node.js +0 -87
- package/src/utils/MultiServerTokenStorageStrategy.js +0 -188
- package/src/utils/OfflineQueueStorageStrategy.js +0 -51
- package/src/utils/TokenStorage.js +0 -153
- package/src/utils/createDefaultMultiServerTokenStorageStrategy.js +0 -51
- package/src/utils/createDefaultTokenStorageStrategy.js +0 -49
- package/src/utils/stream-utils.node.js +0 -10
package/src/api/News.ts
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { ApiError, ApiResponseError } from "../error.js";
|
|
2
|
+
import { BaseEntity } from "./BaseEntity.js";
|
|
3
|
+
|
|
4
|
+
import type { Comment } from "./Comment.js";
|
|
5
|
+
import type { AddNewsData, UpdateNewsData, DeleteNewsData, AddImageNewsData, AddFileNewsData, GetCommentsData } from "./EndpointApi.types.js";
|
|
6
|
+
import type { NewsItemNormalized } from "./serverDataType/News.js";
|
|
7
|
+
|
|
8
|
+
export class News extends BaseEntity<NewsItemNormalized> {
|
|
9
|
+
static override entityType = "news";
|
|
10
|
+
|
|
11
|
+
static override entityTag = "News";
|
|
12
|
+
|
|
13
|
+
static override SCHEMA_CONSTANTS: string[] = [
|
|
14
|
+
"ADD_NEWS"
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
static ADD_BLOCKS = new Map([
|
|
18
|
+
["ADD_NEWS", "addNews"]
|
|
19
|
+
] as const);
|
|
20
|
+
|
|
21
|
+
static UPDATE_BLOCKS = new Map([
|
|
22
|
+
["ADD_NEWS", "updateNews"]
|
|
23
|
+
] as const);
|
|
24
|
+
|
|
25
|
+
override defaultFields: Record<string, any> = {};
|
|
26
|
+
|
|
27
|
+
override removeFields: string[] = ["parentId", "parentType"];
|
|
28
|
+
|
|
29
|
+
override transforms: {
|
|
30
|
+
scope: (val: any) => any;
|
|
31
|
+
mentions: (val: any) => any[];
|
|
32
|
+
mediaImg: (val: any) => {
|
|
33
|
+
countImages: number;
|
|
34
|
+
images: string[];
|
|
35
|
+
};
|
|
36
|
+
mediaFile: (val: any) => {
|
|
37
|
+
countFiles: number;
|
|
38
|
+
files: any[];
|
|
39
|
+
};
|
|
40
|
+
} = {
|
|
41
|
+
scope: val => val?.type,
|
|
42
|
+
mentions: val =>
|
|
43
|
+
Array.isArray(val)
|
|
44
|
+
? val.map(m => ({ ...m, count: parseInt(m.count) }))
|
|
45
|
+
: [],
|
|
46
|
+
mediaImg: val => {
|
|
47
|
+
const images = val?.images?.map((img: any) => img.id).filter(Boolean) || [];
|
|
48
|
+
return images.length > 0 ? { countImages: images.length, images } : { countImages: 0, images: [] };
|
|
49
|
+
},
|
|
50
|
+
mediaFile: val => {
|
|
51
|
+
const files = val?.files?.map((f: any) => f.id).filter(Boolean) || [];
|
|
52
|
+
return files.length > 0 ? { countFiles: files.length, files } : { countFiles: 0, files: [] };
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Transforme les champs imbriqués (author, target, sharedBy, etc.) en instances d'entités.
|
|
58
|
+
* @param data - Les données brutes du serveur.
|
|
59
|
+
* @returns Les données transformées.
|
|
60
|
+
* @protected
|
|
61
|
+
*/
|
|
62
|
+
protected override _transformServerData(data: NewsItemNormalized): NewsItemNormalized {
|
|
63
|
+
// Transformer author en instance User/Organization
|
|
64
|
+
if (data.author) {
|
|
65
|
+
data.author = this._linkNestedEntity(data.author);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Transformer target en instance User/Organization/Project
|
|
69
|
+
if (data.target) {
|
|
70
|
+
data.target = this._linkNestedEntity(data.target);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Transformer lastAuthorShare en instance
|
|
74
|
+
if (data.lastAuthorShare) {
|
|
75
|
+
data.lastAuthorShare = this._linkNestedEntity(data.lastAuthorShare);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Transformer le tableau sharedBy
|
|
79
|
+
if (Array.isArray(data.sharedBy)) {
|
|
80
|
+
data.sharedBy = data.sharedBy.map(item => this._linkNestedEntity(item));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return data;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Récupérer des actualités par IDs : Récupère des actualités à partir d'une liste d'identifiants.
|
|
88
|
+
* Constant : GET_NEWS_BY_ID
|
|
89
|
+
*/
|
|
90
|
+
override async get(): Promise<Record<string, any>> {
|
|
91
|
+
if (!this.id) throw new ApiError("Impossible de rafraîchir sans ID.", 400);
|
|
92
|
+
const id = this.id; // Type narrowing
|
|
93
|
+
|
|
94
|
+
const newsArray = await this.callIsConnected(() =>
|
|
95
|
+
this.endpointApi.getNewsById({ ids: [id] })
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
if (newsArray && Array.isArray(newsArray) && newsArray.length === 1) {
|
|
99
|
+
const data = newsArray[0];
|
|
100
|
+
this._setData(data, { forceInitialDraftReset: true });
|
|
101
|
+
return this.serverData;
|
|
102
|
+
}
|
|
103
|
+
throw new ApiError(`Aucune actualité trouvée pour l'ID ${this.id}`, 404);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
override _add = async (payload: Record<string, any>): Promise<void> => {
|
|
108
|
+
if (!this._calledFromSave) {
|
|
109
|
+
throw new Error("utilisation invalide de _add, utilisez save");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// si le texte est vide, on met un espace pour que le champ soit pris en compte car sur le serveur
|
|
113
|
+
// il y a une vérification de la taille du texte je pense
|
|
114
|
+
if(payload.text === ""){
|
|
115
|
+
payload.text = " ";
|
|
116
|
+
}
|
|
117
|
+
if (this.parent) {
|
|
118
|
+
payload.parentId = this.parent.id;
|
|
119
|
+
payload.parentType = this.parent.getEntityType();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// const data = await this.callIsConnected(() => this.endpointApi.addNews(payload));
|
|
123
|
+
// this._draftData.id = data.object.id;
|
|
124
|
+
// // TODO : voir si j'ai ce qui faut dans reponseData de ADD_NEWS pour mettre à jour #serverData
|
|
125
|
+
// // c'est dans reponseData.object
|
|
126
|
+
// // if(data?.object){
|
|
127
|
+
// // this._serverData = { ...data.object };
|
|
128
|
+
// // }
|
|
129
|
+
|
|
130
|
+
for (const [constant, methodName] of Array.from(News.ADD_BLOCKS)) {
|
|
131
|
+
const blockData = this._extractChangedFieldsFromSchema(
|
|
132
|
+
this.apiClient,
|
|
133
|
+
constant,
|
|
134
|
+
{ ...payload, ...this.defaultFields },
|
|
135
|
+
() => {},
|
|
136
|
+
[]
|
|
137
|
+
);
|
|
138
|
+
if (blockData && Object.keys(blockData).length > 0) {
|
|
139
|
+
const data = await this._invokeBlockMethod(News.ADD_BLOCKS, methodName, blockData);
|
|
140
|
+
if (!this.id && data?.object?.id) {
|
|
141
|
+
this._draftData.id = data.object.id;
|
|
142
|
+
// this._serverData = { ...data.object };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
override _update = async (payload: Record<string, any>): Promise<boolean> => {
|
|
149
|
+
if (!this._calledFromSave) {
|
|
150
|
+
throw new Error("utilisation invalide de _update, utilisez save");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (payload.id) delete payload.id;
|
|
154
|
+
|
|
155
|
+
// si le texte est vide, on met un espace pour que le champ soit pris en compte car sur le serveur
|
|
156
|
+
// il y a une vérification de la taille du texte je pense
|
|
157
|
+
if(payload?.text === ""){
|
|
158
|
+
payload.text = " ";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (this.parent) {
|
|
162
|
+
payload.parentId = this.parent.id;
|
|
163
|
+
payload.parentType = this.parent.getEntityType();
|
|
164
|
+
}
|
|
165
|
+
payload.idNews = this.id;
|
|
166
|
+
|
|
167
|
+
let hasChanged = false;
|
|
168
|
+
await this.callIsConnected(() => this.endpointApi.updateNews(payload as UpdateNewsData)) as any;
|
|
169
|
+
// TODO : voir si j'ai ce qui faut dans data de UPDATE_NEWS pour mettre à jour #serverData
|
|
170
|
+
// c'est dans data.object
|
|
171
|
+
// if(data?.object){
|
|
172
|
+
// this._serverData = { ...data.object };
|
|
173
|
+
// }
|
|
174
|
+
hasChanged = true;
|
|
175
|
+
return hasChanged;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
async addNews(data: Partial<AddNewsData> = {}): Promise<unknown> {
|
|
179
|
+
return this.callIsConnected(() => this.endpointApi.addNews(data as AddNewsData));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async updateNews(data: Partial<UpdateNewsData> = {}): Promise<unknown> {
|
|
183
|
+
return this.callIsConnected(() => this.endpointApi.updateNews(data as UpdateNewsData));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async addMention({ slug, id }: { slug?: string; id?: string; }): Promise<any[]> {
|
|
187
|
+
try {
|
|
188
|
+
if (!slug && !id) {
|
|
189
|
+
throw new ApiError("Vous devez fournir un slug ou un id pour ajouter une mention.", 400);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const userInstance = await this.entity("citoyens", { id, slug });
|
|
193
|
+
// const userInstance = new this.deps.User(this.apiClient, { id, slug }, { EndpointApi: this.endpointApi });
|
|
194
|
+
const user = userInstance.serverData;
|
|
195
|
+
|
|
196
|
+
if (!this._draftData.mentions) {
|
|
197
|
+
this._draftData.mentions = [];
|
|
198
|
+
}
|
|
199
|
+
// Vérification si la mention existe déjà
|
|
200
|
+
if (this._draftData.mentions.find((mention: any) => mention.id === user.id)) {
|
|
201
|
+
// Si la mention existe déjà, on incrémente le compteur
|
|
202
|
+
this._draftData.mentions = this._draftData.mentions.map((mention: any) => {
|
|
203
|
+
if (mention.id === user.id) {
|
|
204
|
+
mention.count += 1;
|
|
205
|
+
}
|
|
206
|
+
return mention;
|
|
207
|
+
});
|
|
208
|
+
return this._draftData.mentions;
|
|
209
|
+
}
|
|
210
|
+
const mention = { id: user.id, slug: user.slug, type: userInstance.getEntityType(), name: user.name, value: user.name, count: 1 };
|
|
211
|
+
this._draftData.mentions.push(mention);
|
|
212
|
+
return this._draftData.mentions;
|
|
213
|
+
} catch (error) {
|
|
214
|
+
this.apiClient._logger.error("Erreur lors de l'ajout de la mention :", error);
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Ajouter une image à une actualité : Ajoute une images à une actualité.
|
|
223
|
+
* Constant : ADD_IMAGE_NEWS
|
|
224
|
+
*/
|
|
225
|
+
async addImage(image: File | Blob | Buffer | import("stream").Readable): Promise<{ id: string; [key: string]: any }> {
|
|
226
|
+
const validatedImage = await this._validateImage(image);
|
|
227
|
+
const data: AddImageNewsData = {
|
|
228
|
+
pathParams: {
|
|
229
|
+
folder: this.parent?.getEntityType() as "citoyens" | "projects" | "organizations",
|
|
230
|
+
ownerId: this.parent?.id as string
|
|
231
|
+
},
|
|
232
|
+
newsImage: validatedImage as any
|
|
233
|
+
};
|
|
234
|
+
const dataImage = await this.callIsConnected(() => this.endpointApi.addImageNews(data)) as { id: string; [key: string]: any };
|
|
235
|
+
if (this._draftData.mediaImg) {
|
|
236
|
+
this._draftData.mediaImg.countImages = this._draftData.mediaImg.countImages + 1;
|
|
237
|
+
this._draftData.mediaImg.images.push(dataImage.id);
|
|
238
|
+
} else {
|
|
239
|
+
this._draftData.mediaImg = { countImages: 1, images: [dataImage.id] };
|
|
240
|
+
}
|
|
241
|
+
return dataImage;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Ajouter un fichier à une actualité : Ajoute un fichier à une actualité.
|
|
246
|
+
* Constant : ADD_FILE_NEWS
|
|
247
|
+
*/
|
|
248
|
+
async addFile(file: File | Blob | Buffer | import("stream").Readable): Promise<unknown> {
|
|
249
|
+
const validatedFile = await this._validateFile(file);
|
|
250
|
+
const data: AddFileNewsData = {
|
|
251
|
+
pathParams: {
|
|
252
|
+
folder: this.parent?.getEntityType() as "citoyens" | "projects" | "organizations",
|
|
253
|
+
ownerId: this.parent?.id as string
|
|
254
|
+
},
|
|
255
|
+
newsFile: validatedFile as any
|
|
256
|
+
};
|
|
257
|
+
const dataFile = await this.callIsConnected(() => this.endpointApi.addFileNews(data));
|
|
258
|
+
if (this._draftData.mediaFile) {
|
|
259
|
+
this._draftData.mediaFile.countFiles = this._draftData.mediaFile.countFiles + 1;
|
|
260
|
+
this._draftData.mediaFile.files.push(dataFile);
|
|
261
|
+
} else {
|
|
262
|
+
this._draftData.mediaFile = { countFiles: 1, files: [dataFile] };
|
|
263
|
+
}
|
|
264
|
+
return dataFile;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Supprimer une actualité : Supprime une actualité existante.
|
|
269
|
+
* Constant : DELETE_NEWS
|
|
270
|
+
*/
|
|
271
|
+
async delete(): Promise<void> {
|
|
272
|
+
if(!this.id) {
|
|
273
|
+
throw new ApiError("Vous devez fournir un id pour supprimer une news.", 400);
|
|
274
|
+
}
|
|
275
|
+
const data = {
|
|
276
|
+
pathParams: { id: this.id },
|
|
277
|
+
isLive: false // Valeur par défaut, peut être surchargée
|
|
278
|
+
};
|
|
279
|
+
await this.callIsConnected(() => this.endpointApi.deleteNews(data as DeleteNewsData));
|
|
280
|
+
|
|
281
|
+
// Vider les objets réactifs sans casser la réactivité
|
|
282
|
+
Object.keys(this._draftData).forEach(key => delete this._draftData[key]);
|
|
283
|
+
Object.keys(this._serverData).forEach(key => delete (this._serverData as any)[key]);
|
|
284
|
+
|
|
285
|
+
// Marquer comme supprimé
|
|
286
|
+
this._isDeleted = true;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Créer une instance de commentaire pour cette news.
|
|
291
|
+
* @param commentData - Données du commentaire.
|
|
292
|
+
* @returns Instance de Comment.
|
|
293
|
+
*/
|
|
294
|
+
async comment(commentData: Record<string, any> = {}): Promise<Comment> {
|
|
295
|
+
if(!this.isConnected){
|
|
296
|
+
throw new ApiError("Vous devez être connecté.", 401);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const entity = await this.entity("comments", commentData);
|
|
300
|
+
return entity as Comment;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Récupérer les commentaires : Récupère la liste de commentaires selon plusieurs critères.
|
|
305
|
+
* Constant : GET_COMMENTS
|
|
306
|
+
* @returns - Les données de réponse.
|
|
307
|
+
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
308
|
+
* @throws {Error} - En cas d'erreur inattendue.
|
|
309
|
+
*/
|
|
310
|
+
async getComments(): Promise<Comment[]> {
|
|
311
|
+
|
|
312
|
+
if (!this.id) {
|
|
313
|
+
throw new ApiError(`${this.constructor.name} non enregistrée.`, 404);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const payload: GetCommentsData = {
|
|
317
|
+
pathParams: { type: "news", id: this.id },
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const arrayObjet = await this.endpointApi.getComments(payload);
|
|
321
|
+
|
|
322
|
+
if(!Array.isArray(arrayObjet)){
|
|
323
|
+
throw new ApiResponseError("Erreur lors de la récupération des commentaires.", 500, arrayObjet as object);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const rawList = this._linkEntities(arrayObjet);
|
|
327
|
+
|
|
328
|
+
return this._createFilteredProxy(rawList);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
}
|