Files
sign/packages/lib/server-only/template/create-document-from-template.ts

142 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-10-06 22:54:24 +00:00
import { nanoid } from '@documenso/lib/universal/id';
import { prisma } from '@documenso/prisma';
2024-02-20 19:46:18 +11:00
import type { RecipientRole } from '@documenso/prisma/client';
2023-10-06 22:54:24 +00:00
2024-02-20 19:46:18 +11:00
export type CreateDocumentFromTemplateOptions = {
templateId: number;
2023-10-06 22:54:24 +00:00
userId: number;
2024-02-22 13:39:34 +11:00
teamId?: number;
2024-02-20 19:46:18 +11:00
recipients?: {
name?: string;
email: string;
role?: RecipientRole;
}[];
2023-10-06 22:54:24 +00:00
};
export const createDocumentFromTemplate = async ({
templateId,
userId,
2024-02-22 13:39:34 +11:00
teamId,
2024-02-20 19:46:18 +11:00
recipients,
2023-10-06 22:54:24 +00:00
}: CreateDocumentFromTemplateOptions) => {
const template = await prisma.template.findUnique({
2024-02-08 12:33:20 +11:00
where: {
id: templateId,
2024-02-22 13:39:34 +11:00
...(teamId
? {
team: {
id: teamId,
members: {
some: {
userId,
},
2024-02-08 12:33:20 +11:00
},
},
2024-02-22 13:39:34 +11:00
}
: {
userId,
teamId: null,
}),
2024-02-08 12:33:20 +11:00
},
2023-10-06 22:54:24 +00:00
include: {
Recipient: true,
Field: true,
templateDocumentData: true,
},
});
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,
},
});
const document = await prisma.document.create({
data: {
userId,
2024-02-08 12:33:20 +11:00
teamId: template.teamId,
2023-10-06 22:54:24 +00:00
title: template.title,
documentDataId: documentData.id,
Recipient: {
create: template.Recipient.map((recipient) => ({
email: recipient.email,
name: recipient.name,
role: recipient.role,
2023-10-06 22:54:24 +00:00
token: nanoid(),
})),
},
},
include: {
2024-02-20 19:46:18 +11:00
Recipient: {
orderBy: {
id: 'asc',
},
},
documentData: true,
2023-10-06 22:54:24 +00:00
},
});
await prisma.field.createMany({
data: template.Field.map((field) => {
const recipient = template.Recipient.find((recipient) => recipient.id === field.recipientId);
2023-12-15 22:07:27 +11:00
const documentRecipient = document.Recipient.find((doc) => doc.email === recipient?.email);
2023-10-06 22:54:24 +00:00
2024-04-18 21:56:31 +07:00
if (!documentRecipient) {
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,
documentId: document.id,
2024-04-18 21:56:31 +07:00
recipientId: documentRecipient.id,
2023-10-06 22:54:24 +00:00
};
}),
});
2024-02-20 19:46:18 +11:00
if (recipients && recipients.length > 0) {
document.Recipient = await Promise.all(
recipients.map(async (recipient, index) => {
const existingRecipient = document.Recipient.at(index);
return await prisma.recipient.upsert({
where: {
documentId_email: {
documentId: document.id,
email: existingRecipient?.email ?? recipient.email,
},
},
update: {
name: recipient.name,
email: recipient.email,
role: recipient.role,
},
create: {
documentId: document.id,
email: recipient.email,
name: recipient.name,
role: recipient.role,
token: nanoid(),
},
});
}),
);
}
2023-10-06 22:54:24 +00:00
return document;
};