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..66d3eb009 100644 --- a/packages/lib/server-only/document/complete-document-with-token.ts +++ b/packages/lib/server-only/document/complete-document-with-token.ts @@ -47,7 +47,13 @@ export const completeDocumentWithToken = async ({ }: CompleteDocumentWithTokenOptions) => { 'use server'; + const startTime = Date.now(); + console.log('Start:' + startTime); + + console.log('getDocumentStart:' + startTime); const document = await getDocument({ token, documentId }); + console.log('getDocumentEnd:' + (Date.now() - startTime)); + console.log('Acc:' + (Date.now() - startTime)); if (document.status !== DocumentStatus.PENDING) { throw new Error(`Document ${document.id} must be pending`); @@ -63,12 +69,16 @@ export const completeDocumentWithToken = async ({ throw new Error(`Recipient ${recipient.id} has already signed`); } + const fieldStartTime = Date.now(); + console.log('fieldStart:' + fieldStartTime); const fields = await prisma.field.findMany({ where: { documentId: document.id, recipientId: recipient.id, }, }); + console.log('fieldEnd:' + (Date.now() - fieldStartTime)); + console.log('Acc:' + (Date.now() - startTime)); if (fields.some((field) => !field.inserted)) { throw new Error(`Recipient ${recipient.id} has unsigned fields`); @@ -93,6 +103,9 @@ export const completeDocumentWithToken = async ({ // throw new AppError(AppErrorCode.UNAUTHORIZED, 'Invalid authentication values'); // } + const recipientUpdateStartTime = Date.now(); + console.log('recipientUpdateStart:' + recipientUpdateStartTime); + await prisma.$transaction(async (tx) => { await tx.recipient.update({ where: { @@ -124,6 +137,12 @@ export const completeDocumentWithToken = async ({ }); }); + console.log('recipientUpdateEnd:' + (Date.now() - recipientUpdateStartTime)); + console.log('Acc:' + (Date.now() - startTime)); + + const pendingRecipientsStartTime = Date.now(); + console.log('pendingRecipientsStart:' + pendingRecipientsStartTime); + const pendingRecipients = await prisma.recipient.count({ where: { documentId: document.id, @@ -132,11 +151,16 @@ export const completeDocumentWithToken = async ({ }, }, }); + console.log('pendingRecipientsEnd:' + (Date.now() - pendingRecipientsStartTime)); + console.log('Acc:' + (Date.now() - startTime)); if (pendingRecipients > 0) { await sendPendingEmail({ documentId, recipientId: recipient.id }); } + const updateDocumentStartTime = Date.now(); + console.log('updateDocumentStart:' + updateDocumentStartTime); + const documents = await prisma.document.updateMany({ where: { id: document.id, @@ -151,12 +175,26 @@ export const completeDocumentWithToken = async ({ completedAt: new Date(), }, }); + console.log('updateDocumentEnd:' + (Date.now() - updateDocumentStartTime)); + console.log('Acc:' + (Date.now() - startTime)); if (documents.count > 0) { + const sealDocumentStartTime = Date.now(); + console.log('sealDocumentStart:' + sealDocumentStartTime); await sealDocument({ documentId: document.id, requestMetadata }); + console.log('sealDocumentEnd:' + (Date.now() - sealDocumentStartTime)); + console.log('Acc:' + (Date.now() - startTime)); } + const updateDocumentStartTime2 = Date.now(); + console.log('updateDocumentStart2:' + updateDocumentStartTime2); + const updatedDocument = await getDocument({ token, documentId }); + console.log('updateDocumentEnd2:' + (Date.now() - updateDocumentStartTime2)); + console.log('Acc:' + (Date.now() - startTime)); + + const triggerWebhookStartTime = Date.now(); + console.log('triggerWebhookStart:' + triggerWebhookStartTime); await triggerWebhook({ event: WebhookTriggerEvents.DOCUMENT_SIGNED, @@ -164,4 +202,6 @@ export const completeDocumentWithToken = async ({ userId: updatedDocument.userId, teamId: updatedDocument.teamId ?? undefined, }); + console.log('triggerWebhookEnd:' + (Date.now() - triggerWebhookStartTime)); + console.log('Acc:' + (Date.now() - startTime)); }; diff --git a/packages/lib/server-only/document/seal-document.ts b/packages/lib/server-only/document/seal-document.ts index 3e366dc81..c52f7e635 100644 --- a/packages/lib/server-only/document/seal-document.ts +++ b/packages/lib/server-only/document/seal-document.ts @@ -90,13 +90,22 @@ export const sealDocument = async ({ } // !: Need to write the fields onto the document as a hard copy + const getFileTime = Date.now(); + console.log('getFileStart:' + getFileTime); const pdfData = await getFile(documentData); + console.log('getFileEnd:' + (Date.now() - getFileTime)); + const getCertificatePdfTime = Date.now(); + console.log('getCertificatePdfStart:' + getCertificatePdfTime); const certificate = await getCertificatePdf({ documentId }).then(async (doc) => PDFDocument.load(doc), ); + console.log('getCertificatePdfEnd:' + (Date.now() - getCertificatePdfTime)); + const loadDoc = Date.now(); + console.log('loadDocStart:' + loadDoc); const doc = await PDFDocument.load(pdfData); + console.log('loadDocEnd:' + (Date.now() - loadDoc)); // Normalize and flatten layers that could cause issues with the signature normalizeSignatureAppearances(doc); diff --git a/packages/lib/server-only/htmltopdf/get-certificate-pdf.ts b/packages/lib/server-only/htmltopdf/get-certificate-pdf.ts index dee40d41a..8a208482f 100644 --- a/packages/lib/server-only/htmltopdf/get-certificate-pdf.ts +++ b/packages/lib/server-only/htmltopdf/get-certificate-pdf.ts @@ -22,7 +22,9 @@ export const getCertificatePdf = async ({ documentId }: GetCertificatePdfOptions // !: Previously we would have to keep the playwright version in sync with the browserless version to avoid errors. browser = await chromium.connectOverCDP(process.env.NEXT_PRIVATE_BROWSERLESS_URL); } else { + console.log('here'); browser = await chromium.launch(); + console.log('here2'); } if (!browser) { @@ -31,17 +33,41 @@ export const getCertificatePdf = async ({ documentId }: GetCertificatePdfOptions ); } + console.log('1'); const page = await browser.newPage(); + console.log('2'); - await page.goto(`${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encryptedId}`, { - waitUntil: 'networkidle', - }); + const domcontentloadedTime = Date.now(); + console.log('domcontentloadedTime:' + domcontentloadedTime); + + await page + .goto(`${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encryptedId}`, { + waitUntil: 'domcontentloaded', + }) + .catch((e) => { + console.log(e); + }); + console.log('domcontentloadedEnd:' + (Date.now() - domcontentloadedTime)); + + const networkidleTime = Date.now(); + console.log('networkidleTime:' + networkidleTime); + + await page + .goto(`${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encryptedId}`, { + waitUntil: 'networkidle', + }) + .catch((e) => { + console.log(e); + }); + console.log('networkidleEnd:' + (Date.now() - networkidleTime)); const result = await page.pdf({ format: 'A4', }); + console.log(4); void browser.close(); + console.log(5); return result; };