From f18010e1e1168fcde6f0e7979dd94c9e548e8fbd Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Thu, 7 Sep 2023 20:38:18 +0000 Subject: [PATCH] refactor: pass document id as arguments --- .../document/complete-document-with-token.ts | 2 +- .../document/send-recipient-signed-email.ts | 36 ++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) 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 77ab33d04..ea440beb9 100644 --- a/packages/lib/server-only/document/complete-document-with-token.ts +++ b/packages/lib/server-only/document/complete-document-with-token.ts @@ -71,7 +71,7 @@ export const completeDocumentWithToken = async ({ }); // TODO: Send email to documents with two or more recipients - await sendPendingEmail({ document, recipient }); + await sendPendingEmail({ documentId, recipientId: recipient.id }); const documents = await prisma.document.updateMany({ where: { diff --git a/packages/lib/server-only/document/send-recipient-signed-email.ts b/packages/lib/server-only/document/send-recipient-signed-email.ts index 5deed2343..ece75caec 100644 --- a/packages/lib/server-only/document/send-recipient-signed-email.ts +++ b/packages/lib/server-only/document/send-recipient-signed-email.ts @@ -3,14 +3,42 @@ 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'; +import { prisma } from '@documenso/prisma'; export interface SendPendingEmailOptions { - document: Document; - recipient: Recipient; + documentId: number; + recipientId: number; } -export const sendPendingEmail = async ({ document, recipient }: SendPendingEmailOptions) => { +export const sendPendingEmail = async ({ documentId, recipientId }: SendPendingEmailOptions) => { + const document = await prisma.document.findFirst({ + where: { + id: documentId, + Recipient: { + some: { + id: recipientId, + }, + }, + }, + include: { + Recipient: { + where: { + id: recipientId, + }, + }, + }, + }); + + if (!document) { + throw new Error('Document not found'); + } + + if (document.Recipient.length === 0) { + throw new Error('Document has no recipients'); + } + + const [recipient] = document.Recipient; + const { email, name } = recipient; const assetBaseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000';