diff --git a/src/common/emails/EmailBuilder.ts b/src/common/emails/EmailBuilder.ts index 8c61d608..81eb1c1e 100644 --- a/src/common/emails/EmailBuilder.ts +++ b/src/common/emails/EmailBuilder.ts @@ -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,84 +8,102 @@ 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"){ - templateName = ETemplates.DOCUMENT_REFUSED; - subject = "Un ou plusieurs documents ne sont pas validés. Vous avez une nouvelle action à réaliser."; - } + 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") { + 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 document = Document.hydrate(documentPrisma); + 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(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 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` - }; + //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, + OR: [{ sentAt: { gte: new Date(Date.now() - 3600000) } }, { sentAt: null }], + templateName: templateName, + }, + }); + if (lastEmail.length > 0) return; - this.mailchimpService.create({ - templateName, - to, - subject, - templateVariables, - uid: "", - from: null, - cc: [], - cci: [], - sentAt: null, - nbTrySend: null, - lastTrySendDate: null, - }); - } + 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`, + }; - 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 => { - const to = user.contact!.email; - const civility = this.getCivility(user.contact!.civility); + this.mailchimpService.create({ + templateName, + to, + subject, + templateVariables, + uid: "", + from: null, + cc: [], + cci: [], + sentAt: null, + nbTrySend: null, + lastTrySendDate: null, + }); + } - const templateVariables = { - civility: civility, - last_name: user.contact!.last_name, - link: this.variables.APP_HOST - }; + public async sendRecapEmails() { + const usersToEmail: User[] = await this.usersService.get({ + where: { office_folders: { some: { documents: { some: { document_status: "DEPOSITED" } } } } }, + distinct: ["uid"], + include: { contact: true }, + }); - const templateName = ETemplates.DOCUMENT_RECAP; - const subject = "Des clients vous ont envoyé des documents qui n'ont pas été validés."; + usersToEmail.forEach((user) => { + const to = user.contact!.email; + const civility = this.getCivility(user.contact!.civility); - this.mailchimpService.create({ - templateName, - to, - subject, - templateVariables, - uid: "", - from: null, - cc: [], - cci: [], - sentAt: null, - nbTrySend: null, - lastTrySendDate: null, - }); - }); - } + const templateVariables = { + civility: civility, + last_name: user.contact!.last_name, + link: this.variables.APP_HOST, + }; - public getCivility(civility: string){ - if(civility === "MALE") return "Mr" - else return "Mme" - } + const templateName = ETemplates.DOCUMENT_RECAP; + const subject = "Des clients vous ont envoyé des documents qui n'ont pas été validés."; + + this.mailchimpService.create({ + templateName, + to, + subject, + templateVariables, + uid: "", + from: null, + cc: [], + cci: [], + sentAt: null, + nbTrySend: null, + lastTrySendDate: null, + }); + }); + } + + public getCivility(civility: string) { + if (civility === "MALE") return "Mr"; + else return "Mme"; + } }