fix: yeet

This commit is contained in:
David Nguyen
2024-04-26 18:11:15 +07:00
parent 20edee7f1a
commit 2dd3e4440f
3 changed files with 78 additions and 3 deletions

View File

@ -47,7 +47,13 @@ export const completeDocumentWithToken = async ({
}: CompleteDocumentWithTokenOptions) => { }: CompleteDocumentWithTokenOptions) => {
'use server'; 'use server';
const startTime = Date.now();
console.log('Start:' + startTime);
console.log('getDocumentStart:' + startTime);
const document = await getDocument({ token, documentId }); const document = await getDocument({ token, documentId });
console.log('getDocumentEnd:' + (Date.now() - startTime));
console.log('Acc:' + (Date.now() - startTime));
if (document.status !== DocumentStatus.PENDING) { if (document.status !== DocumentStatus.PENDING) {
throw new Error(`Document ${document.id} must be 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`); throw new Error(`Recipient ${recipient.id} has already signed`);
} }
const fieldStartTime = Date.now();
console.log('fieldStart:' + fieldStartTime);
const fields = await prisma.field.findMany({ const fields = await prisma.field.findMany({
where: { where: {
documentId: document.id, documentId: document.id,
recipientId: recipient.id, recipientId: recipient.id,
}, },
}); });
console.log('fieldEnd:' + (Date.now() - fieldStartTime));
console.log('Acc:' + (Date.now() - startTime));
if (fields.some((field) => !field.inserted)) { if (fields.some((field) => !field.inserted)) {
throw new Error(`Recipient ${recipient.id} has unsigned fields`); 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'); // throw new AppError(AppErrorCode.UNAUTHORIZED, 'Invalid authentication values');
// } // }
const recipientUpdateStartTime = Date.now();
console.log('recipientUpdateStart:' + recipientUpdateStartTime);
await prisma.$transaction(async (tx) => { await prisma.$transaction(async (tx) => {
await tx.recipient.update({ await tx.recipient.update({
where: { 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({ const pendingRecipients = await prisma.recipient.count({
where: { where: {
documentId: document.id, 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) { if (pendingRecipients > 0) {
await sendPendingEmail({ documentId, recipientId: recipient.id }); await sendPendingEmail({ documentId, recipientId: recipient.id });
} }
const updateDocumentStartTime = Date.now();
console.log('updateDocumentStart:' + updateDocumentStartTime);
const documents = await prisma.document.updateMany({ const documents = await prisma.document.updateMany({
where: { where: {
id: document.id, id: document.id,
@ -151,12 +175,26 @@ export const completeDocumentWithToken = async ({
completedAt: new Date(), completedAt: new Date(),
}, },
}); });
console.log('updateDocumentEnd:' + (Date.now() - updateDocumentStartTime));
console.log('Acc:' + (Date.now() - startTime));
if (documents.count > 0) { if (documents.count > 0) {
const sealDocumentStartTime = Date.now();
console.log('sealDocumentStart:' + sealDocumentStartTime);
await sealDocument({ documentId: document.id, requestMetadata }); 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 }); 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({ await triggerWebhook({
event: WebhookTriggerEvents.DOCUMENT_SIGNED, event: WebhookTriggerEvents.DOCUMENT_SIGNED,
@ -164,4 +202,6 @@ export const completeDocumentWithToken = async ({
userId: updatedDocument.userId, userId: updatedDocument.userId,
teamId: updatedDocument.teamId ?? undefined, teamId: updatedDocument.teamId ?? undefined,
}); });
console.log('triggerWebhookEnd:' + (Date.now() - triggerWebhookStartTime));
console.log('Acc:' + (Date.now() - startTime));
}; };

View File

@ -90,13 +90,22 @@ export const sealDocument = async ({
} }
// !: Need to write the fields onto the document as a hard copy // !: 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); 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) => const certificate = await getCertificatePdf({ documentId }).then(async (doc) =>
PDFDocument.load(doc), PDFDocument.load(doc),
); );
console.log('getCertificatePdfEnd:' + (Date.now() - getCertificatePdfTime));
const loadDoc = Date.now();
console.log('loadDocStart:' + loadDoc);
const doc = await PDFDocument.load(pdfData); const doc = await PDFDocument.load(pdfData);
console.log('loadDocEnd:' + (Date.now() - loadDoc));
// Normalize and flatten layers that could cause issues with the signature // Normalize and flatten layers that could cause issues with the signature
normalizeSignatureAppearances(doc); normalizeSignatureAppearances(doc);

View File

@ -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. // !: 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); browser = await chromium.connectOverCDP(process.env.NEXT_PRIVATE_BROWSERLESS_URL);
} else { } else {
console.log('here');
browser = await chromium.launch(); browser = await chromium.launch();
console.log('here2');
} }
if (!browser) { if (!browser) {
@ -31,17 +33,41 @@ export const getCertificatePdf = async ({ documentId }: GetCertificatePdfOptions
); );
} }
console.log('1');
const page = await browser.newPage(); const page = await browser.newPage();
console.log('2');
await page.goto(`${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encryptedId}`, { const domcontentloadedTime = Date.now();
waitUntil: 'networkidle', 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({ const result = await page.pdf({
format: 'A4', format: 'A4',
}); });
console.log(4);
void browser.close(); void browser.close();
console.log(5);
return result; return result;
}; };