Skip to content
131 changes: 131 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ they reach Production with a separate, approved promotion of DOS.Me `main`.

### Design system and screens

- Atomic moderator status-transition race guard and pre-launch recovery queue:
`apps/web/src/app/admin/actions.ts` passes `p_expected_status` on
`approveCampaign` (`in_review` -> `approved`, `approved` -> `live`) and
`requestChanges` (`in_review`, `approved`, `suspended`) and maps
`GIVE_CAMPAIGN_STATUS_CHANGED` (`40001`) and `suspended -> changes_requested`
refusals in `apps/web/src/lib/errors.ts`; `/admin` adds the `#recovery`
(`"Cần xử lý trước khi mở"`) section backed by
`give.campaigns_needing_recovery()` (`listCampaignsNeedingRecovery`,
`recoveryAdvice`) so moderators can see and recover approved campaigns past
`ends_at` with usable accounts and campaigns suspended before `starts_at`
(never live, no contributions), with unit and RSC Vitest coverage, backed by
`DOS/DOS.Me#1872` (#77 / #306).
- Co-organizers (campaign editors), single-use invites, workspace guards and
public team block: `/me/campaigns/[id]?tab=team` (`TeamTab`,
`CreateInviteForm`, `member-actions.ts`) lets the owning organizer create
Expand Down Expand Up @@ -433,6 +445,26 @@ trang"` gating, `"Quản lý"`, and `changes_requested` review notes),

### Back campaigns (M3)

- Finance-approved receiving-account swap for `back` campaigns and
`payment_due` pledge notifications: extends `canAskSwap`, `approvalBlock`,
`loadEditorAccounts`, `checkSubmitAccount`, `requestAccountSwap`,
`/me/campaigns/[id]` (`ReceivingAccountSection` / `AccountSwapBlock`),
`/admin/account-swaps`, `/admin` `recoveryAdvice` and `lib/errors.ts` to
support finance-approved receiving-account swaps on `back` campaigns in
`live`, `suspended`, `changes_requested` (with recorded transactions;
zero-transaction bindings still direct to early release) and `succeeded`
when pledges still have payments due (`campaignHasPaymentsDue` /
`campaignsWithPaymentsDue`), while strictly preserving `#264` owner-only
money boundaries so co-organizers never view or submit swap requests. Adds
the `payment_account_changed` in-app notice (`lib/notices.ts`, wired into
`asksForPayment` so `stoppedNoticeCampaigns` suppresses payment CTAs while
payment requests are stopped) and the `pledge_account_swapped` transactional
email template (`server/email/templates.ts`, `lib/email-outbox.ts` 14-day
max age, live net amount due, deadline, `/me/pledges/[code]` CTA, no
unsubscribe/stop links), sharing strict anti-leak masking helpers
(`maskedAccountLast4`, `swappedNewAccountLine`, `swappedOldAccountTarget`)
so raw account numbers never leak, backed by `DOS/DOS.Me#1873` with Vitest
suites (#92 / #310).
- Pledge flow, payment page, reminders and lapse (#31 / #88).
- Editor: reward tiers and fulfillment updates (#83 / #87); account section
and submit guard (#85 / #90).
Expand All @@ -447,6 +479,38 @@ trang"` gating, `"Quản lý"`, and `changes_requested` review notes),

### Money (M1)

- Monthly donations (plan A) follow-ups from `#290`: adds the deduplicated,
bounded-concurrency `paymentAccounts()` helper (max 5 in flight,
per-campaign error isolation) in `apps/web/src/server/contributions/index.ts`,
skips `give.payment_account` RPCs on `/me/contributions` for ended plans or
plans without a potentially payable current-month donation (sharing
`currentPlanDonation()` with `PlanList` and `vnMonthStart` in
`lib/plans.ts`), runs usable-account and pledge-balance lookups in parallel
with one consistent clock, and fixes the misplaced `ContributionList` JSDoc
block. Hardens `apps/web/src/server/plans/jobs.ts`,
`GET /api/cron/issue-plan-donations` and `GET /api/cron/plan-notices` to
isolate Step 1 and Step 2 within the shared 45 s budget (still returning
HTTP 500 on step failure), drain multi-batch `expire_plan_donations`
(500 rows/batch) with per-batch error isolation and budget-guarded
stalled-batch detection (`expire_stalled: true`, `done: false`), and emit
structured zero-PII JSON error logs (`plan_cron.failed_outcomes`,
`plan_job.plan_failed`, `plan_job.expire_batch_stalled`,
`plan_job.expire_batch_failed`, `plan_cron.step_failed`) on `'failed'`
outcomes or step/batch errors, with Vitest coverage (#308 / #309).
- Monthly plan donor notifications on receiving-account swap: adds the
`plan_account_swapped` email template (`apps/web/src/server/email/templates.ts`)
and admin outbox metadata (`apps/web/src/lib/email-outbox.ts`:
`OUTBOX_KIND_LABELS.plan_account_swapped`, `OUTBOX_SKIP_LABELS.plan_not_active`,
`OUTBOX_SKIP_LABELS.account_superseded`, and 14-day `outboxMaxAgeDays`).
Notifies active monthly plan donors when a campaign's receiving account is
replaced, showing Vietnam-time swap date, new account holder name, bank
short name or crypto chain, and strictly validated masked last 4 digits only
(`•••• XXXX`, never raw account numbers), warning donors to delete saved
bank transfer templates for the old account, requiring both a signed
one-click plan stop link (`/plans/stop?t=...` and RFC 8058
`List-Unsubscribe`) and notification unsubscribe link
(`/email/unsubscribe?t=...`), and generalizing non-summary unsubscribe copy,
backed by `DOS/DOS.Me#1871` with Vitest suites (#291 / #307).
- Monthly donations, plan A (D9): a signed-in donor signs up on a give
campaign that allows it (amount, day 1–28, reminder consent, the
anonymity warning that a transparent account's statement shows the name
Expand Down Expand Up @@ -616,6 +680,27 @@ trang"` gating, `"Quản lý"`, and `changes_requested` review notes),

### Platform

- Email outbox follow-ups: `apps/web/src/server/email/outbox.ts` lengthens
`CLAIM_TIMEOUT_MS` from `10_000` ms to `15_000` ms (above PostgREST's 8 s
`statement_timeout` plus pooler/network jitter while remaining under
`RUN_BUDGET_MS` = 30 s) and inspects both `claimSignal.aborted` and
`isAbortError(error)` (excluding Postgres server-side rollback `57014`) to
emit an explicit warning that an aborted `claim_outbox` call may still have
committed and leased rows for 5 minutes. `apps/web/src/server/email/provider.ts`
hardens `usesProductionDatabase(env)` to inspect the JWT `ref` claim of
`SUPABASE_SERVICE_ROLE_KEY` and `NEXT_PUBLIC_SUPABASE_ANON_KEY` in addition
to `PRODUCTION_SUPABASE_HOSTS` / `PRODUCTION_SUPABASE_REF` (cleanly ignoring
opaque `sb_secret_*` keys and malformed tokens), backed by `DOS/DOS.Me#1870`
with Vitest coverage (#273 / #305).
- PO configuration update (`docs/product-owner-config.md`): sets
`promotion_po: any` so both Antigravity PO and Claude Code PO hold equal
Production promotion authority (requiring JOY's popup approval on concrete
steps and rollback plan with zero competing open `main` promotion PRs),
records JOY's 2026-11-22 Antigravity PO takeover across `line:campaigns`,
`line:money` and `line:platform` while Claude PO is quota-limited (preserving
`po:agy` / `po:claude` mutual exclusion), and documents the 2026-11-21 local
DB ban (`Give#294` / `DOS/DOS.Me#1855`, running Give pgTAP and migration
replay in GitHub CI on `DOS/DOS.Me`) (#303 / #304).
- Parallel PO configuration (CC-PO v2.18.0): `docs/product-owner-config.md`
assigns Antigravity `Campaigns Product Owner` (`acc0`) to `line:campaigns`
(replacing paused Qwen Code sessions), enforces `po:agy` / `po:claude`
Expand Down Expand Up @@ -680,6 +765,52 @@ trang"` gating, `"Quản lý"`, and `changes_requested` review notes),

### Database (DOS.Me)

- Back-campaign receiving-account swap and `payment_due` pledge notices:
extends `give.request_account_swap`, `give.decide_account_swap`,
`give.account_has_swapped_history` and `give.record_bank_transaction`
post-swap outflow cutoff to `back` campaigns (`live`, `suspended`,
`changes_requested` with recorded transactions, and `succeeded` when
`give.campaign_has_payments_due(id)` is true), preserving `#264` owner-only
money boundaries, `#82` / `#119` lock order and self-review guards; adds
in-app notice kind `payment_account_changed` and transactional outbox kind
`pledge_account_swapped` (14-day max age, masked account details only,
`give.claim_outbox` `account_superseded` / 90-day `payment_requests_stopped`
hold / `not_due` / live `pledge_notice_payload` refresh), with 66 pgTAP
assertions (#92 / DOS/DOS.Me#1873, on Beta). Migration
`20261130140000_give_back_campaign_account_swap`.
- Monthly plan donor swap notifications: adds notification outbox kind
`plan_account_swapped` (14-day max age, subject to `give.email_suppressions`),
`give.outbox_on_account_swap` trigger enqueueing one masked, zero-PII row
per active `give.donation_plans` donor on approved account swaps
(`idempotency_key = 'plan_account_swapped:<swap_request_id>:<plan_id>'`),
and `give.claim_outbox` send-time re-checks (`plan_not_active`,
`account_superseded`, `unsubscribed`, `no_recipient`, `expired`) minting a
fresh 60-day one-use stop token in `give.plan_stop_tokens`, with 53 pgTAP
assertions (#291 / DOS/DOS.Me#1871, on Beta). Migration
`20261130130000_give_plan_account_swap_notice`.
- Atomic expected-status guard and pre-launch recovery queue: adds
backwards-compatible `p_expected_status text DEFAULT NULL` to
`give.transition_campaign` (raising `GIVE_CAMPAIGN_STATUS_CHANGED`, SQLSTATE
`40001`, checked after authorization and before idempotent replay while
preserving `#82` / `#120` staff-role lock order), allows non-owner
`moderator`/`admin` `suspended -> changes_requested` recovery for never-live
campaigns with `starts_at > now()` and zero contributions (preserving `#55`
money-history immutability and `#264` owner-only boundaries), and adds
`give.campaigns_needing_recovery()` (disjoint from
`campaigns_waiting_for_account`, listing `approved_past_end` and
`suspended_before_start`), with 62 pgTAP assertions (#77 / DOS/DOS.Me#1872,
on Beta). Migration `20261130120000_give_campaign_sendback_and_suspended_recovery`.
- Email outbox follow-ups: adds partial index `give.outbox_purge_settled_idx`,
`give.contribution_access_tokens_expires_idx` and `FOR UPDATE SKIP LOCKED`
batching in `give.purge_outbox`, restricts `give.claim_outbox`
`to_email_hash` writes to `category = 'notification'` while clearing
deleted-account and legacy transactional hashes on purge, protects active
in-flight sends (`status = 'sending' AND locked_until > now()`) from purge
step 1, caps `pledge_payment_due` / `pledge_payment_reminder` holds under
`payments_stopped_at` at 90 days (`skipped`/`expired`), and recomputes
`pledge_payment_reminder` `payload->'day'` on claim from
`outcome_at + payments_stopped_total`, with 31 pgTAP assertions (#273 /
DOS/DOS.Me#1870, on Beta). Migration `20261130110000_give_outbox_followups`.
- Co-organizers and single-use invites: `give.campaign_members` (editors only,
blocking owner self-membership) and `give.campaign_member_invites` (32-byte
SHA-256 token hashes only, zero direct table grants), 9 `SECURITY DEFINER`
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/app/admin/account-swaps/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Decision = (typeof DECISIONS)[number]

const DONE_MESSAGES: Record<Decision, string> = {
approved:
"Đã đổi tài khoản nhận: tài khoản cũ đã ngừng và bị khóa, tài khoản mới nhận tiền từ bây giờ. Trang chiến dịch và sao kê hiện thông báo đổi tài khoản khi chiến dịch đang công khai.",
"Đã đổi tài khoản nhận: tài khoản cũ đã ngừng và bị khóa, tài khoản mới nhận tiền từ bây giờ. Trang chiến dịch và sao kê hiện thông báo đổi tài khoản khi chiến dịch đang công khai; nếu là dự án có quà tặng đang chờ thanh toán, người ủng hộ cũng đã được thông báo tài khoản mới.",
rejected: "Đã từ chối yêu cầu đổi tài khoản.",
}

Expand Down Expand Up @@ -85,5 +85,7 @@ export async function decideAccountSwap(
revalidatePath("/me/campaigns/[id]", "page")
revalidatePath("/campaigns/[slug]", "page")
revalidatePath("/campaigns/[slug]/statement", "page")
revalidatePath("/me/pledges/[code]", "page")
revalidatePath("/me/notices")
return done(DONE_MESSAGES[decision])
}
118 changes: 118 additions & 0 deletions apps/web/src/app/admin/account-swaps/page.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { renderToStaticMarkup } from "react-dom/server"
import { beforeEach, describe, expect, it, vi } from "vitest"

import type { SwapForReview } from "@/lib/account-swaps"
import { fakeDb, op, type Call, type Handler } from "@/test/fake-db"

const REVIEWER = "66666666-6666-4666-8666-666666666666"
const OWNER = "11111111-1111-4111-8111-111111111111"
const DUE_CAMPAIGN = "22222222-2222-4222-8222-222222222222"
const PAID_CAMPAIGN = "33333333-3333-4333-8333-333333333333"

const state = vi.hoisted(() => ({
handler: (() => undefined) as Handler,
calls: [] as Call[],
}))

vi.mock("next/navigation", () => ({
notFound: vi.fn(() => {
throw new Error("NEXT_NOT_FOUND")
}),
usePathname: () => "/admin/account-swaps",
}))
vi.mock("@/lib/auth/session", () => ({
requireSession: vi.fn(async () => ({ user: { id: REVIEWER, email: null, name: null }, accessToken: "token" })),
}))
vi.mock("@/lib/supabase", () => ({
dbAs: () => fakeDb((call) => state.handler(call), state.calls),
}))

const { default: AdminAccountSwapsPage } = await import("./page")

function pendingSwapRow(id: string, campaignId: string, title: string): SwapForReview {
return {
id,
campaign_id: campaignId,
old_binding_id: `b-${id}`,
new_receiving_account_id: `a-new-${id}`,
new_binding_id: null,
reason: "Ngân hàng đã đóng tài khoản cũ.",
status: "pending",
decided_at: null,
decision_note: null,
public_note: null,
created_at: "2026-11-22T01:00:00Z",
campaign: {
id: campaignId,
slug: `slug-${id}`,
title,
status: "succeeded",
type: "back",
payments_stopped_at: null,
},
new_account: {
id: `a-new-${id}`,
owner_user_id: OWNER,
channel: "statement_import",
bank_bin: "970422",
account_number: "920002220002",
account_name: "NGUYEN THI DAO",
verification_status: "verified",
review_note: null,
organizer: { legal_name: "Nguyễn Thị Đào", display_name: "Đào", verification_status: "verified" },
},
old_binding: {
id: `b-${id}`,
bound_at: "2026-11-01T00:00:00Z",
released_at: null,
account: {
id: `a-old-${id}`,
owner_user_id: OWNER,
channel: "statement_import",
bank_bin: "970436",
account_number: "920001110001",
account_name: "NGUYEN THI DAO",
verification_status: "disabled",
review_note: "Khóa do ngân hàng đóng",
},
},
}
}

beforeEach(() => {
state.calls.length = 0
state.handler = () => undefined
})

describe("AdminAccountSwapsPage (DOS/Give#92)", () => {
it("returns 404 when the caller does not hold a finance or admin role", async () => {
state.handler = (call) => (call.target === "rpc:is_admin" ? { data: false } : undefined)
await expect(AdminAccountSwapsPage()).rejects.toThrow("NEXT_NOT_FOUND")
})

it("enables approval for a succeeded back campaign with payments due and blocks one with none", async () => {
const dueSwap = pendingSwapRow("swap-due", DUE_CAMPAIGN, "Đèn ngủ gỗ còn chờ thanh toán")
const paidSwap = pendingSwapRow("swap-paid", PAID_CAMPAIGN, "Đèn ngủ gỗ đã thanh toán đủ")

state.handler = (call) => {
if (call.target === "rpc:is_admin") return { data: true }
if (call.target === "account_swap_requests") {
return op(call, "eq")?.[1] === "pending" ? { data: [dueSwap, paidSwap] } : { data: [] }
}
if (call.target === "bank_transactions") return { count: 2 }
if (call.target === "contributions") {
return op(call, "eq")?.[1] === DUE_CAMPAIGN ? { data: [{ id: "pledge-1" }] } : { data: [] }
}
return undefined
}

const html = renderToStaticMarkup(await AdminAccountSwapsPage())
expect(html).toContain("dự án có quà tặng đã đạt mục tiêu nhưng còn cam kết chờ thanh toán")
expect(html).toContain("Đèn ngủ gỗ còn chờ thanh toán")
expect(html).toContain("Đèn ngủ gỗ đã thanh toán đủ")
expect(html).toContain("Duyệt đổi tài khoản")
expect(html).toContain("không còn cam kết nào đang chờ thanh toán")
// Only the due swap gets an approve button; the settled one gets the block callout instead.
expect(html.match(/>Duyệt đổi tài khoản</g)).toHaveLength(1)
})
})
Loading