diff --git a/src/configuration/config.ts b/src/configuration/config.ts index 3d6969e35463dda86086db80ee2bd8f9c762132a..0506dc2ae8a5b211ad5883852d50858f71e84fda 100644 --- a/src/configuration/config.ts +++ b/src/configuration/config.ts @@ -49,5 +49,9 @@ export const config = { ejs: 'adminStructureCreate.ejs', json: 'adminStructureCreate.json', }, + structureErrorReport: { + ejs: 'structureErrorReport.ejs', + json: 'structureErrorReport.json', + }, }, }; diff --git a/src/mailer/mail-templates/structureErrorReport.ejs b/src/mailer/mail-templates/structureErrorReport.ejs new file mode 100644 index 0000000000000000000000000000000000000000..829029e95f157b6034c60889e7ee0798f2518d48 --- /dev/null +++ b/src/mailer/mail-templates/structureErrorReport.ejs @@ -0,0 +1,11 @@ +Bonjour<br /> +<br /> +Un utilisateur de Res'in a relevé une erreur sur la fiche de votre structure (<%= structureName %>). +<br /> +Voici le message:<br /> +<br /> +<strong><%= content %></strong><br /> +<br /> +<a href="<%= config.protocol %>://<%= config.host %><%= config.port ? ':' + config.port : '' %>/acteurs?id=<%= id %>" + >Acceder à votre structure</a +>. diff --git a/src/mailer/mail-templates/structureErrorReport.json b/src/mailer/mail-templates/structureErrorReport.json new file mode 100644 index 0000000000000000000000000000000000000000..3faeacf33799d9891558bedba0a2771ef7344782 --- /dev/null +++ b/src/mailer/mail-templates/structureErrorReport.json @@ -0,0 +1,3 @@ +{ + "subject": "Une erreur a été remontée sur votre structure, Réseau des Acteurs de la Médiation Numérique de la Métropole de Lyon" +} diff --git a/src/mailer/mailer.service.ts b/src/mailer/mailer.service.ts index 0650bc48c0255866d15f86ed725254b665ad01bf..c19d4b2b2830a1ba83c34669ab195df78c90a9ff 100644 --- a/src/mailer/mailer.service.ts +++ b/src/mailer/mailer.service.ts @@ -21,15 +21,15 @@ export class MailerService { * @param {string} html * @param {string} text */ - public async send(to: string, subject: string, html: string): Promise<AxiosResponse<any>> { + public async send(to: string | { email: string }[], subject: string, html: string): Promise<AxiosResponse<any>> { + const emailsToSend = typeof to === 'string' ? [{ email: to }] : to; const formData = new FormData(); - const data = JSON.stringify({ // eslint-disable-next-line camelcase from_email: this.config.from, // eslint-disable-next-line camelcase from_name: this.config.from_name, - to: [{ email: to }], + to: emailsToSend, reply_to: 'inclusionnumerique@grandlyon.com', subject: subject, content: this.addSignature(html), diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 5f1f1fec7da52bd97fd2e3f4c0091186d2123c01..cf86a13399bb94d0a90dabb9ee2355b6ee7e308d 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -316,7 +316,7 @@ export class StructuresService { return this.userService.getStructureOwners(structureId); } - public async updateAccountVerified(idStructure: string, emailUser: string): Promise<Structure> { + public async updateAccountVerified(idStructure: string): Promise<Structure> { const structureLinked = await this.findOne(idStructure); const structure = new this.structureModel(structureLinked); if (!structure) { @@ -338,4 +338,31 @@ export class StructuresService { const owners = await this.userService.getStructureOwnersMails(idStructure, emailUser); return { structure: structure, owners: owners }; } + + public async reportStructureError(structureId: string, content: string) { + const structure = await this.findOne(structureId); + if (!structure) { + throw new HttpException('Invalid structure id', HttpStatus.BAD_REQUEST); + } + const owners = await this.userService.getStructureOwnersMails(structure._id, ''); + const admins = await this.userService.getAdmins(); + const emails = owners.map((owner) => owner.email).concat(admins.map((admin) => admin.email)); + const uniqueEmails = [...new Set(emails)]; + + const emailsObject: { email: string }[] = uniqueEmails.map((item) => { + return { email: item }; + }); + + const config = this.mailerService.config; + const ejsPath = this.mailerService.getTemplateLocation(config.templates.structureErrorReport.ejs); + const jsonConfig = this.mailerService.loadJsonConfig(config.templates.structureErrorReport.json); + + const html = await ejs.renderFile(ejsPath, { + config, + content: content, + id: structure._id, + structureName: structure.structureName, + }); + this.mailerService.send(emailsObject, jsonConfig.subject, html); + } } diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index b25e60d5e1d9427801066aaad25d787e85a4e62f..438107c4edc061bafec9143b69ab2e0bc1b5b421 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -63,11 +63,8 @@ export class StructuresController { } @Put('updateAfterOwnerVerify/:id') - public async updateAfterOwnerVerify( - @Param('id') id: string, - @Body() body: { emailUser: string } - ): Promise<Structure> { - return this.structureService.updateAccountVerified(id, body.emailUser); + public async updateAfterOwnerVerify(@Param('id') id: string): Promise<Structure> { + return this.structureService.updateAccountVerified(id); } @Put(':id') @@ -235,4 +232,9 @@ export class StructuresController { } this.userService.removeFromStructureLinked(userFromDb.email, id); } + + @Post('reportStructureError') + public async reportStructureError(@Body() data: { structureId: string; content: string }): Promise<void> { + return await this.structureService.reportStructureError(data.structureId, data.content); + } }