refactor: pass document id as arguments

This commit is contained in:
Ephraim Atta-Duncan
2023-09-07 20:38:18 +00:00
committed by Mythie
parent 94215fffbb
commit f18010e1e1
2 changed files with 33 additions and 5 deletions

View File

@ -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: {

View File

@ -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';