fix email spams
This commit is contained in:
parent
a20f17dc0b
commit
974d1225ee
@ -1,4 +1,3 @@
|
||||
|
||||
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
|
||||
import { Documents } from "@prisma/client";
|
||||
import User, { Document } from "le-coffre-resources/dist/SuperAdmin";
|
||||
@ -9,33 +8,47 @@ import { BackendVariables } from "@Common/config/variables/Variables";
|
||||
import UsersService from "@Services/super-admin/UsersService/UsersService";
|
||||
|
||||
@Service()
|
||||
export default class EmailBuilder{
|
||||
public constructor(private mailchimpService: MailchimpService ,private documentsService: DocumentsService, protected variables: BackendVariables, private usersService: UsersService){}
|
||||
export default class EmailBuilder {
|
||||
public constructor(
|
||||
private mailchimpService: MailchimpService,
|
||||
private documentsService: DocumentsService,
|
||||
protected variables: BackendVariables,
|
||||
private usersService: UsersService,
|
||||
) {}
|
||||
|
||||
public async sendDocumentEmails(documentEntity: Documents){
|
||||
if(documentEntity.document_status !== "ASKED" && documentEntity.document_status !== "REFUSED") return;
|
||||
public async sendDocumentEmails(documentEntity: Documents) {
|
||||
if (documentEntity.document_status !== "ASKED" && documentEntity.document_status !== "REFUSED") return;
|
||||
|
||||
let templateName = ETemplates.DOCUMENT_ASKED;
|
||||
let subject = "Votre notaire vous demande de déposer des pièces pour traiter votre dossier.";
|
||||
if(documentEntity.document_status === "REFUSED"){
|
||||
if (documentEntity.document_status === "REFUSED") {
|
||||
templateName = ETemplates.DOCUMENT_REFUSED;
|
||||
subject = "Un ou plusieurs documents ne sont pas validés. Vous avez une nouvelle action à réaliser.";
|
||||
}
|
||||
|
||||
const documentPrisma = await this.documentsService.getByUid(documentEntity.uid, { depositor: {include: {contact: true}}, folder:{include:{ office: true}} });
|
||||
if(!documentPrisma) throw new Error("Document not found");
|
||||
const documentPrisma = await this.documentsService.getByUid(documentEntity.uid, {
|
||||
depositor: { include: { contact: true } },
|
||||
folder: { include: { office: true } },
|
||||
});
|
||||
if (!documentPrisma) throw new Error("Document not found");
|
||||
const document = Document.hydrate<Document>(documentPrisma);
|
||||
|
||||
//Use mailchimpService.get get if an email was sent to the user in the lst hour
|
||||
const lastEmail = await this.mailchimpService.get({ where: { to: document.depositor!.contact!.email, sentAt: { gte: new Date(Date.now() - 3600000) }, templateName: templateName} });
|
||||
if(lastEmail.length > 0) return;
|
||||
const lastEmail = await this.mailchimpService.get({
|
||||
where: {
|
||||
to: document.depositor!.contact!.email,
|
||||
OR: [{ sentAt: { gte: new Date(Date.now() - 3600000) } }, { sentAt: null }],
|
||||
templateName: templateName,
|
||||
},
|
||||
});
|
||||
if (lastEmail.length > 0) return;
|
||||
|
||||
const to = document.depositor!.contact!.email;
|
||||
const templateVariables = {
|
||||
first_name: document.depositor!.contact!.first_name,
|
||||
last_name: document.depositor!.contact!.last_name,
|
||||
office_name: document.folder!.office!.name,
|
||||
link: `${this.variables.APP_HOST}/customer-login`
|
||||
link: `${this.variables.APP_HOST}/customer-login`,
|
||||
};
|
||||
|
||||
this.mailchimpService.create({
|
||||
@ -53,17 +66,21 @@ export default class EmailBuilder{
|
||||
});
|
||||
}
|
||||
|
||||
public async sendRecapEmails(){
|
||||
const usersToEmail : User[] = await this.usersService.get({ where: { office_folders: { some:{ documents: { some: { document_status: "DEPOSITED" } } }} }, distinct: ["uid"], include: { contact: true } });
|
||||
public async sendRecapEmails() {
|
||||
const usersToEmail: User[] = await this.usersService.get({
|
||||
where: { office_folders: { some: { documents: { some: { document_status: "DEPOSITED" } } } } },
|
||||
distinct: ["uid"],
|
||||
include: { contact: true },
|
||||
});
|
||||
|
||||
usersToEmail.forEach(user => {
|
||||
usersToEmail.forEach((user) => {
|
||||
const to = user.contact!.email;
|
||||
const civility = this.getCivility(user.contact!.civility);
|
||||
|
||||
const templateVariables = {
|
||||
civility: civility,
|
||||
last_name: user.contact!.last_name,
|
||||
link: this.variables.APP_HOST
|
||||
link: this.variables.APP_HOST,
|
||||
};
|
||||
|
||||
const templateName = ETemplates.DOCUMENT_RECAP;
|
||||
@ -85,8 +102,8 @@ export default class EmailBuilder{
|
||||
});
|
||||
}
|
||||
|
||||
public getCivility(civility: string){
|
||||
if(civility === "MALE") return "Mr"
|
||||
else return "Mme"
|
||||
public getCivility(civility: string) {
|
||||
if (civility === "MALE") return "Mr";
|
||||
else return "Mme";
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user