diff --git a/src/configuration/config.ts b/src/configuration/config.ts index 0506dc2ae8a5b211ad5883852d50858f71e84fda..99476992ceb8facc52d8e4abc96a2ae34000efa9 100644 --- a/src/configuration/config.ts +++ b/src/configuration/config.ts @@ -53,5 +53,13 @@ export const config = { ejs: 'structureErrorReport.ejs', json: 'structureErrorReport.json', }, + structureModificationNotification: { + ejs: 'structureModificationNotification.ejs', + json: 'structureModificationNotification.json', + }, + structureDeletionNotification: { + ejs: 'structureDeletionNotification.ejs', + json: 'structureDeletionNotification.json', + }, }, }; diff --git a/src/mailer/mail-templates/adminStructureCreate.ejs b/src/mailer/mail-templates/adminStructureCreate.ejs index 47b79daa15b999739415094be15f42e9f641ede0..6b74900d4c330fc84bfb73077490b939a60980e6 100644 --- a/src/mailer/mail-templates/adminStructureCreate.ejs +++ b/src/mailer/mail-templates/adminStructureCreate.ejs @@ -2,7 +2,7 @@ Bonjour<br /> <br /> Une nouvelle structure a été créé: <a href="<%= config.protocol %>://<%= config.host %><%= config.port ? ':' + config.port : '' %>/acteurs?id=<%= id %>" - ><strong><%= name %></strong></a + ><strong><%= structureName %></strong></a > <br /> Il est possible que la structure ne soit pas immédiatement visible sur la carto. L'utilisateur doit valider son compte diff --git a/src/mailer/mail-templates/structureDeletionNotification.ejs b/src/mailer/mail-templates/structureDeletionNotification.ejs new file mode 100644 index 0000000000000000000000000000000000000000..73a0954709dd76bc30331bf7fdcf538b9dc57568 --- /dev/null +++ b/src/mailer/mail-templates/structureDeletionNotification.ejs @@ -0,0 +1,3 @@ +Bonjour<br /> +<br /> +Un utilisateur a supprimé la fiche de sa structure (<%= structureName %>). diff --git a/src/mailer/mail-templates/structureDeletionNotification.json b/src/mailer/mail-templates/structureDeletionNotification.json new file mode 100644 index 0000000000000000000000000000000000000000..3fabb0edbd05f73e363f5f035dafd55614d4a33f --- /dev/null +++ b/src/mailer/mail-templates/structureDeletionNotification.json @@ -0,0 +1,3 @@ +{ + "subject": "Une structure à été supprimé de Res'in, Réseau des Acteurs de la Médiation Numérique de la Métropole de Lyon" +} diff --git a/src/mailer/mail-templates/structureModificationNotification.ejs b/src/mailer/mail-templates/structureModificationNotification.ejs new file mode 100644 index 0000000000000000000000000000000000000000..6e8631bf1aedae85148ccb35a96dd48de8f35f59 --- /dev/null +++ b/src/mailer/mail-templates/structureModificationNotification.ejs @@ -0,0 +1,7 @@ +Bonjour<br /> +<br /> +Un utilisateur a modifié une ou plusieurs informations sur la fiche de sa structure (<%= structureName %>). +<br /> +<a href="<%= config.protocol %>://<%= config.host %><%= config.port ? ':' + config.port : '' %>/acteurs?id=<%= id %>" + >Acceder à cette structure</a +>. diff --git a/src/mailer/mail-templates/structureModificationNotification.json b/src/mailer/mail-templates/structureModificationNotification.json new file mode 100644 index 0000000000000000000000000000000000000000..516a30844c6f178ee77134da581d1b44786b6548 --- /dev/null +++ b/src/mailer/mail-templates/structureModificationNotification.json @@ -0,0 +1,3 @@ +{ + "subject": "Une fiche de structure à été mise à jour, Réseau des Acteurs de la Médiation Numérique de la Métropole de Lyon" +} diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 71b6bb189b6aef32dcf401e1bfcdcebe08febdb9..a1c20918b2f8acff192606aeac979c5c2c9a13c5 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -41,7 +41,11 @@ export class StructuresService { user.save(); // Senc admin notification mail - this.userService.sendAdminNewStructureMail(createdStructure.structureName, createdStructure._id); + this.sendAdminStructureNotification( + createdStructure, + this.mailerService.config.templates.adminStructureCreate.ejs, + this.mailerService.config.templates.adminStructureCreate.json + ); return createdStructure; } @@ -111,6 +115,11 @@ export class StructuresService { if (!result) { throw new HttpException('Invalid structure id', HttpStatus.BAD_REQUEST); } else { + this.sendAdminStructureNotification( + result, + this.mailerService.config.templates.structureModificationNotification.ejs, + this.mailerService.config.templates.structureModificationNotification.json + ); this.userService.removeOutdatedStructureFromArray(idStructure); } return this.findOne(idStructure); @@ -233,9 +242,37 @@ export class StructuresService { this.anonymizeStructure(structure).save(); // Remove structure from userModel this.userService.removeStructureIdFromUsers(structure._id); + this.sendAdminStructureNotification( + structure, + this.mailerService.config.templates.structureDeletionNotification.ejs, + this.mailerService.config.templates.structureDeletionNotification.json + ); return structure; } + public async sendAdminStructureNotification( + structure: StructureDocument, + templateLocation: any, + jsonConfigLocation: any + ) { + const uniqueAdminEmails = [...new Set((await this.userService.getAdmins()).map((admin) => admin.email))].map( + (item) => { + return { email: item }; + } + ); + + const config = this.mailerService.config; + const ejsPath = this.mailerService.getTemplateLocation(templateLocation); + const jsonConfig = this.mailerService.loadJsonConfig(jsonConfigLocation); + + const html = await ejs.renderFile(ejsPath, { + config, + id: structure ? structure._id : 0, + structureName: structure ? structure.structureName : '', + }); + this.mailerService.send(uniqueAdminEmails, jsonConfig.subject, html); + } + private anonymizeStructure(structure: StructureDocument): StructureDocument { structure.contactPhone = ''; structure.contactMail = ''; diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 46433a721d056558c711c268c0521fe622901523..6f513374851bfd2b405d920bf55e78b56cc5bd5e 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -9,12 +9,14 @@ import { PasswordResetDto } from './dto/reset-password.dto'; import { UsersService } from './users.service'; import { StructuresService } from '../structures/services/structures.service'; import { TempUserService } from '../temp-user/temp-user.service'; +import { ConfigurationService } from '../configuration/configuration.service'; @Controller('users') export class UsersController { constructor( private usersService: UsersService, private structureService: StructuresService, - private tempUserService: TempUserService + private tempUserService: TempUserService, + private configurationService: ConfigurationService ) {} @UseGuards(JwtAuthGuard) @@ -39,6 +41,11 @@ export class UsersController { const user = await this.usersService.create(createUserDto); if (structureId) { this.usersService.updateStructureLinkedClaim(createUserDto.email, structureId); + this.structureService.sendAdminStructureNotification( + null, + this.configurationService.config.templates.adminStructureClaim.ejs, + this.configurationService.config.templates.adminStructureClaim.json + ); } // Remove temp user if exist const tempUser = await this.tempUserService.findOne(createUserDto.email); diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 00cb1129c062731347632cc2c3b64359a63e9839..b329eb71fe594731e7b8c0deb6a8825ecdec08de 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -138,43 +138,6 @@ export class UsersService { return user; } - /** - * Send to all admins validation email for structures - * new account. - */ - private async sendAdminStructureValidationMail(): Promise<any> { - const config = this.mailerService.config; - const ejsPath = this.mailerService.getTemplateLocation(config.templates.adminStructureClaim.ejs); - const jsonConfig = this.mailerService.loadJsonConfig(config.templates.adminStructureClaim.json); - - const html = await ejs.renderFile(ejsPath, { - config, - }); - const admins = await this.getAdmins(); - admins.forEach((admin) => { - this.mailerService.send(admin.email, jsonConfig.subject, html); - }); - } - - /** - * Send to all admins notification email for new structures - */ - public async sendAdminNewStructureMail(structureName: string, structureId: string): Promise<any> { - const config = this.mailerService.config; - const ejsPath = this.mailerService.getTemplateLocation(config.templates.adminStructureCreate.ejs); - const jsonConfig = this.mailerService.loadJsonConfig(config.templates.adminStructureCreate.json); - - const html = await ejs.renderFile(ejsPath, { - config, - name: structureName, - id: structureId, - }); - const admins = await this.getAdmins(); - admins.forEach((admin) => { - this.mailerService.send(admin.email, jsonConfig.subject, html); - }); - } - /** * Send to all admins mail for aptic duplicated data */ @@ -352,7 +315,6 @@ export class UsersService { public async updateStructureLinkedClaim(userEmail: string, idStructure: string): Promise<Types.ObjectId[]> { const stucturesLinked = this.updatePendingStructureLinked(userEmail, idStructure); - this.sendAdminStructureValidationMail(); return stucturesLinked; }