diff --git a/app/api/broadcasts/queue/route.ts b/app/api/broadcasts/queue/route.ts
index 7c31ab4..e90f118 100644
--- a/app/api/broadcasts/queue/route.ts
+++ b/app/api/broadcasts/queue/route.ts
@@ -1,8 +1,8 @@
import { NextResponse } from "next/server";
-import { recipientsFor } from "@/lib/audience";
+import { chunk } from "@coldtea/broadcast-lib";
import { firestore } from "@/lib/firestore";
export async function POST(request: Request) {
@@ -14,26 +14,20 @@ export async function POST(request: Request) {
const broadcast = await request.json();
- const recipients = await recipientsFor(broadcast.audienceId);
-
- for (const recipient of recipients) {
- await firestore.collection("broadcastQueue").add({
- broadcastId: broadcast.id,
- email: recipient.email,
- status: "pending",
- });
- }
+ await firestore.collection("broadcastQueue").add({
+ broadcastId: broadcast.id,
+ audienceId: broadcast.audienceId,
+ batchSize: chunk.MAX_PER_CALL,
+ status: "pending",
+ });
return NextResponse.json({ queued: true });
}
diff --git a/packages/broadcast-lib/index.ts b/packages/broadcast-lib/index.ts
new file mode 100644
--- /dev/null
+++ b/packages/broadcast-lib/index.ts
@@ -0,0 +1,18 @@
+export const chunk = {
+ MAX_PER_CALL: 500,
+ into<T>(items: readonly T[], size = chunk.MAX_PER_CALL): T[][] {
+ const batches: T[][] = [];
+ for (let i = 0; i < items.length; i += size) batches.push(items.slice(i, i + size));
+ return batches;
+ },
+};
diff --git a/functions/send-broadcast-bulk.ts b/functions/send-broadcast-bulk.ts
new file mode 100644
--- /dev/null
+++ b/functions/send-broadcast-bulk.ts
@@ -0,0 +1,34 @@
+import { chunk } from "@coldtea/broadcast-lib";
+import { buildBulkPayload } from "./build-bulk-payload";
+import { getSuppressedEmails } from "./get-suppressed-emails";
+
+export const sendBroadcastBulk = onWrite("broadcastQueue/{id}", async (event) => {
+ const suppressed = await getSuppressedEmails();
+ const batches = chunk.into(await audienceFor(event), chunk.MAX_PER_CALL);
+
+ for (const batch of batches) {
+ const payload = buildBulkPayload(batch, suppressed);
+ const results = await postmark.post("/email/batch", payload);
+ await writeResults(event.id, results);
+ }
+});
diff --git a/functions/process-broadcast.ts b/functions/process-broadcast.ts
deleted file mode 100644
--- a/functions/process-broadcast.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { sendSingleEmail } from "./send-single-email";
-
-export const processBroadcast = onWrite("broadcastQueue/{id}", async (event) => {
- const job = event.data();
- if (job.status !== "pending") return;
-
- await sendSingleEmail(job.email, job.broadcastId);
- await event.ref.update({ status: "sent" });
-});
diff --git a/functions/send-single-email.ts b/functions/send-single-email.ts
deleted file mode 100644
--- a/functions/send-single-email.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-export async function sendSingleEmail(to: string, broadcastId: string) {
- return postmark.post("/email", {
- To: to,
- TemplateAlias: broadcastId,
- });
-}
Webhook marks the invoice paid before checking the signature
Anyone who can reach the endpoint can mark an invoice paid, because
markPaidruns on the parsed body.Fix: Call
verify(request)before parsing the body and return 401 when it fails.▶ 🤖 Prompt for AI agents