From b17e73003edf8db104ee3321cf5aa18aadc1ac66 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Thu, 7 Sep 2023 19:58:48 +0000 Subject: [PATCH] feat: send email when recipient is done signing --- .../document/complete-document-with-token.ts | 3 ++ .../document/send-recipient-signed-email.ts | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 packages/lib/server-only/document/send-recipient-signed-email.ts diff --git a/packages/lib/server-only/document/complete-document-with-token.ts b/packages/lib/server-only/document/complete-document-with-token.ts index a013d5e69..4d4bacabf 100644 --- a/packages/lib/server-only/document/complete-document-with-token.ts +++ b/packages/lib/server-only/document/complete-document-with-token.ts @@ -4,6 +4,7 @@ import { prisma } from '@documenso/prisma'; import { DocumentStatus, SigningStatus } from '@documenso/prisma/client'; import { sealDocument } from './seal-document'; +import { sendPendingEmail } from './send-recipient-signed-email'; export type CompleteDocumentWithTokenOptions = { token: string; @@ -69,6 +70,8 @@ export const completeDocumentWithToken = async ({ }, }); + await sendPendingEmail({ document, recipient }); + const documents = await prisma.document.updateMany({ where: { id: document.id, diff --git a/packages/lib/server-only/document/send-recipient-signed-email.ts b/packages/lib/server-only/document/send-recipient-signed-email.ts new file mode 100644 index 000000000..5deed2343 --- /dev/null +++ b/packages/lib/server-only/document/send-recipient-signed-email.ts @@ -0,0 +1,36 @@ +import { createElement } from 'react'; + +import { mailer } from '@documenso/email/mailer'; +import { render } from '@documenso/email/render'; +import { DocumentPendingEmailTemplate } from '@documenso/email/templates/document-pending'; +import { Document, Recipient } from '@documenso/prisma/client'; + +export interface SendPendingEmailOptions { + document: Document; + recipient: Recipient; +} + +export const sendPendingEmail = async ({ document, recipient }: SendPendingEmailOptions) => { + const { email, name } = recipient; + + const assetBaseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000'; + + const template = createElement(DocumentPendingEmailTemplate, { + documentName: document.title, + assetBaseUrl, + }); + + await mailer.sendMail({ + to: { + address: email, + name, + }, + from: { + name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso', + address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com', + }, + subject: 'You are done signing.', + html: render(template), + text: render(template, { plainText: true }), + }); +};