From a7adb77e4775de83e342aec0de9121c5be9e6963 Mon Sep 17 00:00:00 2001 From: Catalin Pit <25515812+catalinpit@users.noreply.github.com> Date: Fri, 7 Feb 2025 18:08:14 +0200 Subject: [PATCH] chore: allow document creation from template --- .../template/create-document-from-template.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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 470e909e5..cde04b1c0 100644 --- a/packages/lib/server-only/template/create-document-from-template.ts +++ b/packages/lib/server-only/template/create-document-from-template.ts @@ -256,10 +256,20 @@ export const createDocumentFromTemplate = async ({ }, }); + const recipientMapping = new Map(); + + template.recipients.forEach((templateRecipient, index) => { + const documentRecipient = document.recipients[index]; + if (documentRecipient) { + recipientMapping.set(templateRecipient.id, documentRecipient.id); + } + }); + let fieldsToCreate: Omit[] = []; - Object.values(finalRecipients).forEach(({ email, fields }) => { - const recipient = document.recipients.find((recipient) => recipient.email === email); + finalRecipients.forEach(({ templateRecipientId, fields }) => { + const documentRecipientId = recipientMapping.get(templateRecipientId); + const recipient = document.recipients.find((r) => r.id === documentRecipientId); if (!recipient) { throw new Error('Recipient not found.');