Compare commits

..

1 Commits

Author SHA1 Message Date
31b6d41cd2 fix: improve sealing speed 2025-11-19 12:09:05 +11:00
7 changed files with 120 additions and 236 deletions

View File

@ -5,7 +5,7 @@ import { msg } from '@lingui/core/macro';
import { Trans, useLingui } from '@lingui/react/macro'; import { Trans, useLingui } from '@lingui/react/macro';
import { FieldType, RecipientRole } from '@prisma/client'; import { FieldType, RecipientRole } from '@prisma/client';
import { FileTextIcon } from 'lucide-react'; import { FileTextIcon } from 'lucide-react';
import { Link, useSearchParams } from 'react-router'; import { Link } from 'react-router';
import { isDeepEqual } from 'remeda'; import { isDeepEqual } from 'remeda';
import { match } from 'ts-pattern'; import { match } from 'ts-pattern';
@ -65,8 +65,6 @@ const FieldSettingsTypeTranslations: Record<FieldType, MessageDescriptor> = {
}; };
export const EnvelopeEditorFieldsPage = () => { export const EnvelopeEditorFieldsPage = () => {
const [searchParams] = useSearchParams();
const { envelope, editorFields, relativePath } = useCurrentEnvelopeEditor(); const { envelope, editorFields, relativePath } = useCurrentEnvelopeEditor();
const { currentEnvelopeItem } = useCurrentEnvelopeRender(); const { currentEnvelopeItem } = useCurrentEnvelopeRender();
@ -210,37 +208,6 @@ export const EnvelopeEditorFieldsPage = () => {
<section> <section>
<Separator className="my-4" /> <Separator className="my-4" />
{searchParams.get('devmode') && (
<>
<div className="px-4">
<h3 className="text-foreground mb-3 text-sm font-semibold">
<Trans>Developer Mode</Trans>
</h3>
<div className="bg-muted/50 border-border text-foreground space-y-2 rounded-md border p-3 text-sm">
<p>
<span className="text-muted-foreground min-w-12">Pos X:&nbsp;</span>
{selectedField.positionX.toFixed(2)}
</p>
<p>
<span className="text-muted-foreground min-w-12">Pos Y:&nbsp;</span>
{selectedField.positionY.toFixed(2)}
</p>
<p>
<span className="text-muted-foreground min-w-12">Width:&nbsp;</span>
{selectedField.width.toFixed(2)}
</p>
<p>
<span className="text-muted-foreground min-w-12">Height:&nbsp;</span>
{selectedField.height.toFixed(2)}
</p>
</div>
</div>
<Separator className="my-4" />
</>
)}
<div className="[&_label]:text-foreground/70 px-4 [&_label]:text-xs"> <div className="[&_label]:text-foreground/70 px-4 [&_label]:text-xs">
<h3 className="text-sm font-semibold"> <h3 className="text-sm font-semibold">
{t(FieldSettingsTypeTranslations[selectedField.type])} {t(FieldSettingsTypeTranslations[selectedField.type])}

View File

@ -106,5 +106,5 @@
"vite-plugin-babel-macros": "^1.0.6", "vite-plugin-babel-macros": "^1.0.6",
"vite-tsconfig-paths": "^5.1.4" "vite-tsconfig-paths": "^5.1.4"
}, },
"version": "2.0.13" "version": "2.0.12"
} }

6
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@documenso/root", "name": "@documenso/root",
"version": "2.0.13", "version": "2.0.12",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@documenso/root", "name": "@documenso/root",
"version": "2.0.13", "version": "2.0.12",
"workspaces": [ "workspaces": [
"apps/*", "apps/*",
"packages/*" "packages/*"
@ -101,7 +101,7 @@
}, },
"apps/remix": { "apps/remix": {
"name": "@documenso/remix", "name": "@documenso/remix",
"version": "2.0.13", "version": "2.0.12",
"dependencies": { "dependencies": {
"@cantoo/pdf-lib": "^2.5.2", "@cantoo/pdf-lib": "^2.5.2",
"@documenso/api": "*", "@documenso/api": "*",

View File

@ -1,6 +1,6 @@
{ {
"private": true, "private": true,
"version": "2.0.13", "version": "2.0.12",
"scripts": { "scripts": {
"build": "turbo run build", "build": "turbo run build",
"dev": "turbo run dev --filter=@documenso/remix", "dev": "turbo run dev --filter=@documenso/remix",

View File

@ -1,16 +1,6 @@
import { z } from 'zod'; import { z } from 'zod';
export const SUPPORTED_LANGUAGE_CODES = [ export const SUPPORTED_LANGUAGE_CODES = ['de', 'en', 'fr', 'es', 'it', 'pl'] as const;
'de',
'en',
'fr',
'es',
'it',
'pl',
'ja',
'ko',
'zh',
] as const;
export const ZSupportedLanguageCodeSchema = z.enum(SUPPORTED_LANGUAGE_CODES).catch('en'); export const ZSupportedLanguageCodeSchema = z.enum(SUPPORTED_LANGUAGE_CODES).catch('en');
@ -64,18 +54,6 @@ export const SUPPORTED_LANGUAGES: Record<string, SupportedLanguage> = {
short: 'pl', short: 'pl',
full: 'Polish', full: 'Polish',
}, },
ja: {
short: 'ja',
full: 'Japanese',
},
ko: {
short: 'ko',
full: 'Korean',
},
zh: {
short: 'zh',
full: 'Chinese',
},
} satisfies Record<SupportedLanguageCodes, SupportedLanguage>; } satisfies Record<SupportedLanguageCodes, SupportedLanguage>;
export const isValidLanguageCode = (code: unknown): code is SupportedLanguageCodes => export const isValidLanguageCode = (code: unknown): code is SupportedLanguageCodes =>

View File

@ -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,171 +61,120 @@ export const run = async ({
}) => { }) => {
const { documentId, sendEmail = true, isResealing = false, requestMetadata } = payload; const { documentId, sendEmail = true, isResealing = false, requestMetadata } = payload;
const envelope = await prisma.envelope.findFirstOrThrow({ const { envelopeId, envelopeStatus, isRejected } = await io.runTask('seal-document', async () => {
where: { const envelope = await prisma.envelope.findFirstOrThrow({
type: EnvelopeType.DOCUMENT, where: {
secondaryId: mapDocumentIdToSecondaryId(documentId), type: EnvelopeType.DOCUMENT,
}, secondaryId: mapDocumentIdToSecondaryId(documentId),
include: { },
documentMeta: true, include: {
recipients: true, documentMeta: true,
envelopeItems: { recipients: true,
include: { envelopeItems: {
documentData: true, include: {
field: { documentData: true,
include: { field: {
signature: true, include: {
signature: true,
},
}, },
}, },
}, },
}, },
},
});
if (envelope.envelopeItems.length === 0) {
throw new Error('At least one envelope item required');
}
const settings = await getTeamSettings({
userId: envelope.userId,
teamId: envelope.teamId,
});
const isComplete =
envelope.recipients.some((recipient) => recipient.signingStatus === SigningStatus.REJECTED) ||
envelope.recipients.every((recipient) => recipient.signingStatus === SigningStatus.SIGNED);
if (!isComplete) {
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
message: 'Document is not complete',
}); });
}
// Seems silly but we need to do this in case the job is re-ran if (envelope.envelopeItems.length === 0) {
// after it has already run through the update task further below. throw new Error('At least one envelope item required');
// 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. const settings = await getTeamSettings({
let envelopeItems = await io.runTask( userId: envelope.userId,
'get-document-data-id', teamId: envelope.teamId,
// 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) { const isComplete =
throw new Error(`Document ${envelope.id} has no envelope items`); envelope.recipients.some((recipient) => recipient.signingStatus === SigningStatus.REJECTED) ||
} envelope.recipients.every((recipient) => recipient.signingStatus === SigningStatus.SIGNED);
const recipients = await prisma.recipient.findMany({ if (!isComplete) {
where: { throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
envelopeId: envelope.id, message: 'Document is not complete',
role: { });
not: RecipientRole.CC, }
},
},
});
// Determine if the document has been rejected by checking if any recipient has rejected it let envelopeItems = envelope.envelopeItems;
const rejectedRecipient = recipients.find(
(recipient) => recipient.signingStatus === SigningStatus.REJECTED,
);
const isRejected = Boolean(rejectedRecipient); if (envelopeItems.length < 1) {
throw new Error(`Document ${envelope.id} has no envelope items`);
}
// Get the rejection reason from the rejected recipient const recipients = await prisma.recipient.findMany({
const rejectionReason = rejectedRecipient?.rejectionReason ?? '';
const fields = await prisma.field.findMany({
where: {
envelopeId: envelope.id,
},
include: {
signature: true,
},
});
// Skip the field check if the document is rejected
if (!isRejected && fieldsContainUnsignedRequiredField(fields)) {
throw new Error(`Document ${envelope.id} has unsigned required fields`);
}
if (isResealing) {
// If we're resealing we want to use the initial data for the document
// so we aren't placing fields on top of eachother.
envelopeItems = envelopeItems.map((envelopeItem) => ({
...envelopeItem,
documentData: {
...envelopeItem.documentData,
data: envelopeItem.documentData.initialData,
},
}));
}
if (!envelope.qrToken) {
await prisma.envelope.update({
where: { where: {
id: envelope.id, envelopeId: envelope.id,
}, role: {
data: { not: RecipientRole.CC,
qrToken: prefixedId('qr'), },
}, },
}); });
}
const legacyDocumentId = mapSecondaryIdToDocumentId(envelope.secondaryId); // Determine if the document has been rejected by checking if any recipient has rejected it
const rejectedRecipient = recipients.find(
(recipient) => recipient.signingStatus === SigningStatus.REJECTED,
);
const { certificateData, auditLogData } = await getCertificateAndAuditLogData({ const isRejected = Boolean(rejectedRecipient);
legacyDocumentId,
documentMeta: envelope.documentMeta,
settings,
});
// !: The commented out code is our desired implementation but we're seemingly // Get the rejection reason from the rejected recipient
// !: running into issues with inngest parallelism in production. const rejectionReason = rejectedRecipient?.rejectionReason ?? '';
// !: 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 fields = await prisma.field.findMany({
// const task = io.runTask(`decorate-${envelopeItem.id}`, async () => { where: {
// const envelopeItemFields = envelope.envelopeItems.find( envelopeId: envelope.id,
// (item) => item.id === envelopeItem.id, },
// )?.field; include: {
signature: true,
},
});
// if (!envelopeItemFields) { // Skip the field check if the document is rejected
// throw new Error(`Envelope item fields not found for envelope item ${envelopeItem.id}`); if (!isRejected && fieldsContainUnsignedRequiredField(fields)) {
// } throw new Error(`Document ${envelope.id} has unsigned required fields`);
}
// return decorateAndSignPdf({ if (isResealing) {
// envelope, // If we're resealing we want to use the initial data for the document
// envelopeItem, // so we aren't placing fields on top of eachother.
// envelopeItemFields, envelopeItems = envelopeItems.map((envelopeItem) => ({
// isRejected, ...envelopeItem,
// rejectionReason, documentData: {
// certificateData, ...envelopeItem.documentData,
// auditLogData, data: envelopeItem.documentData.initialData,
// }); },
// }); }));
}
// decoratePromises.push(task); if (!envelope.qrToken) {
// } await prisma.envelope.update({
where: {
id: envelope.id,
},
data: {
qrToken: prefixedId('qr'),
},
});
}
// const newDocumentData = await Promise.all(decoratePromises); const legacyDocumentId = mapSecondaryIdToDocumentId(envelope.secondaryId);
// TODO: Remove once parallelization is working const { certificateData, auditLogData } = await getCertificateAndAuditLogData({
const newDocumentData: Array<{ oldDocumentDataId: string; newDocumentDataId: string }> = []; legacyDocumentId,
documentMeta: envelope.documentMeta,
settings,
});
for (const envelopeItem of envelopeItems) { const newDocumentData: Array<{ oldDocumentDataId: string; newDocumentDataId: string }> = [];
const result = await io.runTask(`decorate-${envelopeItem.id}`, async () => {
for (const envelopeItem of envelopeItems) {
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,

View File

@ -31,16 +31,26 @@ export const viewedDocument = async ({
type: EnvelopeType.DOCUMENT, type: EnvelopeType.DOCUMENT,
}, },
}, },
include: {
envelope: {
include: {
documentMeta: true,
recipients: true,
},
},
},
}); });
if (!recipient) { if (!recipient) {
return; return;
} }
const { envelope } = recipient;
await prisma.documentAuditLog.create({ await prisma.documentAuditLog.create({
data: createDocumentAuditLogData({ data: createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_VIEWED, type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_VIEWED,
envelopeId: recipient.envelopeId, envelopeId: envelope.id,
user: { user: {
name: recipient.name, name: recipient.name,
email: recipient.email, email: recipient.email,
@ -76,7 +86,7 @@ export const viewedDocument = async ({
await tx.documentAuditLog.create({ await tx.documentAuditLog.create({
data: createDocumentAuditLogData({ data: createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_OPENED, type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_OPENED,
envelopeId: recipient.envelopeId, envelopeId: envelope.id,
user: { user: {
name: recipient.name, name: recipient.name,
email: recipient.email, email: recipient.email,
@ -93,16 +103,6 @@ export const viewedDocument = async ({
}); });
}); });
const envelope = await prisma.envelope.findUniqueOrThrow({
where: {
id: recipient.envelopeId,
},
include: {
documentMeta: true,
recipients: true,
},
});
await triggerWebhook({ await triggerWebhook({
event: WebhookTriggerEvents.DOCUMENT_OPENED, event: WebhookTriggerEvents.DOCUMENT_OPENED,
data: ZWebhookDocumentSchema.parse(mapEnvelopeToWebhookDocumentPayload(envelope)), data: ZWebhookDocumentSchema.parse(mapEnvelopeToWebhookDocumentPayload(envelope)),