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 88ac765e6..a16416a44 100644 --- a/packages/lib/server-only/template/create-document-from-template.ts +++ b/packages/lib/server-only/template/create-document-from-template.ts @@ -47,7 +47,11 @@ export const createDocumentFromTemplate = async ({ }, include: { Recipient: true, - Field: true, + Field: { + include: { + Recipient: true, + }, + }, templateDocumentData: true, }, }); @@ -125,12 +129,10 @@ export const createDocumentFromTemplate = async ({ await tx.field.createMany({ data: template.Field.map((field) => { - const recipient = template.Recipient.find( - (recipient) => recipient.id === field.recipientId, + const documentRecipient = document.Recipient.find( + (recipient) => recipient.email === field.Recipient.email, ); - const documentRecipient = document.Recipient.find((doc) => doc.email === recipient?.email); - if (!documentRecipient) { throw new Error('Recipient not found.'); }