Email on document sent by notary
This commit is contained in:
parent
71c0a29c61
commit
ffce128c83
@ -1,4 +1,5 @@
|
|||||||
import authHandler from "@App/middlewares/AuthHandler";
|
import authHandler from "@App/middlewares/AuthHandler";
|
||||||
|
import EmailBuilder from "@Common/emails/EmailBuilder";
|
||||||
import ApiController from "@Common/system/controller-pattern/ApiController";
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
||||||
import { Controller, Delete, Get, Post } from "@ControllerPattern/index";
|
import { Controller, Delete, Get, Post } from "@ControllerPattern/index";
|
||||||
import { DocumentsNotary, Prisma } from "@prisma/client";
|
import { DocumentsNotary, Prisma } from "@prisma/client";
|
||||||
@ -20,6 +21,7 @@ export default class DocumentsNotaryController extends ApiController {
|
|||||||
private customerService: CustomersService,
|
private customerService: CustomersService,
|
||||||
private userService: UsersService,
|
private userService: UsersService,
|
||||||
private filesNotaryService: FilesNotaryService,
|
private filesNotaryService: FilesNotaryService,
|
||||||
|
private emailBuilder: EmailBuilder,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@ -92,6 +94,9 @@ export default class DocumentsNotaryController extends ApiController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const documentNotary = await this.documentsNotaryService.getByUid(documentNotaryEntityCreated.uid);
|
const documentNotary = await this.documentsNotaryService.getByUid(documentNotaryEntityCreated.uid);
|
||||||
|
if(!documentNotary) return;
|
||||||
|
|
||||||
|
await this.emailBuilder.sendDocumentNotaryEmails(documentNotary);
|
||||||
|
|
||||||
const document = DocumentNotary.hydrate<DocumentNotary>(documentNotary!);
|
const document = DocumentNotary.hydrate<DocumentNotary>(documentNotary!);
|
||||||
//success
|
//success
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
|
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
|
||||||
import { Documents } from "@prisma/client";
|
import { Documents, DocumentsNotary } from "@prisma/client";
|
||||||
import { Document } from "le-coffre-resources/dist/SuperAdmin";
|
import { Document } from "le-coffre-resources/dist/SuperAdmin";
|
||||||
import { Service } from "typedi";
|
import { Service } from "typedi";
|
||||||
import { ETemplates } from "./Templates/EmailTemplates";
|
import { ETemplates } from "./Templates/EmailTemplates";
|
||||||
@ -7,7 +7,8 @@ import MailchimpService from "@Services/common/MailchimpService/MailchimpService
|
|||||||
import { BackendVariables } from "@Common/config/variables/Variables";
|
import { BackendVariables } from "@Common/config/variables/Variables";
|
||||||
import UsersService from "@Services/super-admin/UsersService/UsersService";
|
import UsersService from "@Services/super-admin/UsersService/UsersService";
|
||||||
import User from "le-coffre-resources/dist/SuperAdmin";
|
import User from "le-coffre-resources/dist/SuperAdmin";
|
||||||
import { Customer } from "le-coffre-resources/dist/Notary";
|
import { Customer, DocumentNotary } from "le-coffre-resources/dist/Notary";
|
||||||
|
import DocumentsNotaryService from "@Services/notary/DocumentsNotaryService/DocumentsNotaryService";
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class EmailBuilder {
|
export default class EmailBuilder {
|
||||||
@ -16,6 +17,7 @@ export default class EmailBuilder {
|
|||||||
private documentsService: DocumentsService,
|
private documentsService: DocumentsService,
|
||||||
protected variables: BackendVariables,
|
protected variables: BackendVariables,
|
||||||
private usersService: UsersService,
|
private usersService: UsersService,
|
||||||
|
private documentNotaryService : DocumentsNotaryService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async sendDocumentEmails(documentEntity: Documents) {
|
public async sendDocumentEmails(documentEntity: Documents) {
|
||||||
@ -166,4 +168,49 @@ export default class EmailBuilder {
|
|||||||
lastTrySendDate: 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,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,4 +4,5 @@ export const ETemplates = {
|
|||||||
DOCUMENT_RECAP: "DOCUMENT_RECAP",
|
DOCUMENT_RECAP: "DOCUMENT_RECAP",
|
||||||
SUBSCRIPTION_INVITATION: "SUBSCRIPTION_INVITATION",
|
SUBSCRIPTION_INVITATION: "SUBSCRIPTION_INVITATION",
|
||||||
DOCUMENT_REMINDER: "DOCUMENT_REMINDER",
|
DOCUMENT_REMINDER: "DOCUMENT_REMINDER",
|
||||||
|
DOCUMENT_SEND: "DOCUMENT_SEND",
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user