diff --git a/packages/api/v1/schema.ts b/packages/api/v1/schema.ts index be0ea1271..b26eaf7da 100644 --- a/packages/api/v1/schema.ts +++ b/packages/api/v1/schema.ts @@ -100,13 +100,21 @@ export type TCreateDocumentMutationResponseSchema = z.infer< export const ZCreateDocumentFromTemplateMutationSchema = z.object({ title: z.string().min(1), - recipients: z.array( - z.object({ - name: z.string().min(1), - email: z.string().email().min(1), - role: z.nativeEnum(RecipientRole).optional().default(RecipientRole.SIGNER), - }), - ), + recipients: z.union([ + z.array( + z.object({ + id: z.number(), + name: z.string().min(1), + email: z.string().email().min(1), + }), + ), + z.array( + z.object({ + name: z.string().min(1), + email: z.string().email().min(1), + }), + ), + ]), meta: z .object({ subject: z.string(), diff --git a/packages/lib/server-only/template/create-document-from-template.ts b/packages/lib/server-only/template/create-document-from-template.ts index 08363434f..68790f6fb 100644 --- a/packages/lib/server-only/template/create-document-from-template.ts +++ b/packages/lib/server-only/template/create-document-from-template.ts @@ -1,15 +1,23 @@ import { nanoid } from '@documenso/lib/universal/id'; import { prisma } from '@documenso/prisma'; +import type { Recipient } from '@documenso/prisma/client'; + +type RecipientWithId = { + id: number; + name?: string; + email: string; +}; export type CreateDocumentFromTemplateOptions = { templateId: number; userId: number; teamId?: number; - recipients: { - id: number; - name?: string; - email: string; - }[]; + recipients: + | RecipientWithId[] + | { + name?: string; + email: string; + }[]; }; export const createDocumentFromTemplate = async ({ @@ -48,19 +56,38 @@ export const createDocumentFromTemplate = async ({ throw new Error('Template not found.'); } - const finalRecipients = template.Recipient.map((templateRecipient) => { - const foundRecipient = recipients.find((recipient) => recipient.id === templateRecipient.id); + if (recipients.length === 0 || recipients.length !== template.Recipient.length) { + throw new Error('Invalid number of recipients.'); + } - if (!foundRecipient) { - throw new Error('Recipient not found.'); - } + let finalRecipients: Pick[] = []; - return { - name: foundRecipient.name, - email: foundRecipient.email, - role: templateRecipient.role, - }; - }); + if (Object.prototype.hasOwnProperty.call(recipients[0], 'id')) { + finalRecipients = template.Recipient.map((templateRecipient) => { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + const foundRecipient = (recipients as RecipientWithId[]).find( + (recipient) => recipient.id === templateRecipient.id, + ); + + if (!foundRecipient) { + throw new Error('Recipient not found.'); + } + + return { + name: foundRecipient.name ?? '', + email: foundRecipient.email, + role: templateRecipient.role, + }; + }); + } else { + // Backwards compatible logic for /v1/ API where we use the index to associate + // the provided recipient with the template recipient. + finalRecipients = recipients.map((recipient, index) => ({ + name: recipient.name ?? '', + email: recipient.email, + role: template.Recipient[index].role, + })); + } const documentData = await prisma.documentData.create({ data: {