mirror of
https://github.com/documenso/documenso.git
synced 2026-07-06 11:05:00 +10:00
f3f5903760
Merge origin/main into feat/document-file-conversion. Conflicts were format-only (Tailwind class ordering, single-line vs multi-line) plus two semantic merges: - files.helpers.ts: combine main's pending-PDF download path with the branch's original-source-file (DOCX/PNG/JPEG) download path - download-pdf.ts: combine main's versionToFilenameSuffix helper with the branch's server-provided Content-Disposition filename support
129 lines
3.6 KiB
TypeScript
129 lines
3.6 KiB
TypeScript
import { getServerLimits } from '@documenso/ee/server-only/limits/server';
|
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import { createEnvelope } from '@documenso/lib/server-only/envelope/create-envelope';
|
|
import { convertToPdfIfNeeded } from '@documenso/lib/server-only/file-conversion/convert-to-pdf';
|
|
import { insertFormValuesInPdf } from '@documenso/lib/server-only/pdf/insert-form-values-in-pdf';
|
|
import { putFileServerSide, putNormalizedPdfFileServerSide } from '@documenso/lib/universal/upload/put-file.server';
|
|
import { mapSecondaryIdToDocumentId } from '@documenso/lib/utils/envelope';
|
|
import { EnvelopeType } from '@prisma/client';
|
|
|
|
import { authenticatedProcedure } from '../trpc';
|
|
import {
|
|
createDocumentMeta,
|
|
ZCreateDocumentRequestSchema,
|
|
ZCreateDocumentResponseSchema,
|
|
} from './create-document.types';
|
|
|
|
export const createDocumentRoute = authenticatedProcedure
|
|
.meta(createDocumentMeta)
|
|
.input(ZCreateDocumentRequestSchema)
|
|
.output(ZCreateDocumentResponseSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
const { user, teamId } = ctx;
|
|
|
|
const { payload, file } = input;
|
|
|
|
const {
|
|
title,
|
|
externalId,
|
|
visibility,
|
|
globalAccessAuth,
|
|
globalActionAuth,
|
|
recipients,
|
|
meta,
|
|
folderId,
|
|
formValues,
|
|
attachments,
|
|
} = payload;
|
|
|
|
const { pdfBuffer, originalBuffer, originalMimeType } = await convertToPdfIfNeeded(file);
|
|
|
|
let pdf = pdfBuffer;
|
|
|
|
if (formValues) {
|
|
// eslint-disable-next-line require-atomic-updates
|
|
pdf = await insertFormValuesInPdf({
|
|
pdf,
|
|
formValues,
|
|
});
|
|
}
|
|
|
|
let originalData: string | undefined;
|
|
if (originalBuffer) {
|
|
const stored = await putFileServerSide({
|
|
name: `original-${file.name}`,
|
|
type: originalMimeType,
|
|
arrayBuffer: async () => Promise.resolve(originalBuffer),
|
|
});
|
|
originalData = stored.data;
|
|
}
|
|
|
|
const { id: documentDataId } = await putNormalizedPdfFileServerSide({
|
|
file: {
|
|
name: file.name,
|
|
type: 'application/pdf',
|
|
arrayBuffer: async () => Promise.resolve(pdf),
|
|
},
|
|
originalData,
|
|
originalMimeType,
|
|
});
|
|
|
|
ctx.logger.info({
|
|
input: {
|
|
folderId,
|
|
},
|
|
});
|
|
|
|
const { remaining } = await getServerLimits({ userId: user.id, 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,
|
|
});
|
|
}
|
|
|
|
const document = await createEnvelope({
|
|
userId: user.id,
|
|
teamId,
|
|
internalVersion: 1,
|
|
data: {
|
|
type: EnvelopeType.DOCUMENT,
|
|
title,
|
|
externalId,
|
|
visibility,
|
|
globalAccessAuth,
|
|
globalActionAuth,
|
|
formValues,
|
|
recipients: (recipients || []).map((recipient) => ({
|
|
...recipient,
|
|
fields: (recipient.fields || []).map((field) => ({
|
|
...field,
|
|
page: field.pageNumber,
|
|
positionX: field.pageX,
|
|
positionY: field.pageY,
|
|
documentDataId,
|
|
})),
|
|
})),
|
|
folderId,
|
|
envelopeItems: [
|
|
{
|
|
// If you ever allow more than 1 in this endpoint, make sure to use `maximumEnvelopeItemCount` to limit it.
|
|
documentDataId,
|
|
},
|
|
],
|
|
},
|
|
attachments,
|
|
meta: {
|
|
...meta,
|
|
emailSettings: meta?.emailSettings ?? undefined,
|
|
},
|
|
requestMetadata: ctx.metadata,
|
|
});
|
|
|
|
return {
|
|
envelopeId: document.id,
|
|
id: mapSecondaryIdToDocumentId(document.secondaryId),
|
|
};
|
|
});
|