chore: send email to document owner (#1031)

**Description:**

This PR sends an email to the document owner once the signing has been
completed
This commit is contained in:
Lucas Smith
2024-03-16 22:29:28 +11:00
committed by GitHub

View File

@ -24,6 +24,13 @@ export const sendCompletedEmail = async ({ documentId, requestMetadata }: SendDo
include: { include: {
documentData: true, documentData: true,
Recipient: true, Recipient: true,
User: true,
team: {
select: {
id: true,
url: true,
},
},
}, },
}); });
@ -35,27 +42,35 @@ export const sendCompletedEmail = async ({ documentId, requestMetadata }: SendDo
throw new Error('Document has no recipients'); throw new Error('Document has no recipients');
} }
const buffer = await getFile(document.documentData); const { User: owner } = document;
await Promise.all( const completedDocument = await getFile(document.documentData);
document.Recipient.map(async (recipient) => {
const { email, name, token } = recipient;
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000'; const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
let documentOwnerDownloadLink = `${NEXT_PUBLIC_WEBAPP_URL()}/documents/${document.id}`;
if (document.team?.url) {
documentOwnerDownloadLink = `${NEXT_PUBLIC_WEBAPP_URL()}/t/${document.team.url}/documents/${
document.id
}`;
}
// If the document owner is not a recipient then send the email to them separately
if (!document.Recipient.find((recipient) => recipient.email === owner.email)) {
const template = createElement(DocumentCompletedEmailTemplate, { const template = createElement(DocumentCompletedEmailTemplate, {
documentName: document.title, documentName: document.title,
assetBaseUrl, assetBaseUrl,
downloadLink: `${NEXT_PUBLIC_WEBAPP_URL()}/sign/${token}/complete`, downloadLink: documentOwnerDownloadLink,
}); });
await prisma.$transaction(
async (tx) => {
await mailer.sendMail({ await mailer.sendMail({
to: { to: [
address: email, {
name, name: owner.name || '',
address: owner.email,
}, },
],
from: { from: {
name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso', name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso',
address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com', address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com',
@ -66,12 +81,62 @@ export const sendCompletedEmail = async ({ documentId, requestMetadata }: SendDo
attachments: [ attachments: [
{ {
filename: document.title, filename: document.title,
content: Buffer.from(buffer), content: Buffer.from(completedDocument),
}, },
], ],
}); });
await tx.documentAuditLog.create({ await prisma.documentAuditLog.create({
data: createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT,
documentId: document.id,
user: null,
requestMetadata,
data: {
emailType: 'DOCUMENT_COMPLETED',
recipientEmail: owner.email,
recipientName: owner.name,
recipientId: owner.id,
recipientRole: 'OWNER',
isResending: false,
},
}),
});
}
await Promise.all(
document.Recipient.map(async (recipient) => {
const downloadLink = `${NEXT_PUBLIC_WEBAPP_URL()}/sign/${recipient.token}/complete`;
const template = createElement(DocumentCompletedEmailTemplate, {
documentName: document.title,
assetBaseUrl,
downloadLink: recipient.email === owner.email ? documentOwnerDownloadLink : downloadLink,
});
await mailer.sendMail({
to: [
{
name: recipient.name,
address: recipient.email,
},
],
from: {
name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso',
address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com',
},
subject: 'Signing Complete!',
html: render(template),
text: render(template, { plainText: true }),
attachments: [
{
filename: document.title,
content: Buffer.from(completedDocument),
},
],
});
await prisma.documentAuditLog.create({
data: createDocumentAuditLogData({ data: createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT, type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT,
documentId: document.id, documentId: document.id,
@ -87,9 +152,6 @@ export const sendCompletedEmail = async ({ documentId, requestMetadata }: SendDo
}, },
}), }),
}); });
},
{ timeout: 30_000 },
);
}), }),
); );
}; };