Open source CapCut alternative.
TypeScript
fix(site): settle unsubscribed email failures as skipped
David Le922bb1341c68
diff --git a/site/src/lib/email/fixtures/unsubscribed.ts b/site/src/lib/email/fixtures/unsubscribed.tsnew file mode 100644index 00000000..cdb35632--- /dev/null+++ b/site/src/lib/email/fixtures/unsubscribed.ts@@ -0,0 +1,99 @@+import { mock } from "bun:test";+import assert from "node:assert/strict";++import type { Prisma } from "@/generated/prisma/client";+import { DEFAULT_EMAIL_PRIORITIES } from "@/lib/email/kindIds";+import type { OutreachPayload } from "@/lib/marketing/send-outreach";++let optedOut = true;+let state = "queued";+const now = new Date();+const user = { id: "user", email: "[email protected]", name: "Reader" };+const payload: OutreachPayload = {+ outreachId: "outreach", attempt: 1, actorUserId: "operator", subject: "Hello {{firstName}}",+ body: "A note", creditOffer: null, unsubscribeLink: false, trackReplies: false,+ vars: { balance: "0", email: user.email, firstName: user.name, name: user.name, spent: "0", storage: "0" },+};+const row = {+ id: "send", kind: "outreach", idempotencyKey: "outreach:outreach:1", payload, userId: user.id,+ promotionId: null, attempts: 0, error: null, priority: 80, rank: 0, notBefore: now, createdAt: now,+ updatedAt: now, sentAt: null, clickedAt: null,+};+const updates: Prisma.EmailSendUpdateManyArgs[] = [];+const send = mock(() => { throw new Error("Unexpected provider send"); });+const schedule = mock(() => { throw new Error("Unexpected retry job"); });+mock.module("@/lib/prisma", () => ({ prisma: {+ user: { findUnique: async () => user },+ userEmailSettings: { findUnique: async () => ({ marketingUnsubscribedAt: optedOut ? now : null }) },+ emailSend: {+ createMany: async () => ({ count: 1 }),+ findUniqueOrThrow: async () => ({ ...row, state }),+ findMany: async () => [],+ findFirst: async () => null,+ groupBy: async () => [],+ update: async ({ data }: { data: { state: string; error: unknown } }) => {+ state = data.state;+ assert.equal(data.error, null);+ return { ...row, state };+ },+ updateMany: async (args: Prisma.EmailSendUpdateManyArgs) => {+ updates.push(args);+ if (args.where?.user) {+ assert.equal(args.where.state, "failed");+ assert.deepEqual(args.where.user, { emailSettings: { marketingUnsubscribedAt: { not: null } } });+ assert.deepEqual(args.data, { error: null, state: "skipped" });+ assert.deepEqual(args.where.kind, { in: ["outreach", "promotion-hand", "outreach-test", "promotion"] });+ if (state !== "failed" || !optedOut) return { count: 0 };+ state = "skipped";+ return { count: 1 };+ }+ if (args.where?.state === "failed" && state !== "failed") return { count: 0 };+ return { count: 1 };+ },+ },+ promotion: { findMany: async () => [] },+ asyncJob: { findMany: async () => [] },+} }));+mock.module("@/lib/config/effective", () => ({ getGlobalSetting: async () => DEFAULT_EMAIL_PRIORITIES }));+mock.module("@/lib/email/resend", () => ({+ emailFrom: () => "Donkey <[email protected]>",+ bulkFrom: () => "Donkey <[email protected]>",+ isResendConfigured: () => true,+ getResend: () => ({ emails: { send } }),+ ResendNotConfiguredError: class extends Error {},+}));+mock.module("@/lib/email/send-budget", () => ({+ withEmailBudget: async (_kind: string, work: () => Promise<unknown>) => work(),+ emailBudgetStatus: async () => ({}),+ EmailBudgetError: class extends Error {},+}));+mock.module("@/lib/jobs/queue", () => ({ ensureJob: schedule }));++const { deliverEmail, drainOutbox, outboxOverview, retryEmail } = await import("@/lib/email/outbox");+const { buildOutreachEmail } = await import("@/lib/marketing/send-outreach");+assert.equal(await buildOutreachEmail(payload, user), null);+optedOut = false;+assert.equal((await buildOutreachEmail(payload, user))?.subject, "Hello Reader");+optedOut = true;+assert.deepEqual(await deliverEmail({ kind: "outreach", payload, idempotencyKey: row.idempotencyKey, userId: user.id }), {+ id: row.id, state: "skipped", error: null,+});+assert.equal(send.mock.calls.length, 0);++state = "failed";+assert.equal(await retryEmail(row.id), false);+assert.equal(updates.at(-2)?.where?.id, row.id);+assert.equal(state, "skipped");+assert.equal(schedule.mock.calls.length, 0);++state = "failed";+await outboxOverview();+assert.equal(state, "skipped");+state = "failed";+await drainOutbox(1000);+assert.equal(state, "skipped");++optedOut = false;+state = "failed";+await outboxOverview();+assert.equal(state, "failed");diff --git a/site/src/lib/email/kinds.ts b/site/src/lib/email/kinds.tsindex 6db3cc1a..ccb39731 100644--- a/site/src/lib/email/kinds.ts+++ b/site/src/lib/email/kinds.ts@@ -36,6 +36,7 @@ export type OutboxRow = {export type EmailKind<P = unknown> = {quota: EmailSendKind;+ respectsMarketingOptOut?: boolean;payload: z.ZodType<P>;// The message for the provider, or null when nothing should go out any// more (the account is gone or opted out): the row is dropped as skipped.@@ -101,6 +102,7 @@ const creditOffer = define({const outreach = define({quota: "manual",+ respectsMarketingOptOut: true,payload: outreachPayloadSchema,build: async (payload, row) => {const user = await userOf(row);@@ -114,6 +116,7 @@ const outreach = define({// the sending address, since the row's alias would file the row.const outreachTest = define({quota: "manual",+ respectsMarketingOptOut: true,payload: outreachPayloadSchema,build: async (payload, row) => {const user = await userOf(row);@@ -165,6 +168,7 @@ async function promotionEmailFor(const promotion = define({quota: "bulk",+ respectsMarketingOptOut: true,payload: promotionPayloadSchema,build: async ({ promotionId }, row) => {const [user, promotion] = await Promise.all([userOf(row), promotionRowOf(promotionId)]);@@ -194,6 +198,7 @@ const promotionTest = define({// again. The row's outreach entry is filed as sent, like a note.const promotionHand = define({quota: "manual",+ respectsMarketingOptOut: true,payload: z.object({ promotionId: z.string().min(1), outreachId: z.string().min(1), actorUserId: z.string().min(1) }).strict(),diff --git a/site/src/lib/email/outbox.test.ts b/site/src/lib/email/outbox.test.tsnew file mode 100644index 00000000..1123b77d--- /dev/null+++ b/site/src/lib/email/outbox.test.ts@@ -0,0 +1,12 @@+import { expect, test } from "bun:test";+import { execFile } from "node:child_process";+import { fileURLToPath } from "node:url";+import { promisify } from "node:util";++test("unsubscribed email is skipped and existing failures settle without sending", async () => {+ // Isolate module mocks from the rest of the test suite.+ const { stdout, stderr } = await promisify(execFile)(process.execPath, [+ "run", fileURLToPath(new URL("./fixtures/unsubscribed.ts", import.meta.url)),+ ]);+ expect({ stdout, stderr }).toEqual({ stdout: "", stderr: "" });+});diff --git a/site/src/lib/email/outbox.ts b/site/src/lib/email/outbox.tsindex 8a5cfc12..823d788f 100644--- a/site/src/lib/email/outbox.ts+++ b/site/src/lib/email/outbox.ts@@ -48,6 +48,19 @@ type Outcome =// not burn its attempts.const backoffSeconds = (attempts: number) => attempts * 5 * 60;+// Failed mail for an opted-out recipient is settled without another send.+async function skipUnsubscribedFailures(id?: string): Promise<void> {+ await prisma.emailSend.updateMany({+ data: { error: null, state: "skipped" },+ where: {+ id,+ state: "failed",+ kind: { in: EMAIL_KIND_IDS.filter((kind) => EMAIL_KINDS[kind].respectsMarketingOptOut) },+ user: { emailSettings: { marketingUnsubscribedAt: { not: null } } },+ },+ });+}+function claimable(now: Date): Prisma.EmailSendWhereInput {return {OR: [{ state: "queued" }, { state: "sending", updatedAt: { lt: new Date(now.getTime() - STALE_SENDING_MS) } }],@@ -196,6 +209,7 @@ export type DrainResult = {* rows go. */export async function drainOutbox(budgetMs: number, kind?: EmailKindId): Promise<DrainResult> {const startedAt = Date.now();+ await skipUnsubscribedFailures();const result: DrainResult = { sent: 0, failed: 0, skipped: 0, retried: 0, retryAfterSeconds: null, more: false };const refused = new Map<EmailSendKind, number>();const touchedPromotions = new Set<string>();@@ -327,6 +341,7 @@ const ITEMS = 60;/** The outbox as su sees it: the cycle's budget, every kind's standing, and the* rows worth a look. */export async function outboxOverview(now = new Date()): Promise<OutboxOverview> {+ await skipUnsubscribedFailures();const dayStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));const [quota, priorities, queued, sentToday, failed, byCampaign, waiting, broken, recent, drains] = await Promise.all([emailBudgetStatus(now),@@ -349,7 +364,7 @@ export async function outboxOverview(now = new Date()): Promise<OutboxOverview>}),prisma.emailSend.findMany({include: { promotion: { select: { name: true } }, user: { select: { email: true } } },- orderBy: { sentAt: "desc" },+ orderBy: { updatedAt: "desc" },take: ITEMS,where: { state: { in: ["sent", "skipped"] } },}),@@ -419,6 +434,7 @@ export async function outboxOverview(now = new Date()): Promise<OutboxOverview>/** Puts a failed row back in the queue with a clean slate and sends for a* drainer. */export async function retryEmail(id: string): Promise<boolean> {+ await skipUnsubscribedFailures(id);const { count } = await prisma.emailSend.updateMany({data: { attempts: 0, error: null, notBefore: new Date(), state: "queued" },where: { id, state: "failed" },diff --git a/site/src/lib/marketing/send-outreach.ts b/site/src/lib/marketing/send-outreach.tsindex 1ae4f5cc..6363ebd0 100644--- a/site/src/lib/marketing/send-outreach.ts+++ b/site/src/lib/marketing/send-outreach.ts@@ -71,10 +71,10 @@ export async function buildOutreachEmail(payload: OutreachPayload,user: EmailUser,offerScope = outreachOfferScope(payload.outreachId, payload.attempt),-): Promise<EmailMessage> {+): Promise<EmailMessage | null> {+ if (await isMarketingUnsubscribed(user.id)) return null;const from = emailFrom();if (!from) throw new PermanentSendError("RESEND_FROM_EMAIL is not configured.");- if (await isMarketingUnsubscribed(user.id)) throw new PermanentSendError("That account is unsubscribed.");const offer = payload.creditOffer? await createTermsCreditOffer({