Files
sign/packages/lib/server-only/template/duplicate-template.ts

117 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-01-02 15:33:37 +11:00
import type { Prisma } from '@prisma/client';
2024-11-01 19:29:38 +09:00
import { omit } from 'remeda';
2023-10-06 22:54:24 +00:00
import { nanoid } from '@documenso/lib/universal/id';
import { prisma } from '@documenso/prisma';
2023-12-15 22:07:27 +11:00
import type { TDuplicateTemplateMutationSchema } from '@documenso/trpc/server/template-router/schema';
2023-10-06 22:54:24 +00:00
export type DuplicateTemplateOptions = TDuplicateTemplateMutationSchema & {
userId: number;
teamId?: number;
2023-10-06 22:54:24 +00:00
};
2024-02-08 12:33:20 +11:00
export const duplicateTemplate = async ({
templateId,
userId,
teamId,
}: DuplicateTemplateOptions) => {
const template = await prisma.template.findUnique({
where: {
2024-02-08 12:33:20 +11:00
id: templateId,
...(teamId
? {
team: {
id: teamId,
members: {
some: {
userId,
},
},
},
}
: {
2024-02-08 12:33:20 +11:00
userId,
teamId: null,
}),
},
2023-10-06 22:54:24 +00:00
include: {
2025-01-13 13:41:53 +11:00
recipients: true,
fields: true,
2023-10-06 22:54:24 +00:00
templateDocumentData: true,
2024-11-01 19:29:38 +09:00
templateMeta: true,
2023-10-06 22:54:24 +00:00
},
});
if (!template) {
throw new Error('Template not found.');
}
const documentData = await prisma.documentData.create({
data: {
type: template.templateDocumentData.type,
data: template.templateDocumentData.data,
initialData: template.templateDocumentData.initialData,
},
});
2024-11-01 19:29:38 +09:00
let templateMeta: Prisma.TemplateCreateArgs['data']['templateMeta'] | undefined = undefined;
if (template.templateMeta) {
templateMeta = {
create: {
...omit(template.templateMeta, ['id', 'templateId']),
emailSettings: template.templateMeta.emailSettings || undefined,
},
2024-11-01 19:29:38 +09:00
};
}
2023-10-06 22:54:24 +00:00
const duplicatedTemplate = await prisma.template.create({
data: {
userId,
2024-02-08 12:33:20 +11:00
teamId,
2023-10-06 22:54:24 +00:00
title: template.title + ' (copy)',
templateDocumentDataId: documentData.id,
2025-01-13 13:41:53 +11:00
recipients: {
create: template.recipients.map((recipient) => ({
2023-10-06 22:54:24 +00:00
email: recipient.email,
name: recipient.name,
token: nanoid(),
})),
},
2024-11-01 19:29:38 +09:00
templateMeta,
2023-10-06 22:54:24 +00:00
},
include: {
2025-01-13 13:41:53 +11:00
recipients: true,
2023-10-06 22:54:24 +00:00
},
});
await prisma.field.createMany({
2025-01-13 13:41:53 +11:00
data: template.fields.map((field) => {
const recipient = template.recipients.find((recipient) => recipient.id === field.recipientId);
2023-10-06 22:54:24 +00:00
2025-01-13 13:41:53 +11:00
const duplicatedTemplateRecipient = duplicatedTemplate.recipients.find(
2023-12-15 22:07:27 +11:00
(doc) => doc.email === recipient?.email,
2023-10-06 22:54:24 +00:00
);
2024-04-18 21:56:31 +07:00
if (!duplicatedTemplateRecipient) {
throw new Error('Recipient not found.');
}
2023-10-06 22:54:24 +00:00
return {
type: field.type,
page: field.page,
positionX: field.positionX,
positionY: field.positionY,
width: field.width,
height: field.height,
customText: field.customText,
inserted: field.inserted,
templateId: duplicatedTemplate.id,
2024-04-18 21:56:31 +07:00
recipientId: duplicatedTemplateRecipient.id,
2023-10-06 22:54:24 +00:00
};
}),
});
return duplicatedTemplate;
};