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

fix: update mailing templates, user model wrong type and aptic structure...

fix: update mailing templates, user model wrong type and aptic structure handling when a structure is updated.
parent eb8b2433
No related branches found
No related tags found
2 merge requests!27Recette,!26Dev
......@@ -33,5 +33,9 @@ export const config = {
ejs: 'structureOutdatedInfo.ejs',
json: 'structureOutdatedInfo.json',
},
apticStructureDuplication: {
ejs: 'apticStructureDuplication.ejs',
json: 'apticStructureDuplication.json',
},
},
};
Bonjour,<br />
<br />
La fiche structure: <strong><%= name %></strong> a été créée après récupération des données aptic. Elle correspond
potientiellement a la structure existante : <strong><%= duplicatedStructureName %></strong>.
<br />
<br />
Cordialement,
<br />
<br />
L'équipe RES'in
<br />
<br />
Ce mail est un mail automatique. Merci de ne pas y répondre.
{
"subject": "Doublon Aptic"
}
Bonjour<br />
<br />
La fiche de votre structure: <strong><%= name %></strong> n'a pas été mise a jour depuis 6mois.
Vous recevez ce message, parce que votre structure <strong><%= name %></strong> est référencée sur RES'in, le réseau des
acteurs de l'inclusion numérique de la Métropole de Lyon. Pouvez-vous nous aider en vérifiant que vos données sont bien
à jour en
<a href="<%= config.protocol %>://<%= config.host %><%= config.port ? ':' + config.port : '' %>/home?id=<%= id %>"
>cliquant ici</a
>.
<br />
Cordialement,
<br />
L'équipe RES'in
<br />
<br />
Ce mail est un mail automatique. Merci de ne pas y répondre.
......@@ -47,7 +47,7 @@ export class MailerService {
})
.subscribe(
(body) => {
Logger.log(`Send mail : ${subject} success`, 'Mailer');
Logger.log(`Send mail - success : ${subject}`, 'Mailer');
return resolve(body);
},
(err) => {
......
......@@ -10,11 +10,13 @@ import { Model } from 'mongoose';
import { Structure, StructureDocument } from '../schemas/structure.schema';
import { ApticStructure } from '../schemas/aptic-structure.schema';
import { Address } from '../schemas/address.schema';
import { UsersService } from '../../users/users.service';
@Injectable()
export class ApticStructuresService {
constructor(
private readonly httpService: HttpService,
private readonly userService: UsersService,
@InjectModel(Structure.name) private structureModel: Model<StructureDocument>
) {}
......@@ -52,11 +54,10 @@ export class ApticStructuresService {
createdStructure.contactPhone = structure.presence_phone;
createdStructure.labelsQualifications = ['passNumerique'];
// Address
const address = new Address();
address.street = structure.presence_address;
address.commune = structure.city;
createdStructure.address = address;
createdStructure.address = this.formatAddress(structure);
createdStructure.save();
// Send admin weird structure mail
this.verifyDuplication(createdStructure);
}
});
}
......@@ -126,4 +127,33 @@ export class ApticStructuresService {
},
});
}
private async verifyDuplication(createdStructure: StructureDocument): Promise<void> {
const sameAddrStructure = await this.structureModel
.findOne({
_id: { $ne: createdStructure._id },
address: createdStructure.address,
})
.exec();
if (sameAddrStructure) {
this.userService.sendAdminApticStructureMail(createdStructure.structureName, sameAddrStructure.structureName);
}
}
/**
* Format aptic structure address
*/
private formatAddress(structure: ApticStructure): Address {
const address = new Address();
const regexWithSpace = /\d+\s/g; // NOSONAR
const regex = /\d+/g; // NOSONAR
if (structure.presence_address.match(regex)) {
address.numero = structure.presence_address.match(regex)[0];
address.street = structure.presence_address.replace(regexWithSpace, '');
} else {
address.street = structure.presence_address;
}
address.commune = structure.city;
return address;
}
}
......@@ -102,6 +102,8 @@ export class StructuresService {
const result = await this.structureModel.findByIdAndUpdate(Types.ObjectId(idStructure), structure).exec();
if (!result) {
throw new HttpException('Invalid structure id', HttpStatus.BAD_REQUEST);
} else {
this.userService.removeOutdatedStructureFromArray(idStructure);
}
return this.findOne(idStructure);
}
......@@ -269,7 +271,7 @@ export class StructuresService {
if (user.structureOutdatedMailSent.includes(data.structure._id)) {
return;
} else {
this.sendOutdatedEmailToUser(data.owner.email, data.structure.structureName);
this.sendOutdatedEmailToUser(data.owner.email, data.structure.structureName, data.structure._id);
user.structureOutdatedMailSent.push(data.structure._id);
user.save();
}
......@@ -282,7 +284,7 @@ export class StructuresService {
* a new account.
* @param user User
*/
private async sendOutdatedEmailToUser(userEmail: string, structureName: string): Promise<any> {
private async sendOutdatedEmailToUser(userEmail: string, structureName: string, id: string): Promise<any> {
const config = this.mailerService.config;
const ejsPath = this.mailerService.getTemplateLocation(config.templates.structureOutdatedInfo.ejs);
const jsonConfig = this.mailerService.loadJsonConfig(config.templates.structureOutdatedInfo.json);
......@@ -290,6 +292,7 @@ export class StructuresService {
const html = await ejs.renderFile(ejsPath, {
config,
name: structureName,
id: id,
});
this.mailerService.send(userEmail, jsonConfig.subject, html);
}
......
import { Document } from 'mongoose';
import { Document, Types } from 'mongoose';
export interface IUser extends Document {
readonly _id: string;
......@@ -15,5 +15,5 @@ export interface IUser extends Document {
newEmail: string;
structuresLink: string[];
pendingStructuresLink: string[];
structureOutdatedMailSent: string[];
structureOutdatedMailSent: Types.ObjectId[];
}
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Types } from 'mongoose';
import { UserRole } from '../enum/user-role.enum';
@Schema()
export class User {
......@@ -42,7 +43,7 @@ export class User {
pendingStructuresLink: string[];
@Prop({ default: null })
structureOutdatedMailSent: string[];
structureOutdatedMailSent: Types.ObjectId[];
}
export const UserSchema = SchemaFactory.createForClass(User);
......@@ -3,7 +3,7 @@ import { InjectModel } from '@nestjs/mongoose';
import * as bcrypt from 'bcrypt';
import * as ejs from 'ejs';
import * as crypto from 'crypto';
import { Model } from 'mongoose';
import { Model, Types } from 'mongoose';
import { LoginDto } from '../auth/login-dto';
import { CreateUserDto } from './dto/create-user.dto';
import { User } from './schemas/user.schema';
......@@ -128,7 +128,6 @@ export class UsersService {
/**
* Send to all admins validation email for structures
* a new account.
* @param user User
*/
private async sendAdminStructureValidationMail(): Promise<any> {
const config = this.mailerService.config;
......@@ -144,6 +143,25 @@ export class UsersService {
});
}
/**
* Send to all admins mail for aptic duplicated data
*/
public async sendAdminApticStructureMail(structureName: string, duplicatedStructureName: string): Promise<any> {
const config = this.mailerService.config;
const ejsPath = this.mailerService.getTemplateLocation(config.templates.apticStructureDuplication.ejs);
const jsonConfig = this.mailerService.loadJsonConfig(config.templates.apticStructureDuplication.json);
const html = await ejs.renderFile(ejsPath, {
config,
name: structureName,
duplicatedStructureName: duplicatedStructureName,
});
const admins = await this.getAdmins();
admins.forEach((admin) => {
this.mailerService.send(admin.email, jsonConfig.subject, html);
});
}
/**
* Send approval email for user
* a new account.
......@@ -371,4 +389,14 @@ export class UsersService {
);
}
}
public async removeOutdatedStructureFromArray(structureId: string): Promise<void> {
const users = await this.userModel.find({ structureOutdatedMailSent: Types.ObjectId(structureId) }).exec();
users.forEach((user) => {
user.structureOutdatedMailSent = user.structureOutdatedMailSent.filter((item) =>
Types.ObjectId(structureId).equals(item)
);
user.save();
});
}
}
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