mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
The product is branded Hanzo eSign (esign.hanzo.ai, repo hanzoai/esign). Rename all workspace packages @hanzo/sign-* → @hanzo/esign-* (1052 files) and align docker/Dockerfile turbo --scope/--filter to the actual remix package name @hanzo/esign (was a pre-existing mismatch with -remix that broke the build). Protected deps (@hanzo/insights, @hanzo/logo) untouched.
180 lines
4.8 KiB
TypeScript
180 lines
4.8 KiB
TypeScript
import { EnvelopeType } from '@prisma/client';
|
|
|
|
import { getServerLimits } from '@hanzo/esign-lib/server-only/limits/server';
|
|
import { AppError, AppErrorCode } from '@hanzo/esign-lib/errors/app-error';
|
|
import { createEnvelope } from '@hanzo/esign-lib/server-only/envelope/create-envelope';
|
|
import { extractPdfPlaceholders } from '@hanzo/esign-lib/server-only/pdf/auto-place-fields';
|
|
import { normalizePdf } from '@hanzo/esign-lib/server-only/pdf/normalize-pdf';
|
|
import type { ApiRequestMetadata } from '@hanzo/esign-lib/universal/extract-request-metadata';
|
|
import { putPdfFileServerSide } from '@hanzo/esign-lib/universal/upload/put-file.server';
|
|
|
|
import { insertFormValuesInPdf } from '../../../lib/server-only/pdf/insert-form-values-in-pdf';
|
|
import type { TCreateEnvelopeRequest } from './create-envelope.types';
|
|
|
|
type CreateEnvelopeRouteOptions = {
|
|
/**
|
|
* Verified user ID.
|
|
*/
|
|
userId: number;
|
|
|
|
/**
|
|
* Unverified team ID.
|
|
*/
|
|
teamId: number;
|
|
input: TCreateEnvelopeRequest;
|
|
apiRequestMetadata: ApiRequestMetadata;
|
|
|
|
options?: {
|
|
bypassDefaultRecipients?: boolean;
|
|
};
|
|
};
|
|
|
|
export const createEnvelopeRouteCaller = async ({
|
|
userId,
|
|
teamId,
|
|
input,
|
|
apiRequestMetadata,
|
|
options = {},
|
|
}: CreateEnvelopeRouteOptions) => {
|
|
const { payload, files } = input;
|
|
|
|
const {
|
|
title,
|
|
type,
|
|
externalId,
|
|
visibility,
|
|
globalAccessAuth,
|
|
globalActionAuth,
|
|
formValues,
|
|
recipients,
|
|
folderId,
|
|
meta,
|
|
attachments,
|
|
delegatedDocumentOwner,
|
|
} = payload;
|
|
|
|
const { remaining, maximumEnvelopeItemCount } = await getServerLimits({
|
|
userId,
|
|
teamId,
|
|
});
|
|
|
|
if (remaining.documents <= 0) {
|
|
throw new AppError(AppErrorCode.LIMIT_EXCEEDED, {
|
|
message: 'You have reached your document limit for this month. Please upgrade your plan.',
|
|
statusCode: 400,
|
|
});
|
|
}
|
|
|
|
if (files.length > maximumEnvelopeItemCount) {
|
|
throw new AppError('ENVELOPE_ITEM_LIMIT_EXCEEDED', {
|
|
message: `You cannot upload more than ${maximumEnvelopeItemCount} envelope items per envelope`,
|
|
statusCode: 400,
|
|
});
|
|
}
|
|
|
|
if (files.some((file) => !file.type.startsWith('application/pdf'))) {
|
|
throw new AppError('INVALID_DOCUMENT_FILE', {
|
|
message: 'You cannot upload non-PDF files',
|
|
statusCode: 400,
|
|
});
|
|
}
|
|
|
|
// For each file: normalize, extract & clean placeholders, then upload.
|
|
const envelopeItems = await Promise.all(
|
|
files.map(async (file) => {
|
|
let pdf = Buffer.from(await file.arrayBuffer());
|
|
|
|
if (formValues) {
|
|
// eslint-disable-next-line require-atomic-updates
|
|
pdf = (await insertFormValuesInPdf({
|
|
pdf,
|
|
formValues,
|
|
})) as Buffer<ArrayBuffer>;
|
|
}
|
|
|
|
const normalized = await normalizePdf(pdf, {
|
|
flattenForm: type !== EnvelopeType.TEMPLATE,
|
|
});
|
|
|
|
// Todo: Embeds - Might need to add this for client-side embeds in the future.
|
|
const { cleanedPdf, placeholders } = await extractPdfPlaceholders(normalized);
|
|
|
|
const { id: documentDataId } = await putPdfFileServerSide({
|
|
name: file.name,
|
|
type: 'application/pdf',
|
|
arrayBuffer: async () => Promise.resolve(cleanedPdf as unknown as ArrayBuffer),
|
|
});
|
|
|
|
return {
|
|
title: file.name,
|
|
documentDataId,
|
|
placeholders,
|
|
};
|
|
}),
|
|
);
|
|
|
|
const recipientsToCreate = recipients?.map((recipient) => ({
|
|
email: recipient.email,
|
|
name: recipient.name,
|
|
role: recipient.role,
|
|
signingOrder: recipient.signingOrder,
|
|
accessAuth: recipient.accessAuth,
|
|
actionAuth: recipient.actionAuth,
|
|
fields: recipient.fields?.map((field) => {
|
|
let documentDataId: string | undefined = undefined;
|
|
|
|
if (typeof field.identifier === 'string') {
|
|
documentDataId = envelopeItems.find(
|
|
(item) => item.title === field.identifier,
|
|
)?.documentDataId;
|
|
}
|
|
|
|
if (typeof field.identifier === 'number') {
|
|
documentDataId = envelopeItems.at(field.identifier)?.documentDataId;
|
|
}
|
|
|
|
if (field.identifier === undefined) {
|
|
documentDataId = envelopeItems.at(0)?.documentDataId;
|
|
}
|
|
|
|
if (!documentDataId) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Document data not found',
|
|
});
|
|
}
|
|
|
|
return {
|
|
...field,
|
|
documentDataId,
|
|
};
|
|
}),
|
|
}));
|
|
|
|
const envelope = await createEnvelope({
|
|
userId,
|
|
teamId,
|
|
internalVersion: 2,
|
|
data: {
|
|
type,
|
|
title,
|
|
externalId,
|
|
formValues,
|
|
visibility,
|
|
globalAccessAuth,
|
|
globalActionAuth,
|
|
recipients: recipientsToCreate,
|
|
folderId,
|
|
envelopeItems,
|
|
delegatedDocumentOwner,
|
|
},
|
|
attachments,
|
|
meta,
|
|
requestMetadata: apiRequestMetadata,
|
|
bypassDefaultRecipients: options.bypassDefaultRecipients,
|
|
});
|
|
|
|
return {
|
|
id: envelope.id,
|
|
};
|
|
};
|