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 d16b83ea1..053b2f1c2 100644 --- a/packages/lib/server-only/document/complete-document-with-token.ts +++ b/packages/lib/server-only/document/complete-document-with-token.ts @@ -137,7 +137,7 @@ export const completeDocumentWithToken = async ({ await sendPendingEmail({ documentId, recipientId: recipient.id }); } - const documents = await prisma.document.updateMany({ + const haveAllRecipientsSigned = await prisma.document.findFirst({ where: { id: document.id, Recipient: { @@ -146,16 +146,21 @@ export const completeDocumentWithToken = async ({ }, }, }, + }); + + if (haveAllRecipientsSigned) { + await sealDocument({ documentId: document.id, requestMetadata }); + } + + await prisma.document.update({ + where: { + id: document.id, + }, data: { - status: DocumentStatus.COMPLETED, completedAt: new Date(), }, }); - if (documents.count > 0) { - await sealDocument({ documentId: document.id, requestMetadata }); - } - const updatedDocument = await getDocument({ token, documentId }); await triggerWebhook({ diff --git a/packages/lib/server-only/document/seal-document.ts b/packages/lib/server-only/document/seal-document.ts index 3e366dc81..cbfb1dd98 100644 --- a/packages/lib/server-only/document/seal-document.ts +++ b/packages/lib/server-only/document/seal-document.ts @@ -40,6 +40,11 @@ export const sealDocument = async ({ const document = await prisma.document.findFirstOrThrow({ where: { id: documentId, + Recipient: { + every: { + signingStatus: SigningStatus.SIGNED, + }, + }, }, include: { documentData: true, @@ -53,10 +58,6 @@ export const sealDocument = async ({ throw new Error(`Document ${document.id} has no document data`); } - if (document.status !== DocumentStatus.COMPLETED) { - throw new Error(`Document ${document.id} has not been completed`); - } - const recipients = await prisma.recipient.findMany({ where: { documentId: document.id, @@ -92,9 +93,9 @@ export const sealDocument = async ({ // !: Need to write the fields onto the document as a hard copy const pdfData = await getFile(documentData); - const certificate = await getCertificatePdf({ documentId }).then(async (doc) => - PDFDocument.load(doc), - ); + const certificate = await getCertificatePdf({ documentId }) + .then(async (doc) => PDFDocument.load(doc)) + .catch(() => null); const doc = await PDFDocument.load(pdfData); @@ -103,11 +104,13 @@ export const sealDocument = async ({ doc.getForm().flatten(); flattenAnnotations(doc); - const certificatePages = await doc.copyPages(certificate, certificate.getPageIndices()); + if (certificate) { + const certificatePages = await doc.copyPages(certificate, certificate.getPageIndices()); - certificatePages.forEach((page) => { - doc.addPage(page); - }); + certificatePages.forEach((page) => { + doc.addPage(page); + }); + } for (const field of fields) { await insertFieldInPDF(doc, field); @@ -138,6 +141,15 @@ export const sealDocument = async ({ } await prisma.$transaction(async (tx) => { + await tx.document.update({ + where: { + id: document.id, + }, + data: { + status: DocumentStatus.COMPLETED, + }, + }); + await tx.documentData.update({ where: { id: documentData.id, diff --git a/packages/lib/server-only/htmltopdf/get-certificate-pdf.ts b/packages/lib/server-only/htmltopdf/get-certificate-pdf.ts index dee40d41a..1b6150fb9 100644 --- a/packages/lib/server-only/htmltopdf/get-certificate-pdf.ts +++ b/packages/lib/server-only/htmltopdf/get-certificate-pdf.ts @@ -35,6 +35,7 @@ export const getCertificatePdf = async ({ documentId }: GetCertificatePdfOptions await page.goto(`${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encryptedId}`, { waitUntil: 'networkidle', + timeout: 10_000, }); const result = await page.pdf({