mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +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.
197 lines
5.5 KiB
TypeScript
197 lines
5.5 KiB
TypeScript
import { DocumentSource, EnvelopeType, WebhookTriggerEvents } from '@prisma/client';
|
|
import { omit } from 'remeda';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
|
import {
|
|
ZWebhookDocumentSchema,
|
|
mapEnvelopeToWebhookDocumentPayload,
|
|
} from '../../types/webhook-payload';
|
|
import { nanoid, prefixedId } from '../../universal/id';
|
|
import type { EnvelopeIdOptions } from '../../utils/envelope';
|
|
import { getEnvelopeWhereInput } from '../envelope/get-envelope-by-id';
|
|
import { incrementDocumentId, incrementTemplateId } from '../envelope/increment-id';
|
|
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
|
|
|
export interface DuplicateEnvelopeOptions {
|
|
id: EnvelopeIdOptions;
|
|
userId: number;
|
|
teamId: number;
|
|
}
|
|
|
|
export const duplicateEnvelope = async ({ id, userId, teamId }: DuplicateEnvelopeOptions) => {
|
|
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
|
id,
|
|
type: null,
|
|
userId,
|
|
teamId,
|
|
});
|
|
|
|
const envelope = await prisma.envelope.findFirst({
|
|
where: envelopeWhereInput,
|
|
select: {
|
|
type: true,
|
|
title: true,
|
|
userId: true,
|
|
internalVersion: true,
|
|
envelopeItems: {
|
|
include: {
|
|
documentData: {
|
|
select: {
|
|
data: true,
|
|
initialData: true,
|
|
type: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
authOptions: true,
|
|
visibility: true,
|
|
documentMeta: true,
|
|
recipients: {
|
|
select: {
|
|
email: true,
|
|
name: true,
|
|
role: true,
|
|
signingOrder: true,
|
|
fields: true,
|
|
},
|
|
},
|
|
teamId: true,
|
|
},
|
|
});
|
|
|
|
if (!envelope) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Document not found',
|
|
});
|
|
}
|
|
|
|
const { legacyNumberId, secondaryId } =
|
|
envelope.type === EnvelopeType.DOCUMENT
|
|
? await incrementDocumentId().then(({ documentId, formattedDocumentId }) => ({
|
|
legacyNumberId: documentId,
|
|
secondaryId: formattedDocumentId,
|
|
}))
|
|
: await incrementTemplateId().then(({ templateId, formattedTemplateId }) => ({
|
|
legacyNumberId: templateId,
|
|
secondaryId: formattedTemplateId,
|
|
}));
|
|
|
|
const createdDocumentMeta = await prisma.documentMeta.create({
|
|
data: {
|
|
...omit(envelope.documentMeta, ['id']),
|
|
emailSettings: envelope.documentMeta.emailSettings || undefined,
|
|
},
|
|
});
|
|
|
|
const duplicatedEnvelope = await prisma.envelope.create({
|
|
data: {
|
|
id: prefixedId('envelope'),
|
|
secondaryId,
|
|
type: envelope.type,
|
|
internalVersion: envelope.internalVersion,
|
|
userId,
|
|
teamId,
|
|
title: envelope.title + ' (copy)',
|
|
documentMetaId: createdDocumentMeta.id,
|
|
authOptions: envelope.authOptions || undefined,
|
|
visibility: envelope.visibility,
|
|
source:
|
|
envelope.type === EnvelopeType.DOCUMENT ? DocumentSource.DOCUMENT : DocumentSource.TEMPLATE,
|
|
},
|
|
include: {
|
|
recipients: true,
|
|
documentMeta: true,
|
|
},
|
|
});
|
|
|
|
// Key = original envelope item ID
|
|
// Value = duplicated envelope item ID.
|
|
const oldEnvelopeItemToNewEnvelopeItemIdMap: Record<string, string> = {};
|
|
|
|
// Duplicate the envelope items.
|
|
await Promise.all(
|
|
envelope.envelopeItems.map(async (envelopeItem) => {
|
|
const duplicatedDocumentData = await prisma.documentData.create({
|
|
data: {
|
|
type: envelopeItem.documentData.type,
|
|
data: envelopeItem.documentData.initialData,
|
|
initialData: envelopeItem.documentData.initialData,
|
|
},
|
|
});
|
|
|
|
const duplicatedEnvelopeItem = await prisma.envelopeItem.create({
|
|
data: {
|
|
id: prefixedId('envelope_item'),
|
|
title: envelopeItem.title,
|
|
order: envelopeItem.order,
|
|
envelopeId: duplicatedEnvelope.id,
|
|
documentDataId: duplicatedDocumentData.id,
|
|
},
|
|
});
|
|
|
|
oldEnvelopeItemToNewEnvelopeItemIdMap[envelopeItem.id] = duplicatedEnvelopeItem.id;
|
|
}),
|
|
);
|
|
|
|
for (const recipient of envelope.recipients) {
|
|
await prisma.recipient.create({
|
|
data: {
|
|
envelopeId: duplicatedEnvelope.id,
|
|
email: recipient.email,
|
|
name: recipient.name,
|
|
role: recipient.role,
|
|
signingOrder: recipient.signingOrder,
|
|
token: nanoid(),
|
|
fields: {
|
|
createMany: {
|
|
data: recipient.fields.map((field) => ({
|
|
envelopeId: duplicatedEnvelope.id,
|
|
envelopeItemId: oldEnvelopeItemToNewEnvelopeItemIdMap[field.envelopeItemId],
|
|
type: field.type,
|
|
page: field.page,
|
|
positionX: field.positionX,
|
|
positionY: field.positionY,
|
|
width: field.width,
|
|
height: field.height,
|
|
customText: '',
|
|
inserted: false,
|
|
fieldMeta: field.fieldMeta as PrismaJson.FieldMeta,
|
|
})),
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
if (duplicatedEnvelope.type === EnvelopeType.DOCUMENT) {
|
|
const refetchedEnvelope = await prisma.envelope.findFirstOrThrow({
|
|
where: {
|
|
id: duplicatedEnvelope.id,
|
|
},
|
|
include: {
|
|
documentMeta: true,
|
|
recipients: true,
|
|
},
|
|
});
|
|
|
|
await triggerWebhook({
|
|
event: WebhookTriggerEvents.DOCUMENT_CREATED,
|
|
data: ZWebhookDocumentSchema.parse(mapEnvelopeToWebhookDocumentPayload(refetchedEnvelope)),
|
|
userId: userId,
|
|
teamId: teamId,
|
|
});
|
|
}
|
|
|
|
return {
|
|
id: duplicatedEnvelope.id,
|
|
envelope: duplicatedEnvelope,
|
|
legacyId: {
|
|
type: envelope.type,
|
|
id: legacyNumberId,
|
|
},
|
|
};
|
|
};
|