217 lines
6.4 KiB
TypeScript
217 lines
6.4 KiB
TypeScript
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
|
|
import { Documents, DocumentsNotary } from "@prisma/client";
|
|
import { 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";
|
|
import User from "le-coffre-resources/dist/SuperAdmin";
|
|
import { Customer, DocumentNotary } from "le-coffre-resources/dist/Notary";
|
|
import DocumentsNotaryService from "@Services/notary/DocumentsNotaryService/DocumentsNotaryService";
|
|
|
|
@Service()
|
|
export default class EmailBuilder {
|
|
public constructor(
|
|
private mailchimpService: MailchimpService,
|
|
private documentsService: DocumentsService,
|
|
protected variables: BackendVariables,
|
|
private usersService: UsersService,
|
|
private documentNotaryService : DocumentsNotaryService,
|
|
) {}
|
|
|
|
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.";
|
|
}
|
|
|
|
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,
|
|
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`,
|
|
};
|
|
|
|
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";
|
|
}
|
|
|
|
public async sendInvitationEmails(emails: string[]) {
|
|
emails.forEach((email) => {
|
|
const to = email;
|
|
|
|
const templateVariables = {
|
|
link: this.variables.APP_HOST,
|
|
idNotLink: this.variables.IDNOT_PROD_BASE_URL,
|
|
};
|
|
|
|
const templateName = ETemplates.SUBSCRIPTION_INVITATION;
|
|
const subject = "Invitation abonnement LeCoffre";
|
|
|
|
this.mailchimpService.create({
|
|
templateName,
|
|
to,
|
|
subject,
|
|
templateVariables,
|
|
uid: "",
|
|
from: null,
|
|
cc: [],
|
|
cci: [],
|
|
sentAt: null,
|
|
nbTrySend: null,
|
|
lastTrySendDate: null,
|
|
});
|
|
// this.mailchimpService.sendEmails();
|
|
});
|
|
|
|
|
|
}
|
|
|
|
public async sendReminder(customer: Customer, documents: Documents[]) {
|
|
const to = customer.contact!.email;
|
|
const templateVariables = {
|
|
office_name: customer.office?.name,
|
|
last_name: customer.contact!.last_name || "",
|
|
first_name: customer.contact!.first_name || "",
|
|
link: this.variables.APP_HOST,
|
|
};
|
|
|
|
const templateName = ETemplates.DOCUMENT_REMINDER;
|
|
const subject = "Vous avez des documents à déposer pour votre dossier.";
|
|
|
|
this.mailchimpService.create({
|
|
templateName,
|
|
to,
|
|
subject,
|
|
templateVariables,
|
|
uid: "",
|
|
from: null,
|
|
cc: [],
|
|
cci: [],
|
|
sentAt: null,
|
|
nbTrySend: null,
|
|
lastTrySendDate: null,
|
|
});
|
|
}
|
|
|
|
public async sendDocumentNotaryEmails(documentEntity: DocumentsNotary) {
|
|
const templateName = ETemplates.DOCUMENT_SEND;
|
|
const subject = "Votre notaire vous a envoyé des documents.";
|
|
|
|
const documentPrisma = await this.documentNotaryService.getByUid(documentEntity.uid, {
|
|
depositor: { include: { contact: true } },
|
|
folder: { include: { office: true } },
|
|
customer: { include: { contact: true } },
|
|
});
|
|
if (!documentPrisma) throw new Error("Document not found");
|
|
const document = DocumentNotary.hydrate<DocumentNotary>(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.customer?.contact?.email,
|
|
OR: [{ sentAt: { gte: new Date(Date.now() - 3600000) } }, { sentAt: null }],
|
|
templateName: templateName,
|
|
},
|
|
});
|
|
if (lastEmail.length > 0) return;
|
|
|
|
const to = document.customer!.contact!.email;
|
|
const templateVariables = {
|
|
first_name: document.customer?.contact?.first_name || "",
|
|
last_name: document.customer?.contact?.last_name || "",
|
|
office_name: document.folder!.office!.name,
|
|
link: `${this.variables.APP_HOST}/customer-login`,
|
|
};
|
|
|
|
this.mailchimpService.create({
|
|
templateName,
|
|
to,
|
|
subject,
|
|
templateVariables,
|
|
uid: "",
|
|
from: null,
|
|
cc: [],
|
|
cci: [],
|
|
sentAt: null,
|
|
nbTrySend: null,
|
|
lastTrySendDate: null,
|
|
});
|
|
}
|
|
}
|