Compare commits

...

2 Commits

Author SHA1 Message Date
ea93c1f508 fix: asdf 2024-04-26 18:31:49 +07:00
2dd3e4440f fix: yeet 2024-04-26 18:11:15 +07:00
3 changed files with 114 additions and 4 deletions

View File

@ -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));
};

View File

@ -90,41 +90,69 @@ 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);
doc.getForm().flatten();
flattenAnnotations(doc);
const certificatePageTime = Date.now();
console.log('certificatePageStart:' + certificatePageTime);
const certificatePages = await doc.copyPages(certificate, certificate.getPageIndices());
console.log('certificatePageEnd:' + (Date.now() - certificatePageTime));
certificatePages.forEach((page) => {
doc.addPage(page);
});
for (const field of fields) {
const insertFIeldTime = Date.now();
console.log('insertFieldStart:' + insertFIeldTime);
await insertFieldInPDF(doc, field);
console.log('insertFieldEnd:' + (Date.now() - insertFIeldTime));
}
const pdfBytes = await doc.save();
const docSaveTime = Date.now();
console.log('docSaveStart:' + docSaveTime);
const pdfBytes = await doc.save();
console.log('docSaveEnd:' + (Date.now() - docSaveTime));
const pdfBufferTIme = Date.now();
console.log('pdfBufferStart:' + pdfBufferTIme);
const pdfBuffer = await signPdf({ pdf: Buffer.from(pdfBytes) });
console.log('pdfBufferEnd:' + (Date.now() - pdfBufferTIme));
const { name, ext } = path.parse(document.title);
const putFIleTIme = Date.now();
console.log('putFileStart:' + putFIleTIme);
const { data: newData } = await putFile({
name: `${name}_signed${ext}`,
type: 'application/pdf',
arrayBuffer: async () => Promise.resolve(pdfBuffer),
});
console.log('putFileEnd:' + (Date.now() - putFIleTIme));
const postHog = PostHogServerClient();
if (postHog) {
@ -137,6 +165,9 @@ export const sealDocument = async ({
});
}
const updateDocumentTime = Date.now();
console.log('updateDocumentStart:' + updateDocumentTime);
await prisma.$transaction(async (tx) => {
await tx.documentData.update({
where: {
@ -160,10 +191,18 @@ export const sealDocument = async ({
});
});
console.log('updateDocumentEnd:' + (Date.now() - updateDocumentTime));
if (sendEmail && !isResealing) {
const sendCompleteEmailTime = Date.now();
console.log('sendCompleteEmailStart:' + sendCompleteEmailTime);
await sendCompletedEmail({ documentId, requestMetadata });
console.log('sendCompleteEmailEnd:' + (Date.now() - sendCompleteEmailTime));
}
const asdfasdfasdf = Date.now();
console.log('updateDocumentStart:' + asdfasdfasdf);
const updatedDocument = await prisma.document.findFirstOrThrow({
where: {
id: document.id,
@ -173,6 +212,10 @@ export const sealDocument = async ({
Recipient: true,
},
});
console.log('updateDocumentEnd:' + (Date.now() - asdfasdfasdf));
const triggerWebhookTime = Date.now();
console.log('triggerWebhookStart:' + triggerWebhookTime);
await triggerWebhook({
event: WebhookTriggerEvents.DOCUMENT_COMPLETED,
@ -180,4 +223,5 @@ export const sealDocument = async ({
userId: document.userId,
teamId: document.teamId ?? undefined,
});
console.log('triggerWebhookEnd:' + (Date.now() - triggerWebhookTime));
};

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.
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;
};