Skip to content
Snippets Groups Projects
Commit 6d683b5f authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

feat: add sending of email when structure is validate or not by an admin

parent e0ff4df5
No related branches found
No related tags found
3 merge requests!27Recette,!26Dev,!13feat(admin) : update model + add get endpoint
......@@ -25,5 +25,9 @@ export const config = {
ejs: 'adminStructureClaim.ejs',
json: 'adminStructureClaim.json',
},
structureClaimValidation: {
ejs: 'structureClaimValidation.ejs',
json: 'structureClaimValidation.json',
},
},
};
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.
{
"subject": "Votre demande de rattachement, Réseau des Acteurs de la Médiation Numérique de la Métropole de Lyon"
}
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment