diff --git a/apps/web/src/lib/email-outbox.test.ts b/apps/web/src/lib/email-outbox.test.ts index 46fa55d6..d763d851 100644 --- a/apps/web/src/lib/email-outbox.test.ts +++ b/apps/web/src/lib/email-outbox.test.ts @@ -23,11 +23,14 @@ describe("outbox maximum age", () => { expect(outboxMaxAgeDays("plan_reminder")).toBe(3) expect(outboxMaxAgeDays("plan_followup")).toBe(2) expect(outboxMaxAgeDays("plan_ended")).toBe(14) + expect(outboxMaxAgeDays("plan_account_swapped")).toBe(14) }) it("labels every email kind the sender knows", () => { for (const kind of EMAIL_KINDS) expect(OUTBOX_KIND_LABELS[kind], kind).toBeTruthy() expect(OUTBOX_SKIP_LABELS.not_payable).toBeTruthy() + expect(OUTBOX_SKIP_LABELS.plan_not_active).toBeTruthy() + expect(OUTBOX_SKIP_LABELS.account_superseded).toBeTruthy() }) it("counts from the later of queueing and the last deferral", () => { diff --git a/apps/web/src/lib/email-outbox.ts b/apps/web/src/lib/email-outbox.ts index ba8ddfda..2c973a5b 100644 --- a/apps/web/src/lib/email-outbox.ts +++ b/apps/web/src/lib/email-outbox.ts @@ -20,6 +20,7 @@ export const OUTBOX_KIND_LABELS: Record = { plan_reminder: "Mã ủng hộ hằng tháng", plan_followup: "Nhắc lại mã ủng hộ hằng tháng", plan_ended: "Kế hoạch hằng tháng đã dừng", + plan_account_swapped: "Thay đổi tài khoản nhận của kế hoạch hằng tháng", } export const OUTBOX_STATUS_LABELS: Record = { @@ -39,6 +40,8 @@ export const OUTBOX_SKIP_LABELS: Record = { not_due: "cam kết không còn đến hạn thanh toán", not_payable: "khoản ủng hộ hằng tháng không còn chuyển được (kế hoạch đã dừng, hết tháng hoặc chiến dịch không còn nhận)", + plan_not_active: "kế hoạch ủng hộ hằng tháng đã dừng", + account_superseded: "tài khoản nhận đã được thay đổi một lần nữa trước khi gửi", } /** @@ -53,7 +56,13 @@ export const THROTTLE_HOLD_PREFIX = "THROTTLE:" export function outboxMaxAgeDays(kind: string): number { if (kind === "pledge_payment_reminder" || kind === "plan_followup") return 2 if (kind === "plan_reminder") return 3 - if (kind === "pledge_payment_due" || kind === "campaign_ended" || kind === "plan_ended") return 14 + if ( + kind === "pledge_payment_due" || + kind === "campaign_ended" || + kind === "plan_ended" || + kind === "plan_account_swapped" + ) + return 14 return 7 } diff --git a/apps/web/src/server/email/outbox.test.ts b/apps/web/src/server/email/outbox.test.ts index 4bc2f8f7..fffdefb7 100644 --- a/apps/web/src/server/email/outbox.test.ts +++ b/apps/web/src/server/email/outbox.test.ts @@ -177,6 +177,69 @@ describe("sendDueEmails", () => { expect(sent).toHaveLength(1) expect(sent[0].headers["List-Unsubscribe"]).toBeUndefined() }) + + it("signs stop_token and includes unsubscribe link for plan_account_swapped (DOS/Give#291)", async () => { + claims = [ + [ + row(24, { + kind: "plan_account_swapped", + category: "notification", + unsubscribe_token: UNSUB, + stop_token: STOP, + payload: { + campaign_slug: "nuoc-sach", + campaign_title: "Nước sạch", + changed_date: "2026-11-22", + new_channel: "statement_import", + new_bank_bin: "970422", + new_account_name: "NGUYEN MINH TAM", + new_account_last4: "0002", + new_account_masked: "****0002", + old_bank_bin: "970436", + old_account_last4: "0001", + old_account_masked: "****0001", + }, + }), + ], + ] + await sendDueEmails(fakeDb(handler, calls) as never, transport, OPTIONS) + const signed = signStopToken(STOP, SECRET)! + expect(sent).toHaveLength(1) + expect(sent[0].headers["List-Unsubscribe"]).toBe( + `` + ) + expect(sent[0].headers["List-Unsubscribe-Post"]).toBe("List-Unsubscribe=One-Click") + expect(sent[0].text).toContain(`https://give.vn/plans/stop?t=${encodeURIComponent(signed)}`) + expect(sent[0].text).toContain(`https://give.vn/email/unsubscribe?t=${UNSUB}`) + }) + + it("marks plan_account_swapped dead when stop_token or unsubscribe_token is missing", async () => { + claims = [ + [ + row(25, { + kind: "plan_account_swapped", + category: "notification", + unsubscribe_token: UNSUB, + stop_token: null, + payload: { campaign_title: "Nước sạch" }, + }), + row(26, { + kind: "plan_account_swapped", + category: "notification", + unsubscribe_token: null, + stop_token: STOP, + payload: { campaign_title: "Nước sạch" }, + }), + ], + ] + completeStatus = "dead" + await sendDueEmails(fakeDb(handler, calls) as never, transport, OPTIONS) + expect(sent).toHaveLength(0) + expect(completes()).toMatchObject([ + { p_id: row(25).id, p_permanent: true, p_error: "render: stop_token_missing" }, + { p_id: row(26).id, p_permanent: true, p_error: "render: unsubscribe_token_missing" }, + ]) + }) }) it("reports a failure without the recipient's address, as permanent only when the provider says so", async () => { diff --git a/apps/web/src/server/email/templates.test.ts b/apps/web/src/server/email/templates.test.ts index 3b726664..65fcf647 100644 --- a/apps/web/src/server/email/templates.test.ts +++ b/apps/web/src/server/email/templates.test.ts @@ -277,6 +277,126 @@ describe("renderEmail: monthly plans (DOS/Give#276)", () => { }) }) +describe("renderEmail: plan_account_swapped (DOS/Give#291)", () => { + const SIGNED = `${"cd".repeat(32)}.${"A".repeat(21)}w` + const swap = (payload: Record = {}, overrides: Partial = {}) => + email({ + kind: "plan_account_swapped", + category: "notification", + tracking_token: null, + reference_code: null, + unsubscribe_token: UNSUB, + stop_token: "cd".repeat(32), + payload: { + campaign_id: CAMPAIGN, + campaign_slug: "nuoc-sach", + campaign_title: "Nước sạch cho bản", + campaign_type: "give", + plan_id: "33333333-3333-4333-8333-333333333333", + swap_request_id: "55555555-5555-4555-8555-555555555555", + new_binding_id: "66666666-6666-4666-8666-666666666666", + changed_at: "2026-11-22T09:00:00Z", + changed_date: "2026-11-22", + new_channel: "statement_import", + new_bank_bin: "970422", + new_chain: null, + new_account_name: "NGUYEN MINH TAM", + new_account_last4: "0002", + new_account_masked: "****0002", + old_bank_bin: "970436", + old_account_last4: "0001", + old_account_masked: "****0001", + ...payload, + }, + ...overrides, + }) + const FORBIDDEN = /quá hạn|nợ|còn thiếu/i + + it("states holder name, bank, masked last 4 digits, swap date, campaign link, stop link and unsubscribe without asking to transfer", () => { + const r = renderEmail(swap(), ORIGIN, { signedStopToken: SIGNED }) + expect(r.subject).toBe("Tài khoản nhận của chiến dịch “Nước sạch cho bản” đã thay đổi") + expect(r.text).toContain("Ngày 22/11/2026, Give đã duyệt thay đổi tài khoản nhận") + expect(r.text).toContain( + "Tài khoản nhận mới: chủ tài khoản NGUYEN MINH TAM, ngân hàng MBBank, số tài khoản •••• 0002." + ) + expect(r.text).toContain("không yêu cầu bạn chuyển tiền") + expect(r.text).toContain("tài khoản cũ Vietcombank •••• 0001") + expect(r.text).toContain(`Xem trang chiến dịch: ${ORIGIN}/campaigns/nuoc-sach`) + const stop = `${ORIGIN}/plans/stop?t=${encodeURIComponent(SIGNED)}` + const unsub = `${ORIGIN}/email/unsubscribe?t=${UNSUB}` + expect(r.text).toContain(`Không muốn nhận lời nhắc nữa? Dừng ủng hộ hằng tháng: ${stop}`) + expect(r.text).toContain(`Không muốn nhận các email thông báo như thế này? Hủy đăng ký: ${unsub}`) + expect(r.text).not.toContain("email tổng kết") + expect(r.html).toContain(`href="${stop}"`) + expect(r.html).toContain(`href="${unsub}"`) + expect(r.unsubscribeUrl).toBe(unsub) + expect(r.oneClickUnsubscribeUrl).toBe(`${ORIGIN}/api/plans/stop?t=${encodeURIComponent(SIGNED)}`) + expect(r.text).not.toMatch(FORBIDDEN) + expect(r.text.split("\n--\n")[0]).not.toMatch(/\d{6,}/) + expect(r.html).not.toMatch(/ { + const r = renderEmail( + swap({ + changed_date: null, + changed_at: "2026-11-22T18:30:00Z", // 2026-11-23 01:30 Asia/Ho_Chi_Minh + campaign_slug: null, + old_bank_bin: null, + old_account_last4: null, + old_account_masked: null, + }), + ORIGIN, + { signedStopToken: SIGNED } + ) + expect(r.text).toContain("Ngày 23/11/2026") + expect(r.text).toContain("tài khoản cũ của chiến dịch") + expect(r.text).toContain(`${ORIGIN}/me/contributions`) + }) + + it("supports crypto channel formatting with masked last 4 characters", () => { + const r = renderEmail( + swap({ + new_channel: "crypto", + new_bank_bin: null, + new_account_name: null, + new_chain: "polygon", + new_account_last4: "a9F2", + new_account_masked: "****a9F2", + }), + ORIGIN, + { signedStopToken: SIGNED } + ) + expect(r.text).toContain("Ví nhận mới trên mạng polygon: •••• a9F2.") + }) + + it("never renders raw multi-digit account numbers even from a malformed payload and escapes HTML", () => { + const r = renderEmail( + swap({ + new_account_name: 'TAM ', + new_account_last4: "291000220002", + new_account_masked: "291000220002", + old_account_last4: "291000110001", + old_account_masked: "291000110001", + }), + ORIGIN, + { signedStopToken: SIGNED } + ) + expect(r.text).not.toContain("291000220002") + expect(r.text).not.toContain("291000110001") + expect(r.html).not.toContain("291000220002") + expect(r.html).not.toContain("