add batch email api (#149)

* add bulk email

* add bulk email api

* add batch email sdk changes
This commit is contained in:
KM Koushik
2025-04-19 21:45:17 +10:00
committed by GitHub
parent 44e4f43e66
commit 3fe96b477f
10 changed files with 724 additions and 49 deletions

View File

@@ -0,0 +1,73 @@
import { createRoute, z } from "@hono/zod-openapi";
import { PublicAPIApp } from "~/server/public-api/hono";
import { getTeamFromToken } from "~/server/public-api/auth";
import { sendBulkEmails } from "~/server/service/email-service";
import { EmailContent } from "~/types";
import { emailSchema } from "../../schemas/email-schema"; // Corrected import path
// Define the schema for a single email within the bulk request
// This is similar to the schema in send-email.ts but without the top-level 'required'
// Removed inline emailSchema definition
const route = createRoute({
method: "post",
path: "/v1/emails/batch",
request: {
body: {
required: true,
content: {
"application/json": {
// Use the imported schema in an array
schema: z.array(emailSchema).max(100, {
message:
"Cannot send more than 100 emails in a single bulk request",
}),
},
},
},
},
responses: {
200: {
content: {
"application/json": {
// Return an array of objects with the created email IDs
schema: z.object({
data: z.array(z.object({ emailId: z.string() })),
}),
},
},
description: "List of successfully created email IDs",
},
// Add other potential error responses based on sendBulkEmails logic if needed
},
});
function sendBatch(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = await getTeamFromToken(c);
const emailPayloads = c.req.valid("json");
// Add teamId and apiKeyId to each email payload
const emailsToSend: Array<
EmailContent & { teamId: number; apiKeyId?: number }
> = emailPayloads.map((payload) => ({
...payload,
text: payload.text ?? undefined,
html: payload.html ?? undefined,
teamId: team.id,
apiKeyId: team.apiKeyId,
}));
// Call the service function to send emails in bulk
const createdEmails = await sendBulkEmails(emailsToSend);
// Map the result to the response format
const responseData = createdEmails.map((email) => ({
emailId: email.id,
}));
return c.json({ data: responseData });
});
}
export default sendBatch;

View File

@@ -2,6 +2,7 @@ import { createRoute, z } from "@hono/zod-openapi";
import { PublicAPIApp } from "~/server/public-api/hono";
import { getTeamFromToken } from "~/server/public-api/auth";
import { sendEmail } from "~/server/service/email-service";
import { emailSchema } from "../../schemas/email-schema";
const route = createRoute({
method: "post",
@@ -11,36 +12,7 @@ const route = createRoute({
required: true,
content: {
"application/json": {
schema: z
.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",
}),
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().nullable(),
html: z.string().optional().nullable(),
attachments: z
.array(
z.object({
filename: z.string(),
content: z.string(),
})
)
.optional(),
scheduledAt: z.string().datetime().optional(),
})
.refine(
(data) => !!data.subject || !!data.templateId,
"Either subject or templateId should be passed."
),
schema: emailSchema,
},
},
},

View File

@@ -12,6 +12,7 @@ import upsertContact from "./api/contacts/upsert-contact";
import createDomain from "./api/domains/create-domain";
import deleteContact from "./api/contacts/delete-contact";
import verifyDomain from "./api/domains/verify-domain";
import sendBatch from "./api/emails/batch-email";
export const app = getApp();
@@ -23,6 +24,7 @@ verifyDomain(app);
/**Email related APIs */
getEmail(app);
sendEmail(app);
sendBatch(app);
updateEmailScheduledAt(app);
cancelScheduledEmail(app);

View File

@@ -0,0 +1,40 @@
import { z } from "@hono/zod-openapi";
/**
* Reusable Zod schema for a single email payload used in public API requests.
*/
export const emailSchema = z
.object({
to: z.string().email().or(z.array(z.string().email())),
from: z.string().email(),
subject: z.string().min(1).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().email().or(z.array(z.string().email())).optional(),
cc: z.string().email().or(z.array(z.string().email())).optional(),
bcc: z.string().email().or(z.array(z.string().email())).optional(),
text: z.string().min(1).optional().nullable(),
html: z.string().min(1).optional().nullable(),
attachments: z
.array(
z.object({
filename: z.string().min(1),
content: z.string().min(1), // Consider base64 validation if needed
})
)
.max(10) // Limit attachments array size if desired
.optional(),
scheduledAt: z.string().datetime({ offset: true }).optional(), // Ensure ISO 8601 format with offset
})
.refine(
(data) => !!data.subject || !!data.templateId,
"Either subject or templateId must be provided."
)
.refine(
(data) => !!data.text || !!data.html,
"Either text or html content must be provided."
);