mirror of
https://github.com/documenso/documenso.git
synced 2025-11-11 21:12:48 +10:00
This PR is handles the changes required to support envelopes. The new envelope editor/signing page will be hidden during release. The core changes here is to migrate the documents and templates model to a centralized envelopes model. Even though Documents and Templates are removed, from the user perspective they will still exist as we remap envelopes to documents and templates.
139 lines
4.1 KiB
TypeScript
139 lines
4.1 KiB
TypeScript
import { DocumentDataType, EnvelopeType } from '@prisma/client';
|
|
|
|
import { getServerLimits } from '@documenso/ee/server-only/limits/server';
|
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import { createDocumentData } from '@documenso/lib/server-only/document-data/create-document-data';
|
|
import { createEnvelope } from '@documenso/lib/server-only/envelope/create-envelope';
|
|
import { getPresignPostUrl } from '@documenso/lib/universal/upload/server-actions';
|
|
import { mapSecondaryIdToDocumentId } from '@documenso/lib/utils/envelope';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { authenticatedProcedure } from '../trpc';
|
|
import {
|
|
ZCreateDocumentTemporaryRequestSchema,
|
|
ZCreateDocumentTemporaryResponseSchema,
|
|
createDocumentTemporaryMeta,
|
|
} from './create-document-temporary.types';
|
|
|
|
/**
|
|
* Temporariy endpoint for V2 Beta until we allow passthrough documents on create.
|
|
*
|
|
* @public
|
|
* @deprecated
|
|
*/
|
|
export const createDocumentTemporaryRoute = authenticatedProcedure
|
|
.meta(createDocumentTemporaryMeta)
|
|
.input(ZCreateDocumentTemporaryRequestSchema)
|
|
.output(ZCreateDocumentTemporaryResponseSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
const { teamId, user } = ctx;
|
|
|
|
const {
|
|
title,
|
|
externalId,
|
|
visibility,
|
|
globalAccessAuth,
|
|
globalActionAuth,
|
|
recipients,
|
|
meta,
|
|
folderId,
|
|
} = input;
|
|
|
|
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 fileName = title.endsWith('.pdf') ? title : `${title}.pdf`;
|
|
|
|
const { url, key } = await getPresignPostUrl(fileName, 'application/pdf');
|
|
|
|
const documentData = await createDocumentData({
|
|
data: key,
|
|
type: DocumentDataType.S3_PATH,
|
|
});
|
|
|
|
const createdEnvelope = await createEnvelope({
|
|
userId: ctx.user.id,
|
|
teamId,
|
|
normalizePdf: false, // Not normalizing because of presigned URL.
|
|
internalVersion: 1,
|
|
data: {
|
|
type: EnvelopeType.DOCUMENT,
|
|
title,
|
|
externalId,
|
|
visibility,
|
|
globalAccessAuth,
|
|
globalActionAuth,
|
|
recipients: (recipients || []).map((recipient) => ({
|
|
...recipient,
|
|
fields: (recipient.fields || []).map((field) => ({
|
|
...field,
|
|
page: field.pageNumber,
|
|
positionX: field.pageX,
|
|
positionY: field.pageY,
|
|
documentDataId: documentData.id,
|
|
})),
|
|
})),
|
|
folderId,
|
|
envelopeItems: [
|
|
{
|
|
documentDataId: documentData.id,
|
|
},
|
|
],
|
|
},
|
|
meta,
|
|
requestMetadata: ctx.metadata,
|
|
});
|
|
|
|
const envelopeItems = await prisma.envelopeItem.findMany({
|
|
where: {
|
|
envelopeId: createdEnvelope.id,
|
|
},
|
|
include: {
|
|
documentData: true,
|
|
},
|
|
});
|
|
|
|
const legacyDocumentId = mapSecondaryIdToDocumentId(createdEnvelope.secondaryId);
|
|
|
|
const firstDocumentData = envelopeItems[0].documentData;
|
|
|
|
if (!firstDocumentData) {
|
|
throw new Error('Document data not found');
|
|
}
|
|
|
|
return {
|
|
document: {
|
|
...createdEnvelope,
|
|
envelopeId: createdEnvelope.id,
|
|
documentDataId: firstDocumentData.id,
|
|
documentData: {
|
|
...firstDocumentData,
|
|
envelopeItemId: envelopeItems[0].id,
|
|
},
|
|
documentMeta: {
|
|
...createdEnvelope.documentMeta,
|
|
documentId: legacyDocumentId,
|
|
},
|
|
id: legacyDocumentId,
|
|
fields: createdEnvelope.fields.map((field) => ({
|
|
...field,
|
|
documentId: legacyDocumentId,
|
|
templateId: null,
|
|
})),
|
|
recipients: createdEnvelope.recipients.map((recipient) => ({
|
|
...recipient,
|
|
documentId: legacyDocumentId,
|
|
templateId: null,
|
|
})),
|
|
},
|
|
folder: createdEnvelope.folder, // Todo: Remove this prior to api-v2 release.
|
|
uploadUrl: url,
|
|
};
|
|
});
|