mirror of
https://github.com/documenso/documenso.git
synced 2025-11-20 03:32:14 +10:00
fix: improve sealing speed (#2210)
This commit is contained in:
@ -25,7 +25,6 @@ import { signPdf } from '@documenso/signing';
|
|||||||
|
|
||||||
import { AppError, AppErrorCode } from '../../../errors/app-error';
|
import { AppError, AppErrorCode } from '../../../errors/app-error';
|
||||||
import { sendCompletedEmail } from '../../../server-only/document/send-completed-email';
|
import { sendCompletedEmail } from '../../../server-only/document/send-completed-email';
|
||||||
import PostHogServerClient from '../../../server-only/feature-flags/get-post-hog-server-client';
|
|
||||||
import { getAuditLogsPdf } from '../../../server-only/htmltopdf/get-audit-logs-pdf';
|
import { getAuditLogsPdf } from '../../../server-only/htmltopdf/get-audit-logs-pdf';
|
||||||
import { getCertificatePdf } from '../../../server-only/htmltopdf/get-certificate-pdf';
|
import { getCertificatePdf } from '../../../server-only/htmltopdf/get-certificate-pdf';
|
||||||
import { addRejectionStampToPdf } from '../../../server-only/pdf/add-rejection-stamp-to-pdf';
|
import { addRejectionStampToPdf } from '../../../server-only/pdf/add-rejection-stamp-to-pdf';
|
||||||
@ -62,6 +61,7 @@ export const run = async ({
|
|||||||
}) => {
|
}) => {
|
||||||
const { documentId, sendEmail = true, isResealing = false, requestMetadata } = payload;
|
const { documentId, sendEmail = true, isResealing = false, requestMetadata } = payload;
|
||||||
|
|
||||||
|
const { envelopeId, envelopeStatus, isRejected } = await io.runTask('seal-document', async () => {
|
||||||
const envelope = await prisma.envelope.findFirstOrThrow({
|
const envelope = await prisma.envelope.findFirstOrThrow({
|
||||||
where: {
|
where: {
|
||||||
type: EnvelopeType.DOCUMENT,
|
type: EnvelopeType.DOCUMENT,
|
||||||
@ -102,24 +102,7 @@ export const run = async ({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seems silly but we need to do this in case the job is re-ran
|
let envelopeItems = envelope.envelopeItems;
|
||||||
// after it has already run through the update task further below.
|
|
||||||
// eslint-disable-next-line @typescript-eslint/require-await
|
|
||||||
const documentStatus = await io.runTask('get-document-status', async () => {
|
|
||||||
return envelope.status;
|
|
||||||
});
|
|
||||||
|
|
||||||
// This is the same case as above.
|
|
||||||
let envelopeItems = await io.runTask(
|
|
||||||
'get-document-data-id',
|
|
||||||
// eslint-disable-next-line @typescript-eslint/require-await
|
|
||||||
async () => {
|
|
||||||
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
||||||
return envelope.envelopeItems.map(({ field, ...rest }) => ({
|
|
||||||
...rest,
|
|
||||||
}));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (envelopeItems.length < 1) {
|
if (envelopeItems.length < 1) {
|
||||||
throw new Error(`Document ${envelope.id} has no envelope items`);
|
throw new Error(`Document ${envelope.id} has no envelope items`);
|
||||||
@ -189,44 +172,9 @@ export const run = async ({
|
|||||||
settings,
|
settings,
|
||||||
});
|
});
|
||||||
|
|
||||||
// !: The commented out code is our desired implementation but we're seemingly
|
|
||||||
// !: running into issues with inngest parallelism in production.
|
|
||||||
// !: Until this is resolved we will do this sequentially which is slower but
|
|
||||||
// !: will actually work.
|
|
||||||
// const decoratePromises: Array<Promise<{ oldDocumentDataId: string; newDocumentDataId: string }>> =
|
|
||||||
// [];
|
|
||||||
|
|
||||||
// for (const envelopeItem of envelopeItems) {
|
|
||||||
// const task = io.runTask(`decorate-${envelopeItem.id}`, async () => {
|
|
||||||
// const envelopeItemFields = envelope.envelopeItems.find(
|
|
||||||
// (item) => item.id === envelopeItem.id,
|
|
||||||
// )?.field;
|
|
||||||
|
|
||||||
// if (!envelopeItemFields) {
|
|
||||||
// throw new Error(`Envelope item fields not found for envelope item ${envelopeItem.id}`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return decorateAndSignPdf({
|
|
||||||
// envelope,
|
|
||||||
// envelopeItem,
|
|
||||||
// envelopeItemFields,
|
|
||||||
// isRejected,
|
|
||||||
// rejectionReason,
|
|
||||||
// certificateData,
|
|
||||||
// auditLogData,
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
|
|
||||||
// decoratePromises.push(task);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const newDocumentData = await Promise.all(decoratePromises);
|
|
||||||
|
|
||||||
// TODO: Remove once parallelization is working
|
|
||||||
const newDocumentData: Array<{ oldDocumentDataId: string; newDocumentDataId: string }> = [];
|
const newDocumentData: Array<{ oldDocumentDataId: string; newDocumentDataId: string }> = [];
|
||||||
|
|
||||||
for (const envelopeItem of envelopeItems) {
|
for (const envelopeItem of envelopeItems) {
|
||||||
const result = await io.runTask(`decorate-${envelopeItem.id}`, async () => {
|
|
||||||
const envelopeItemFields = envelope.envelopeItems.find(
|
const envelopeItemFields = envelope.envelopeItems.find(
|
||||||
(item) => item.id === envelopeItem.id,
|
(item) => item.id === envelopeItem.id,
|
||||||
)?.field;
|
)?.field;
|
||||||
@ -235,7 +183,7 @@ export const run = async ({
|
|||||||
throw new Error(`Envelope item fields not found for envelope item ${envelopeItem.id}`);
|
throw new Error(`Envelope item fields not found for envelope item ${envelopeItem.id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return decorateAndSignPdf({
|
const result = await decorateAndSignPdf({
|
||||||
envelope,
|
envelope,
|
||||||
envelopeItem,
|
envelopeItem,
|
||||||
envelopeItemFields,
|
envelopeItemFields,
|
||||||
@ -244,25 +192,10 @@ export const run = async ({
|
|||||||
certificateData,
|
certificateData,
|
||||||
auditLogData,
|
auditLogData,
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
newDocumentData.push(result);
|
newDocumentData.push(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
const postHog = PostHogServerClient();
|
|
||||||
|
|
||||||
if (postHog) {
|
|
||||||
postHog.capture({
|
|
||||||
distinctId: nanoid(),
|
|
||||||
event: 'App: Document Sealed',
|
|
||||||
properties: {
|
|
||||||
documentId: envelope.id,
|
|
||||||
isRejected,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await io.runTask('update-document', async () => {
|
|
||||||
await prisma.$transaction(async (tx) => {
|
await prisma.$transaction(async (tx) => {
|
||||||
for (const { oldDocumentDataId, newDocumentDataId } of newDocumentData) {
|
for (const { oldDocumentDataId, newDocumentDataId } of newDocumentData) {
|
||||||
const newData = await tx.documentData.findFirstOrThrow({
|
const newData = await tx.documentData.findFirstOrThrow({
|
||||||
@ -304,18 +237,24 @@ export const run = async ({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
envelopeId: envelope.id,
|
||||||
|
envelopeStatus: envelope.status,
|
||||||
|
isRejected,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
await io.runTask('send-completed-email', async () => {
|
await io.runTask('send-completed-email', async () => {
|
||||||
let shouldSendCompletedEmail = sendEmail && !isResealing && !isRejected;
|
let shouldSendCompletedEmail = sendEmail && !isResealing && !isRejected;
|
||||||
|
|
||||||
if (isResealing && !isDocumentCompleted(envelope.status)) {
|
if (isResealing && !isDocumentCompleted(envelopeStatus)) {
|
||||||
shouldSendCompletedEmail = sendEmail;
|
shouldSendCompletedEmail = sendEmail;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldSendCompletedEmail) {
|
if (shouldSendCompletedEmail) {
|
||||||
await sendCompletedEmail({
|
await sendCompletedEmail({
|
||||||
id: { type: 'envelopeId', id: envelope.id },
|
id: { type: 'envelopeId', id: envelopeId },
|
||||||
requestMetadata,
|
requestMetadata,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -323,7 +262,7 @@ export const run = async ({
|
|||||||
|
|
||||||
const updatedEnvelope = await prisma.envelope.findFirstOrThrow({
|
const updatedEnvelope = await prisma.envelope.findFirstOrThrow({
|
||||||
where: {
|
where: {
|
||||||
id: envelope.id,
|
id: envelopeId,
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
documentMeta: true,
|
documentMeta: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user