diff --git a/src/configuration/config.ts b/src/configuration/config.ts index 34c5a4a24c2560003e0a91754e1c5cf50b8dcb2b..00bbfff6d98d45181cad4ed3e58d048e1467a3a5 100644 --- a/src/configuration/config.ts +++ b/src/configuration/config.ts @@ -25,5 +25,9 @@ export const config = { ejs: 'adminStructureClaim.ejs', json: 'adminStructureClaim.json', }, + structureClaimValidation: { + ejs: 'structureClaimValidation.ejs', + json: 'structureClaimValidation.json', + }, }, }; diff --git a/src/mailer/mail-templates/structureClaimValidation.ejs b/src/mailer/mail-templates/structureClaimValidation.ejs new file mode 100644 index 0000000000000000000000000000000000000000..eca79ec7fe230a7dd947880ee5795c57cd1647cc --- /dev/null +++ b/src/mailer/mail-templates/structureClaimValidation.ejs @@ -0,0 +1,6 @@ +Bonjour<br /> +<br /> +La demande de rattachement de structure a votre compte a été <strong><%= status %></strong>. +<br /> +<br /> +Ce mail est un mail automatique. Merci de ne pas y répondre. diff --git a/src/mailer/mail-templates/structureClaimValidation.json b/src/mailer/mail-templates/structureClaimValidation.json new file mode 100644 index 0000000000000000000000000000000000000000..bdcb607dc59c9beed42c21a607287be19c06e217 --- /dev/null +++ b/src/mailer/mail-templates/structureClaimValidation.json @@ -0,0 +1,3 @@ +{ + "subject": "Votre demande de rattachement, Réseau des Acteurs de la Médiation Numérique de la Métropole de Lyon" +} diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 649ed4c889ea783c57acba0117029bd1f0630593..c4c4a5a252967c2f76f40cb35cd3f1990cb99b25 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -13,7 +13,7 @@ import { EmailChangeDto } from './dto/change-email.dto'; @Injectable() export class UsersService { - constructor(@InjectModel(User.name) private userModel: Model<IUser>, private mailerService: MailerService) {} + constructor(@InjectModel(User.name) private userModel: Model<IUser>, private readonly mailerService: MailerService) {} /** * Create a user account @@ -126,7 +126,7 @@ export class UsersService { } /** - * Generate activation token and send it to user by email, in order to validate + * Send to all admins validation email for structures * a new account. * @param user User */ @@ -144,6 +144,23 @@ export class UsersService { }); } + /** + * Send approval email for user + * a new account. + * @param user User + */ + private async sendStructureClaimApproval(userEmail: string, status: boolean): Promise<any> { + const config = this.mailerService.config; + const ejsPath = this.mailerService.getTemplateLocation(config.templates.structureClaimValidation.ejs); + const jsonConfig = this.mailerService.loadJsonConfig(config.templates.structureClaimValidation.json); + + const html = await ejs.renderFile(ejsPath, { + config, + status: status ? 'accepté' : 'refusée', + }); + this.mailerService.send(userEmail, jsonConfig.subject, html); + } + /** * Check that the given token is associated to userId. If it's true, validate user account. * @param userId string @@ -309,6 +326,7 @@ export class UsersService { validate: boolean ): Promise<{ userEmail: string; structureId: number }[]> { const users = await this.findOne(userEmail); + let status = false; if (!users) { throw new HttpException('User not found', HttpStatus.NOT_FOUND); } @@ -317,7 +335,10 @@ export class UsersService { // If it's a validation case, push structureId into validated user structures if (validate) { users.structuresLink.push(structureId); + // Send validation email + status = true; } + this.sendStructureClaimApproval(userEmail, status); await users.save(); return this.getPendingStructures(); } else {