-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcourse.service.ts
More file actions
348 lines (295 loc) · 12 KB
/
course.service.ts
File metadata and controls
348 lines (295 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { ChannelType, Client, Guild, GuildChannel } from 'discord.js';
import { logger } from '../../config/logger';
import { authService } from '../../services/auth.service';
interface GuildData {
uuid: string;
name: string;
memberCount: string;
configuration: {};
}
interface CategoryData {
uuid: string;
uuidGuild: string;
name: string;
position: number;
}
interface Course {
uuidCourse: string;
name: string;
isCertified: boolean;
uuidGuild: string;
uuidCategory: string;
uuidRole: string;
createdAt: string;
updatedAt: string;
}
interface Role {
uuidRole: string;
name: string;
color: string;
hoist: boolean;
position: string;
memberCount: string;
uuidGuild: string;
createdAt: string;
updatedAt: string;
}
export class CourseService {
private apiUrl: string;
private client: Client;
private readonly SIMPLON_GUILD: GuildData = {
uuid: "1338499599584722965",
name: "Simplon",
memberCount: "0",
configuration: {}
};
private readonly TEMPLATE_CATEGORY: CategoryData = {
uuid: "1344811915301490748",
uuidGuild: "1338499599584722965",
name: "Templates Formations",
position: 0
};
constructor(client: Client) {
this.apiUrl = process.env.API_URL || 'http://localhost:3000';
this.client = client;
}
private async ensureGuild(): Promise<void> {
try {
const headers = await authService.getAuthHeaders();
const response = await fetch(`${this.apiUrl}/guilds/${this.SIMPLON_GUILD.uuid}`);
if (!response.ok) {
const createResponse = await fetch(`${this.apiUrl}/guilds`, {
method: 'POST',
headers,
body: JSON.stringify(this.SIMPLON_GUILD),
});
if (!createResponse.ok) {
throw new Error('Impossible de créer la guild dans l\'API');
}
logger.info('Guild créée dans l\'API avec succès');
}
} catch (error) {
logger.error(error, 'Erreur lors de la vérification/création de la guild dans l\'API');
throw error;
}
}
private async ensureTemplateCategory(): Promise<void> {
try {
const headers = await authService.getAuthHeaders();
logger.debug({
categoryData: this.TEMPLATE_CATEGORY,
endpoint: `${this.apiUrl}/categories/${this.TEMPLATE_CATEGORY.uuid}`
}, 'Vérification de la catégorie dans l\'API');
const response = await fetch(`${this.apiUrl}/categories/${this.TEMPLATE_CATEGORY.uuid}`);
const categoryData = await response.json();
if (!response.ok || !categoryData || !categoryData.uuid) {
const createResponse = await fetch(`${this.apiUrl}/categories`, {
method: 'POST',
headers,
body: JSON.stringify(this.TEMPLATE_CATEGORY),
});
if (!createResponse.ok) {
throw new Error('Impossible de créer la catégorie dans l\'API');
}
logger.info('Catégorie créée dans l\'API avec succès');
}
} catch (error) {
logger.error(error, 'Erreur lors de la vérification/création de la catégorie dans l\'API');
throw error;
}
}
async getAllCourses(): Promise<Course[]> {
try {
const response = await fetch(`${this.apiUrl}/courses`);
if (!response.ok) throw new Error('Erreur lors de la récupération des formations');
const courses = await response.json();
return courses;
} catch (error) {
logger.error(error, 'Erreur lors de la récupération des formations');
throw error;
}
}
async getCourse(id: string): Promise<Course> {
try {
const response = await fetch(`${this.apiUrl}/courses/${id}`);
if (!response.ok) throw new Error('Formation non trouvée');
return await response.json();
} catch (error) {
logger.error(error, 'Erreur lors de la récupération de la formation');
throw error;
}
}
async createCourse(name: string, isCertified: boolean): Promise<Course> {
try {
await this.ensureGuild();
await this.ensureTemplateCategory();
const headers = await authService.getAuthHeaders();
const guildId = process.env.GUILD_ID;
if (!guildId) {
throw new Error('GUILD_ID non défini dans les variables d\'environnement');
}
const guild = await this.client.guilds.fetch(guildId);
if (!guild) {
throw new Error('Impossible de trouver le serveur Discord');
}
const categoryId = "1344811915301490748";
const forumName = name;
const existingForum = guild.channels.cache.find(
channel => channel.name === forumName && channel.type === ChannelType.GuildForum
);
if (existingForum) {
throw new Error('Une formation avec ce nom existe déjà');
}
const forum = await guild.channels.create({
name: `${name}`,
type: ChannelType.GuildForum,
parent: categoryId,
reason: `Création du forum pour la formation ${name}`
});
let role;
try {
logger.debug('Création du rôle Discord...');
role = await guild.roles.create({
name: name,
color: '#FF0000',
reason: `Création du rôle pour la formation ${name}`
});
logger.debug({ roleId: role.id }, 'Rôle Discord créé avec succès');
} catch (error) {
logger.error(error, 'Erreur lors de la création du rôle Discord');
throw new Error('Impossible de créer le rôle Discord pour la formation');
}
const roleResponse = await fetch(`${this.apiUrl}/roles`, {
method: 'POST',
headers,
body: JSON.stringify({
name: role.name,
uuidGuild: guildId,
uuidRole: role.id,
memberCount: "0",
rolePosition: "1",
hoist: role.hoist,
color: role.hexColor
}),
});
if (!roleResponse.ok) {
await role.delete().catch(e => logger.error(e, 'Erreur lors de la suppression du rôle Discord'));
throw new Error('Erreur lors de la création du rôle dans l\'API');
}
const courseData = {
name,
isCertified,
uuidGuild: guildId,
uuidCategory: categoryId,
uuidRole: role.id
};
logger.debug({
courseData,
endpoint: `${this.apiUrl}/courses`
}, 'Tentative de création de formation dans l\'API');
const response = await fetch(`${this.apiUrl}/courses`, {
method: 'POST',
headers,
body: JSON.stringify(courseData),
});
if (!response.ok) {
const errorData = await response.text();
logger.error({
status: response.status,
errorData
}, 'Réponse d\'erreur de l\'API');
throw new Error('Erreur lors de la création de la formation');
}
return await response.json();
} catch (error) {
logger.error(error, 'Erreur lors de la création de la formation');
throw error;
}
}
async deleteCourse(courseId: string): Promise<void> {
try {
const discordChannel = await this.client.channels.fetch(courseId);
if (!discordChannel || !('name' in discordChannel)) {
throw new Error('Course not found');
}
if (!(discordChannel instanceof GuildChannel)) {
throw new Error('Invalid course type');
}
logger.debug({
channelId: courseId,
channelName: discordChannel.name
}, 'Channel Discord trouvé');
const response = await fetch(`${this.apiUrl}/courses`);
if (!response.ok) {
const errorData = await response.text();
logger.error({
status: response.status,
errorData
}, 'Réponse d\'erreur de l\'API');
throw new Error('Erreur lors de la récupération des formations');
}
const { data: courses } = await response.json();
const course = courses.find((c: Course) => c.name === discordChannel.name);
if (!course) {
throw new Error(`Formation "${discordChannel.name}" non trouvée`);
}
logger.debug({
courseName: course.name,
courseUuid: course.uuid,
roles: course.roles
}, 'Formation trouvée avec ses rôles');
// Stocker l'ID du rôle pour plus tard
const roleId = course.roles?.[0]?.uuidRole;
// Supprimer d'abord la formation
logger.debug({
courseUuid: course.uuid,
endpoint: `${this.apiUrl}/courses/${course.uuid}`
}, 'Tentative de suppression de la formation');
const deleteResponse = await fetch(`${this.apiUrl}/courses/${course.uuid}`, {
method: 'DELETE'
});
if (!deleteResponse.ok) {
const errorText = await deleteResponse.text();
logger.error({
status: deleteResponse.status,
response: errorText
}, 'Erreur détaillée de la suppression de la formation');
throw new Error('Erreur lors de la suppression de la formation');
}
logger.info({
courseId: course.uuid,
courseName: course.name
}, 'Formation supprimée avec succès');
// Puis supprimer le rôle si on en avait un
if (roleId) {
logger.debug({
roleId,
endpoint: `${this.apiUrl}/roles/${roleId}`
}, 'Tentative de suppression du rôle');
const deleteRoleResponse = await fetch(`${this.apiUrl}/roles/${roleId}`, {
method: 'DELETE'
});
if (!deleteRoleResponse.ok) {
const errorText = await deleteRoleResponse.text();
logger.error({
status: deleteRoleResponse.status,
response: errorText
}, 'Erreur lors de la suppression du rôle');
throw new Error('Erreur lors de la suppression du rôle');
}
logger.info({
roleId,
roleName: course.roles[0].name
}, 'Rôle supprimé avec succès');
}
} catch (error) {
logger.error({
error,
courseId,
message: error instanceof Error ? error.message : 'Unknown error',
stack: error instanceof Error ? error.stack : undefined
}, 'Erreur lors de la suppression de la formation');
throw error;
}
}
}