93 lines
3.5 KiB
TypeScript
93 lines
3.5 KiB
TypeScript
|
|
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
|
|
import { Documents } from "@prisma/client";
|
|
import User, { Document } from "le-coffre-resources/dist/SuperAdmin";
|
|
import { Service } from "typedi";
|
|
import { ETemplates } from "./Templates/EmailTemplates";
|
|
import MailchimpService from "@Services/common/MailchimpService/MailchimpService";
|
|
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){}
|
|
|
|
public async sendDocumentEmails(documentEntity: Documents){
|
|
if(documentEntity.document_status !== "ASKED" && documentEntity.document_status !== "REFUSED") return;
|
|
|
|
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) } } });
|
|
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`
|
|
};
|
|
|
|
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.";
|
|
}
|
|
|
|
this.mailchimpService.create({
|
|
templateName,
|
|
to,
|
|
subject,
|
|
templateVariables,
|
|
uid: "",
|
|
from: null,
|
|
cc: [],
|
|
cci: [],
|
|
sentAt: null,
|
|
nbTrySend: null,
|
|
lastTrySendDate: null,
|
|
});
|
|
}
|
|
|
|
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);
|
|
|
|
const templateVariables = {
|
|
civility: civility,
|
|
last_name: user.contact!.last_name,
|
|
link: this.variables.APP_HOST
|
|
};
|
|
|
|
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"
|
|
}
|
|
}
|