From d0780290b04131706b24c9ec7ec135df1e128add Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Mon, 31 Mar 2025 23:25:03 +1100 Subject: [PATCH] allow text to be nullable (#142) --- .../public-api/api/emails/send-email.ts | 24 ++++++++----------- apps/web/src/server/service/email-service.ts | 7 ++++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/apps/web/src/server/public-api/api/emails/send-email.ts b/apps/web/src/server/public-api/api/emails/send-email.ts index 4d67eaf..41ac37f 100644 --- a/apps/web/src/server/public-api/api/emails/send-email.ts +++ b/apps/web/src/server/public-api/api/emails/send-email.ts @@ -15,24 +15,18 @@ const route = createRoute({ .object({ to: z.string().or(z.array(z.string())), from: z.string(), - subject: z - .string() - .optional() - .openapi({ - description: "Optional when templateId is provided", - }), - templateId: z - .string() - .optional() - .openapi({ - description: "ID of a template from the dashboard", - }), + subject: z.string().optional().openapi({ + description: "Optional when templateId is provided", + }), + templateId: z.string().optional().openapi({ + description: "ID of a template from the dashboard", + }), variables: z.record(z.string()).optional(), replyTo: z.string().or(z.array(z.string())).optional(), cc: z.string().or(z.array(z.string())).optional(), bcc: z.string().or(z.array(z.string())).optional(), - text: z.string().optional(), - html: z.string().optional(), + text: z.string().optional().nullable(), + html: z.string().optional().nullable(), attachments: z .array( z.object({ @@ -71,6 +65,8 @@ function send(app: PublicAPIApp) { ...c.req.valid("json"), teamId: team.id, apiKeyId: team.apiKeyId, + text: c.req.valid("json").text ?? undefined, + html: c.req.valid("json").html ?? undefined, }); return c.json({ emailId: email?.id }); diff --git a/apps/web/src/server/service/email-service.ts b/apps/web/src/server/service/email-service.ts index a2d9104..c47b759 100644 --- a/apps/web/src/server/service/email-service.ts +++ b/apps/web/src/server/service/email-service.ts @@ -99,6 +99,13 @@ export async function sendEmail( } } + if (!text && !html) { + throw new UnsendApiError({ + code: "BAD_REQUEST", + message: "Either text or html is required", + }); + } + const scheduledAtDate = scheduledAt ? new Date(scheduledAt) : undefined; const delay = scheduledAtDate ? Math.max(0, scheduledAtDate.getTime() - Date.now())