From 39bd3e58809d89e4485f5f900ac1bb39746f1699 Mon Sep 17 00:00:00 2001 From: David Nguyen Date: Thu, 25 Apr 2024 22:28:41 +0700 Subject: [PATCH] fix: enhancements --- .../templates/use-template-dialog.tsx | 97 ++++++++++--------- .../template/create-document-from-template.ts | 4 +- .../trpc/server/template-router/router.ts | 4 +- 3 files changed, 57 insertions(+), 48 deletions(-) diff --git a/apps/web/src/app/(dashboard)/templates/use-template-dialog.tsx b/apps/web/src/app/(dashboard)/templates/use-template-dialog.tsx index 7ddc1f90d..8eafcbb24 100644 --- a/apps/web/src/app/(dashboard)/templates/use-template-dialog.tsx +++ b/apps/web/src/app/(dashboard)/templates/use-template-dialog.tsx @@ -98,8 +98,8 @@ export function UseTemplateDialog({ sendDocument: false, recipients: recipients.map((recipient) => ({ id: recipient.id, - name: recipient.name, - email: recipient.email, + name: '', + email: '', })), }, }); @@ -156,15 +156,16 @@ export function UseTemplateDialog({ Create document from template - Add the recipients to create the document with + + {recipients.length === 0 + ? 'A draft document will be created' + : 'Add the recipients to create the document with'} +
-
+
{formRecipients.map((recipient, index) => (
@@ -201,46 +202,48 @@ export function UseTemplateDialog({ ))}
-
- ( - -
- + {recipients.length > 0 && ( +
+ ( + +
+ -
-
- )} - /> -
+

Otherwise, the document will be created as a draft.

+ + + +
+
+ )} + /> +
+ )} @@ -250,7 +253,11 @@ export function UseTemplateDialog({
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 68790f6fb..2a83e17e9 100644 --- a/packages/lib/server-only/template/create-document-from-template.ts +++ b/packages/lib/server-only/template/create-document-from-template.ts @@ -56,13 +56,13 @@ export const createDocumentFromTemplate = async ({ throw new Error('Template not found.'); } - if (recipients.length === 0 || recipients.length !== template.Recipient.length) { + if (recipients.length !== template.Recipient.length) { throw new Error('Invalid number of recipients.'); } let finalRecipients: Pick[] = []; - if (Object.prototype.hasOwnProperty.call(recipients[0], 'id')) { + if (recipients.length > 0 && 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( diff --git a/packages/trpc/server/template-router/router.ts b/packages/trpc/server/template-router/router.ts index 19f1c50f1..07917d600 100644 --- a/packages/trpc/server/template-router/router.ts +++ b/packages/trpc/server/template-router/router.ts @@ -66,7 +66,9 @@ export const templateRouter = router({ userId: ctx.user.id, teamId, requestMetadata: extractNextApiRequestMetadata(ctx.req), - }).catch(() => { + }).catch((err) => { + console.error(err); + throw new AppError('DOCUMENT_SEND_FAILED'); }); }