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 DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
|
||||||
import { Documents } from "@prisma/client";
|
import { Documents } from "@prisma/client";
|
||||||
import User, { Document } from "le-coffre-resources/dist/SuperAdmin";
|
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";
|
import UsersService from "@Services/super-admin/UsersService/UsersService";
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class EmailBuilder{
|
export default class EmailBuilder {
|
||||||
public constructor(private mailchimpService: MailchimpService ,private documentsService: DocumentsService, protected variables: BackendVariables, private usersService: UsersService){}
|
public constructor(
|
||||||
|
private mailchimpService: MailchimpService,
|
||||||
|
private documentsService: DocumentsService,
|
||||||
|
protected variables: BackendVariables,
|
||||||
|
private usersService: UsersService,
|
||||||
|
) {}
|
||||||
|
|
||||||
public async sendDocumentEmails(documentEntity: Documents){
|
public async sendDocumentEmails(documentEntity: Documents) {
|
||||||
if(documentEntity.document_status !== "ASKED" && documentEntity.document_status !== "REFUSED") return;
|
if (documentEntity.document_status !== "ASKED" && documentEntity.document_status !== "REFUSED") return;
|
||||||
|
|
||||||
let templateName = ETemplates.DOCUMENT_ASKED;
|
let templateName = ETemplates.DOCUMENT_ASKED;
|
||||||
let subject = "Votre notaire vous demande de déposer des pièces pour traiter votre dossier.";
|
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;
|
templateName = ETemplates.DOCUMENT_REFUSED;
|
||||||
subject = "Un ou plusieurs documents ne sont pas validés. Vous avez une nouvelle action à réaliser.";
|
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}} });
|
const documentPrisma = await this.documentsService.getByUid(documentEntity.uid, {
|
||||||
if(!documentPrisma) throw new Error("Document not found");
|
depositor: { include: { contact: true } },
|
||||||
const document = Document.hydrate<Document>(documentPrisma);
|
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
|
//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} });
|
const lastEmail = await this.mailchimpService.get({
|
||||||
if(lastEmail.length > 0) return;
|
where: {
|
||||||
|
to: document.depositor!.contact!.email,
|
||||||
const to = document.depositor!.contact!.email;
|
OR: [{ sentAt: { gte: new Date(Date.now() - 3600000) } }, { sentAt: null }],
|
||||||
const templateVariables = {
|
templateName: templateName,
|
||||||
first_name: document.depositor!.contact!.first_name,
|
},
|
||||||
last_name: document.depositor!.contact!.last_name,
|
});
|
||||||
office_name: document.folder!.office!.name,
|
if (lastEmail.length > 0) return;
|
||||||
link: `${this.variables.APP_HOST}/customer-login`
|
|
||||||
};
|
|
||||||
|
|
||||||
this.mailchimpService.create({
|
const to = document.depositor!.contact!.email;
|
||||||
templateName,
|
const templateVariables = {
|
||||||
to,
|
first_name: document.depositor!.contact!.first_name,
|
||||||
subject,
|
last_name: document.depositor!.contact!.last_name,
|
||||||
templateVariables,
|
office_name: document.folder!.office!.name,
|
||||||
uid: "",
|
link: `${this.variables.APP_HOST}/customer-login`,
|
||||||
from: null,
|
};
|
||||||
cc: [],
|
|
||||||
cci: [],
|
|
||||||
sentAt: null,
|
|
||||||
nbTrySend: null,
|
|
||||||
lastTrySendDate: null,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async sendRecapEmails(){
|
this.mailchimpService.create({
|
||||||
const usersToEmail : User[] = await this.usersService.get({ where: { office_folders: { some:{ documents: { some: { document_status: "DEPOSITED" } } }} }, distinct: ["uid"], include: { contact: true } });
|
templateName,
|
||||||
|
to,
|
||||||
usersToEmail.forEach(user => {
|
subject,
|
||||||
const to = user.contact!.email;
|
templateVariables,
|
||||||
const civility = this.getCivility(user.contact!.civility);
|
uid: "",
|
||||||
|
from: null,
|
||||||
|
cc: [],
|
||||||
|
cci: [],
|
||||||
|
sentAt: null,
|
||||||
|
nbTrySend: null,
|
||||||
|
lastTrySendDate: null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const templateVariables = {
|
public async sendRecapEmails() {
|
||||||
civility: civility,
|
const usersToEmail: User[] = await this.usersService.get({
|
||||||
last_name: user.contact!.last_name,
|
where: { office_folders: { some: { documents: { some: { document_status: "DEPOSITED" } } } } },
|
||||||
link: this.variables.APP_HOST
|
distinct: ["uid"],
|
||||||
};
|
include: { contact: true },
|
||||||
|
});
|
||||||
|
|
||||||
const templateName = ETemplates.DOCUMENT_RECAP;
|
usersToEmail.forEach((user) => {
|
||||||
const subject = "Des clients vous ont envoyé des documents qui n'ont pas été validés.";
|
const to = user.contact!.email;
|
||||||
|
const civility = this.getCivility(user.contact!.civility);
|
||||||
|
|
||||||
this.mailchimpService.create({
|
const templateVariables = {
|
||||||
templateName,
|
civility: civility,
|
||||||
to,
|
last_name: user.contact!.last_name,
|
||||||
subject,
|
link: this.variables.APP_HOST,
|
||||||
templateVariables,
|
};
|
||||||
uid: "",
|
|
||||||
from: null,
|
|
||||||
cc: [],
|
|
||||||
cci: [],
|
|
||||||
sentAt: null,
|
|
||||||
nbTrySend: null,
|
|
||||||
lastTrySendDate: null,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public getCivility(civility: string){
|
const templateName = ETemplates.DOCUMENT_RECAP;
|
||||||
if(civility === "MALE") return "Mr"
|
const subject = "Des clients vous ont envoyé des documents qui n'ont pas été validés.";
|
||||||
else return "Mme"
|
|
||||||
}
|
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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user