diff --git a/apps/web/src/app/api/cron/issue-plan-donations/route.test.ts b/apps/web/src/app/api/cron/issue-plan-donations/route.test.ts index 8a7a5f64..9812c1f3 100644 --- a/apps/web/src/app/api/cron/issue-plan-donations/route.test.ts +++ b/apps/web/src/app/api/cron/issue-plan-donations/route.test.ts @@ -5,6 +5,7 @@ import { fakeDb, op, type Call, type Handler } from "@/test/fake-db" const SECRET = "0123456789abcdef0123" const PLAN = "10000000-0000-4000-8000-000000000001" +const PLAN_2 = "20000000-0000-4000-8000-000000000002" const state = vi.hoisted(() => ({ handler: (() => undefined) as Handler, @@ -68,6 +69,63 @@ describe("GET /api/cron/issue-plan-donations", () => { ]) }) + it("emits a structured zero-PII alert when issue_plan_donations records 'failed' outcomes", async () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}) + state.handler = (call) => { + if (call.target === "rpc:expire_plan_donations") return { data: 2 } + const after = (op(call, "args")?.[0] as { p_after: string | null }).p_after + if (after === null) return { data: [{ plan_id: PLAN, outcome: "failed" }] } + if (after === PLAN) return { data: [{ plan_id: PLAN_2, outcome: "issued" }] } + return { data: [] } + } + const response = await GET(request(`Bearer ${SECRET}`)) + expect(response.status).toBe(200) + expect(await response.json()).toEqual({ failed: 1, issued: 1, done: true, expired: 2 }) + + const parsed = error.mock.calls.map(([arg]) => JSON.parse(String(arg)) as Record) + expect(parsed).toContainEqual({ + event: "plan_cron.failed_outcomes", + route: "issue-plan-donations", + job: "issue_plan_donations", + failed: 1, + calls: 3, + done: true, + }) + }) + + it("flags a stalled expiry batch with done=false and expire_stalled=true", async () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}) + state.handler = (call) => { + if (call.target === "rpc:issue_plan_donations") return { data: [] } + if (call.target === "rpc:expire_plan_donations") return { data: 0 } + if (call.target === "contributions") return { data: [{ id: "99999999-9999-4999-8999-999999999999" }] } + return undefined + } + const response = await GET(request(`Bearer ${SECRET}`)) + expect(response.status).toBe(200) + expect(await response.json()).toEqual({ done: false, expired: 0, expire_stalled: true }) + expect(error).toHaveBeenCalledTimes(1) + expect(JSON.parse(String(error.mock.calls[0][0]))).toEqual({ + event: "plan_job.expire_batch_stalled", + job: "expire_plan_donations", + batches: 1, + expired: 0, + }) + }) + + it("isolates step failures: expiry still runs within budget if issue_plan_donations fails", async () => { + vi.spyOn(console, "error").mockImplementation(() => {}) + state.handler = (call) => { + if (call.target === "rpc:issue_plan_donations") return { error: { code: "57014", message: "timeout" } } + if (call.target === "rpc:expire_plan_donations") return { data: 4 } + return undefined + } + const response = await GET(request(`Bearer ${SECRET}`)) + expect(response.status).toBe(500) + expect(await response.json()).toEqual({ error: "issue_failed" }) + expect(state.calls.map((c) => c.target)).toEqual(["rpc:issue_plan_donations", "rpc:expire_plan_donations"]) + }) + it("shares one deadline: past the budget the expiry waits for the next run", async () => { vi.useFakeTimers({ toFake: ["Date"] }) try { diff --git a/apps/web/src/app/api/cron/issue-plan-donations/route.ts b/apps/web/src/app/api/cron/issue-plan-donations/route.ts index a63abb90..28a44a94 100644 --- a/apps/web/src/app/api/cron/issue-plan-donations/route.ts +++ b/apps/web/src/app/api/cron/issue-plan-donations/route.ts @@ -2,7 +2,14 @@ import { NextResponse, type NextRequest } from "next/server" import { isAuthorizedCron } from "@/lib/cron" import { dbService } from "@/lib/supabase" -import { expirePlanDonations, remainingBudget, sweepPlans } from "@/server/plans/jobs" +import { + expirePlanDonations, + logPlanCronFailures, + logPlanStepFailure, + remainingBudget, + sweepPlans, + type PlanSweep, +} from "@/server/plans/jobs" export const dynamic = "force-dynamic" export const maxDuration = 60 @@ -14,8 +21,9 @@ export const maxDuration = 60 * ends the plans of campaigns past their end date; a suspended campaign's * plans wait. Then give.expire_plan_donations marks past months' unpaid plan * donations 'expired' (listing only; money that arrives later still matches). - * A run that stops early, or a run missed, is caught up by the next one; - * past months are never issued late. + * Both steps are isolated so a failure in one does not prevent the other from + * running within the shared deadline; a run that stops early or is missed is + * caught up by the next one, and past months are never issued late. */ export async function GET(request: NextRequest) { if (!isAuthorizedCron(request.headers.get("authorization"), process.env.CRON_SECRET)) { @@ -23,12 +31,27 @@ export async function GET(request: NextRequest) { } const db = dbService() const started = Date.now() + let sweep: PlanSweep | null = null + let sweepFailed = false try { - const sweep = await sweepPlans(db, "issue_plan_donations") - // Both steps share one deadline; past it the expiry waits for the next run. - const expired = remainingBudget(started) > 0 ? await expirePlanDonations(db) : null - return NextResponse.json({ ...sweep.outcomes, done: sweep.done && expired !== null, expired }) - } catch { + sweep = await sweepPlans(db, "issue_plan_donations") + logPlanCronFailures("issue-plan-donations", "issue_plan_donations", sweep) + } catch (error: unknown) { + sweepFailed = true + logPlanStepFailure("issue-plan-donations", "issue_plan_donations", error) + } + + // Both steps share one deadline; past it the expiry waits for the next run. + const budgetLeft = remainingBudget(started) + const expire = budgetLeft > 0 ? await expirePlanDonations(db, Date.now, budgetLeft) : null + + if (sweepFailed || !sweep || expire?.failed) { return NextResponse.json({ error: "issue_failed" }, { status: 500 }) } + return NextResponse.json({ + ...sweep.outcomes, + done: sweep.done && expire !== null && expire.done, + expired: expire ? expire.expired : null, + ...(expire?.stalled ? { expire_stalled: true } : {}), + }) } diff --git a/apps/web/src/app/api/cron/plan-notices/route.test.ts b/apps/web/src/app/api/cron/plan-notices/route.test.ts index 7d424d02..5d023cd7 100644 --- a/apps/web/src/app/api/cron/plan-notices/route.test.ts +++ b/apps/web/src/app/api/cron/plan-notices/route.test.ts @@ -5,6 +5,7 @@ import { fakeDb, op, type Call, type Handler } from "@/test/fake-db" const SECRET = "0123456789abcdef0123" const PLAN = "10000000-0000-4000-8000-000000000001" +const PLAN_2 = "20000000-0000-4000-8000-000000000002" const state = vi.hoisted(() => ({ handler: (() => undefined) as Handler, @@ -64,6 +65,60 @@ describe("GET /api/cron/plan-notices", () => { expect(state.calls.filter((c) => c.target === "rpc:plan_followups")).toHaveLength(2) }) + it("emits structured zero-PII alerts when either sweep records 'failed' outcomes", async () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}) + state.handler = (call) => { + const after = (op(call, "args")?.[0] as { p_after: string | null }).p_after + if (call.target === "rpc:plan_end_notices") { + if (after === null) return { data: [{ plan_id: PLAN, outcome: "failed" }] } + return { data: [] } + } + if (call.target === "rpc:plan_followups") { + if (after === null) return { data: [{ plan_id: PLAN_2, outcome: "failed" }] } + return { data: [] } + } + return { data: [] } + } + const response = await GET(request(`Bearer ${SECRET}`)) + expect(response.status).toBe(200) + expect(await response.json()).toEqual({ + ended: { failed: 1 }, + followups: { failed: 1 }, + done: true, + }) + + const parsed = error.mock.calls.map(([arg]) => JSON.parse(String(arg)) as Record) + expect(parsed).toContainEqual({ + event: "plan_cron.failed_outcomes", + route: "plan-notices", + job: "plan_end_notices", + failed: 1, + calls: 2, + done: true, + }) + expect(parsed).toContainEqual({ + event: "plan_cron.failed_outcomes", + route: "plan-notices", + job: "plan_followups", + failed: 1, + calls: 2, + done: true, + }) + }) + + it("isolates step failures: plan_followups still runs within budget if plan_end_notices fails", async () => { + vi.spyOn(console, "error").mockImplementation(() => {}) + state.handler = (call) => { + if (call.target === "rpc:plan_end_notices") return { error: { code: "57014", message: "timeout" } } + const after = (op(call, "args")?.[0] as { p_after: string | null }).p_after + return { data: after === null ? [{ plan_id: PLAN, outcome: "reminded" }] : [] } + } + const response = await GET(request(`Bearer ${SECRET}`)) + expect(response.status).toBe(500) + expect(await response.json()).toEqual({ error: "notices_failed" }) + expect(state.calls.filter((c) => c.target === "rpc:plan_followups")).toHaveLength(2) + }) + it("shares one deadline: past the budget the second reminders wait for the next run", async () => { vi.useFakeTimers({ toFake: ["Date"] }) try { diff --git a/apps/web/src/app/api/cron/plan-notices/route.ts b/apps/web/src/app/api/cron/plan-notices/route.ts index 2b0bba4a..812c0609 100644 --- a/apps/web/src/app/api/cron/plan-notices/route.ts +++ b/apps/web/src/app/api/cron/plan-notices/route.ts @@ -2,7 +2,13 @@ import { NextResponse, type NextRequest } from "next/server" import { isAuthorizedCron } from "@/lib/cron" import { dbService } from "@/lib/supabase" -import { remainingBudget, sweepPlans } from "@/server/plans/jobs" +import { + logPlanCronFailures, + logPlanStepFailure, + remainingBudget, + sweepPlans, + type PlanSweep, +} from "@/server/plans/jobs" export const dynamic = "force-dynamic" export const maxDuration = 60 @@ -15,7 +21,8 @@ export const maxDuration = 60 * off, the end date); give.plan_followups sends the second reminder where * the campaign's account allows it (none until the Tingee channel lands). * Both write in-app notices that the outbox turns into emails, exactly once - * each, however often this runs. + * each, however often this runs. Each sweep is isolated so a failure in end + * notices does not block follow-ups within the shared deadline. */ export async function GET(request: NextRequest) { if (!isAuthorizedCron(request.headers.get("authorization"), process.env.CRON_SECRET)) { @@ -23,16 +30,34 @@ export async function GET(request: NextRequest) { } const db = dbService() const started = Date.now() + let ended: PlanSweep | null = null + let followups: PlanSweep | null = null + let endedFailed = false + let followupsFailed = false + try { // Both sweeps share one deadline, so the run ends inside maxDuration. - const ended = await sweepPlans(db, "plan_end_notices") - const followups = await sweepPlans(db, "plan_followups", Date.now, remainingBudget(started)) - return NextResponse.json({ - ended: ended.outcomes, - followups: followups.outcomes, - done: ended.done && followups.done, - }) - } catch { + ended = await sweepPlans(db, "plan_end_notices") + logPlanCronFailures("plan-notices", "plan_end_notices", ended) + } catch (error: unknown) { + endedFailed = true + logPlanStepFailure("plan-notices", "plan_end_notices", error) + } + + try { + followups = await sweepPlans(db, "plan_followups", Date.now, remainingBudget(started)) + logPlanCronFailures("plan-notices", "plan_followups", followups) + } catch (error: unknown) { + followupsFailed = true + logPlanStepFailure("plan-notices", "plan_followups", error) + } + + if (endedFailed || followupsFailed || !ended || !followups) { return NextResponse.json({ error: "notices_failed" }, { status: 500 }) } + return NextResponse.json({ + ended: ended.outcomes, + followups: followups.outcomes, + done: ended.done && followups.done, + }) } diff --git a/apps/web/src/app/me/contributions/PlanList.tsx b/apps/web/src/app/me/contributions/PlanList.tsx index 759fe8be..f5ddd725 100644 --- a/apps/web/src/app/me/contributions/PlanList.tsx +++ b/apps/web/src/app/me/contributions/PlanList.tsx @@ -14,11 +14,14 @@ import { planDayLabel, planDonationPayable, planEndedReasonLabel, + vnMonthStart, } from "@/lib/plans" import { DONATION_TONES, toneOf } from "../_components/tones" import { cancelPlan, updatePlan } from "./actions" +export { vnMonthStart } + export interface MyPlan { id: string campaign_id: string @@ -48,10 +51,16 @@ export function effectiveNextPeriod(nextPeriod: string, month: string): string { return nextPeriod < month ? month : nextPeriod } -/** The first day of the current month in Vietnam time (UTC+7), as YYYY-MM-DD. */ -export function vnMonthStart(now = new Date()): string { - const vn = new Date(now.getTime() + 7 * 60 * 60 * 1000) - return `${vn.getUTCFullYear()}-${String(vn.getUTCMonth() + 1).padStart(2, "0")}-01` +/** + * This month's plan donation for the campaign: a plan cancelled and signed up + * again in the same month keeps the earlier plan's donation. + */ +export function currentPlanDonation( + campaignId: string, + donations: readonly PlanDonation[], + month: string +): PlanDonation | null { + return donations.find((d) => d.plan_id !== null && d.plan_period === month && d.campaign_id === campaignId) ?? null } const DAYS = Array.from({ length: 28 }, (_, i) => i + 1) @@ -91,13 +100,7 @@ export function PlanList({ plan={plan} month={month} now={now} - // This month's donation of the campaign: a plan cancelled and signed - // up again in the same month keeps the earlier plan's donation. - current={ - donations.find( - (d) => d.plan_id !== null && d.plan_period === month && d.campaign_id === plan.campaign_id - ) ?? null - } + current={currentPlanDonation(plan.campaign_id, donations, month)} /> ))} diff --git a/apps/web/src/app/me/contributions/page.test.ts b/apps/web/src/app/me/contributions/page.test.ts index 16b2d9e0..35accfa3 100644 --- a/apps/web/src/app/me/contributions/page.test.ts +++ b/apps/web/src/app/me/contributions/page.test.ts @@ -26,6 +26,7 @@ const state = vi.hoisted(() => ({ calls: [] as Call[], // The service role's give.payment_account: the campaign's usable account. service: (() => undefined) as Handler, + serviceCalls: [] as Call[], })) vi.mock("@/lib/auth/session", () => ({ @@ -33,7 +34,7 @@ vi.mock("@/lib/auth/session", () => ({ })) vi.mock("@/lib/supabase", () => ({ dbAs: () => fakeDb((call) => state.handler(call), state.calls), - dbService: () => fakeDb((call) => state.service(call)), + dbService: () => fakeDb((call) => state.service(call), state.serviceCalls), })) vi.mock("../_components/MePage", () => ({ MePage: ({ children }: { children: ReactNode }) => createElement("main", null, children), @@ -125,10 +126,12 @@ beforeEach(() => { vi.useFakeTimers({ toFake: ["Date"] }) vi.setSystemTime(NOW) state.calls.length = 0 + state.serviceCalls.length = 0 state.service = (call) => (call.target === "rpc:payment_account" ? { data: ACCOUNT } : undefined) }) afterEach(() => { vi.useRealTimers() + vi.restoreAllMocks() }) describe("MyContributionsPage monthly plans (DOS/Give#276)", () => { @@ -247,4 +250,68 @@ describe("MyContributionsPage monthly plans (DOS/Give#276)", () => { ;({ doc } = await render()) expect(doc.body.textContent).toContain("Bạn chưa ủng hộ chiến dịch hay dự án nào.") }) + + it("skips payment_account lookups when no active plan has a payable current-month donation", async () => { + serve( + [ + plan({ id: "a1", campaign_id: CAMPAIGN }), + plan({ id: "a2", campaign_id: OTHER_CAMPAIGN, day_of_month: 25, next_period: "2026-11-01" }), + plan({ id: "a3", campaign_id: "77777777-7777-4777-8777-777777777777", status: "ended" }), + ], + [donation({ campaign_id: CAMPAIGN, status: "received", received_amount: 200000 })] + ) + await render() + expect(state.serviceCalls.filter((c) => c.target === "rpc:payment_account")).toHaveLength(0) + }) + + it("deduplicates payment_account lookups across plans and isolates a failing lookup without logging PII", async () => { + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + const THIRD_CAMPAIGN = "77777777-7777-4777-8777-777777777777" + serve( + [ + plan({ id: PLAN, campaign_id: CAMPAIGN }), + plan({ id: "88888888-8888-4888-8888-888888888888", campaign_id: CAMPAIGN }), + plan({ id: OLD_PLAN, campaign_id: OTHER_CAMPAIGN }), + plan({ id: "99999999-9999-4999-8999-999999999999", campaign_id: THIRD_CAMPAIGN }), + ], + [ + donation({ id: "d1", campaign_id: CAMPAIGN, reference_code: "GVCAMP001" }), + donation({ id: "d2", campaign_id: OTHER_CAMPAIGN, plan_id: OLD_PLAN, reference_code: "GVCAMP002" }), + donation({ + id: "d3", + campaign_id: THIRD_CAMPAIGN, + plan_id: "99999999-9999-4999-8999-999999999999", + reference_code: "GVCAMP003", + }), + ] + ) + state.service = (call) => { + if (call.target !== "rpc:payment_account") return undefined + const id = (call.ops.find(([n]) => n === "args")?.[1] as { p_campaign_id: string }).p_campaign_id + if (id === CAMPAIGN) return { data: ACCOUNT } + if (id === OTHER_CAMPAIGN) return { data: null } + return { error: { code: "57014", message: "donor@example.com secret" } } + } + + const { doc } = await render() + const accountCalls = state.serviceCalls.filter((c) => c.target === "rpc:payment_account") + expect(accountCalls).toHaveLength(3) + const section = planSection(doc)! + // CAMPAIGN has usable account -> shows transfer button + expect(section.querySelector('a[href="/donations/GVCAMP001"]')?.textContent).toBe("Chuyển khoản tháng này") + // OTHER_CAMPAIGN has no usable account -> hides transfer button + expect(section.querySelector('a[href="/donations/GVCAMP002"]')?.textContent).not.toBe("Chuyển khoản tháng này") + // THIRD_CAMPAIGN failed lookup -> stays unknown (shows button; payment page re-checks) + expect(section.querySelector('a[href="/donations/GVCAMP003"]')?.textContent).toBe("Chuyển khoản tháng này") + + expect(errorSpy).toHaveBeenCalledTimes(1) + const logged = JSON.parse(String(errorSpy.mock.calls[0][0])) as Record + expect(logged).toEqual({ + event: "contributions.payment_account_lookup_failed", + campaign_id: THIRD_CAMPAIGN, + code: "57014", + }) + expect(JSON.stringify(logged)).not.toContain("donor@example.com") + expect(JSON.stringify(logged)).not.toContain(USER) + }) }) diff --git a/apps/web/src/app/me/contributions/page.tsx b/apps/web/src/app/me/contributions/page.tsx index 27b3e56a..7acbefde 100644 --- a/apps/web/src/app/me/contributions/page.tsx +++ b/apps/web/src/app/me/contributions/page.tsx @@ -12,17 +12,17 @@ import { SkeletonList } from "@/components/ui/Skeleton" import { requireSession, type Session } from "@/lib/auth/session" import { donationStatusLabel } from "@/lib/contributions" import { formatDate, formatVnd } from "@/lib/format" -import { periodLabel } from "@/lib/plans" +import { periodLabel, planDonationPayable, vnMonthStart } from "@/lib/plans" import { fulfillmentLabel, offersPayment, pledgeListSummary, withoutReplacedPledges } from "@/lib/pledges" import { dbAs, dbService } from "@/lib/supabase" -import { paymentAccount } from "@/server/contributions" +import { paymentAccounts } from "@/server/contributions" import { pledgeBalances, type PledgeBalance } from "@/server/pledges" import { MePage } from "../_components/MePage" import { DONATION_TONES, toneOf } from "../_components/tones" import { RefCode } from "@/components/ui/RefCode" -import { PlanList, type MyPlan } from "./PlanList" +import { currentPlanDonation, PlanList, type MyPlan } from "./PlanList" export const metadata: Metadata = { title: "Ủng hộ của tôi", robots: { index: false } } @@ -60,6 +60,46 @@ export default async function MyContributionsPage() { ) } +/** + * Marks each active plan whose current-month donation is otherwise payable + * with whether its campaign has a usable account (give.payment_account, as + * the service role, deduped and bounded in parallel), so "Chuyển khoản tháng + * này" is offered only when the payment page can show a QR, as + * give.plan_donation_payable decides. Plans without an awaiting current-month + * donation skip the lookup; a failed lookup leaves it unknown and the payment + * page checks again. + */ +async function withUsableAccounts(plans: MyPlan[], donations: readonly MyContribution[], now: Date): Promise { + const month = vnMonthStart(now) + const ids = plans + .filter((p) => { + if (p.status !== "active" || !p.campaign) return false + const current = currentPlanDonation(p.campaign_id, donations, month) + return current !== null && planDonationPayable({ ...current, campaign: p.campaign }, now) + }) + .map((p) => p.campaign_id) + if (ids.length === 0) return plans + + const accounts = await paymentAccounts(dbService(), ids, { + onError: (campaignId, error) => { + const rawCode = (error as { code?: unknown } | null)?.code + const code = typeof rawCode === "string" && rawCode.length > 0 ? rawCode : undefined + console.error( + JSON.stringify({ + event: "contributions.payment_account_lookup_failed", + campaign_id: campaignId, + ...(code ? { code } : {}), + }) + ) + }, + }) + return plans.map((p) => + p.campaign && accounts.has(p.campaign_id) + ? { ...p, campaign: { ...p.campaign, usable_account: accounts.get(p.campaign_id) !== null } } + : p + ) +} + /** * The signed-in user's donations and pledges, read with their own token under * RLS. Pledges that change_pledge replaced are hidden; the current one shows @@ -71,37 +111,6 @@ export default async function MyContributionsPage() { * Monthly plans (DOS/Give#276) come first, also read under RLS; this month's * donation of each is among the newest contributions. */ -/** - * Marks each active plan's live campaign with whether it has a usable - * account (give.payment_account, as the service role, for campaigns the - * donor's own plans name), so "Chuyển khoản tháng này" is offered only when - * the payment page can show a QR, as give.plan_donation_payable decides. A - * failed lookup leaves it unknown; the payment page checks again. - */ -async function withUsableAccounts(plans: MyPlan[]): Promise { - const ids = [ - ...new Set(plans.filter((p) => p.status === "active" && p.campaign?.status === "live").map((p) => p.campaign_id)), - ] - if (ids.length === 0) return plans - const service = dbService() - const usable = new Map( - await Promise.all( - ids.map(async (id) => { - const account = await paymentAccount(service, id).catch((e: unknown) => { - console.error("contributions: payment account lookup failed", e) - return undefined - }) - return [id, account === undefined ? undefined : account !== null] as const - }) - ) - ) - return plans.map((p) => - p.campaign && usable.get(p.campaign_id) !== undefined - ? { ...p, campaign: { ...p.campaign, usable_account: usable.get(p.campaign_id) } } - : p - ) -} - async function ContributionList({ session }: { session: Session }) { const db = dbAs(session.accessToken) const [{ data, error }, plansRead] = await Promise.all([ @@ -126,15 +135,19 @@ async function ContributionList({ session }: { session: Session }) { ]) if (error) throw error if (plansRead.error) throw plansRead.error - const plans = await withUsableAccounts((plansRead.data ?? []) as unknown as MyPlan[]) + const now = new Date() const contributions = withoutReplacedPledges((data ?? []) as unknown as MyContribution[]) - const balances: Map = contributions.some((c) => c.kind === "pledge") - ? await pledgeBalances(dbService(), session.user.id).catch((e: unknown) => { - // The list still renders; pledge lines leave out what refunds could change. - console.error("contributions: pledge balances lookup failed", e) - return new Map() - }) - : new Map() + const planDonations = contributions.filter((c) => c.plan_id !== null) + const [plans, balances] = await Promise.all([ + withUsableAccounts((plansRead.data ?? []) as unknown as MyPlan[], planDonations, now), + contributions.some((c) => c.kind === "pledge") + ? pledgeBalances(dbService(), session.user.id).catch((e: unknown) => { + // The list still renders; pledge lines leave out what refunds could change. + console.error("contributions: pledge balances lookup failed", e) + return new Map() + }) + : Promise.resolve(new Map()), + ]) if (contributions.length === 0 && plans.length === 0) { return ( @@ -150,7 +163,7 @@ async function ContributionList({ session }: { session: Session }) { return (
- {plans.length > 0 && c.plan_id !== null)} />} + {plans.length > 0 && } {contributions.length > 0 && (plans.length > 0 ? (
diff --git a/apps/web/src/lib/plans.test.ts b/apps/web/src/lib/plans.test.ts index 6e2ad301..fd1d5e32 100644 --- a/apps/web/src/lib/plans.test.ts +++ b/apps/web/src/lib/plans.test.ts @@ -10,6 +10,7 @@ import { planStopOneClickPath, planStopPath, planUpdateSchema, + vnMonthStart, } from "./plans" const live = { status: "live", ends_at: "2026-12-20T00:00:00Z" } @@ -20,6 +21,8 @@ describe("plan periods", () => { expect(periodEnd("2026-11-01")?.toISOString()).toBe("2026-11-30T17:00:00.000Z") expect(periodEnd("2026-12-01")?.toISOString()).toBe("2026-12-31T17:00:00.000Z") expect(periodEnd("2026-11-05")).toBeNull() + expect(vnMonthStart(new Date("2026-11-30T16:59:59Z"))).toBe("2026-11-01") + expect(vnMonthStart(new Date("2026-11-30T17:00:00Z"))).toBe("2026-12-01") }) it("shows the QR only while awaiting, live, before ends_at and inside the month", () => { diff --git a/apps/web/src/lib/plans.ts b/apps/web/src/lib/plans.ts index 91496e0c..adf1b098 100644 --- a/apps/web/src/lib/plans.ts +++ b/apps/web/src/lib/plans.ts @@ -51,6 +51,12 @@ export function periodLabel(period: string): string { return match ? `${match[2]}/${match[1]}` : period } +/** The first day of the current month in Vietnam time (UTC+7), as YYYY-MM-DD. */ +export function vnMonthStart(now = new Date()): string { + const vn = new Date(now.getTime() + 7 * 60 * 60 * 1000) + return `${vn.getUTCFullYear()}-${String(vn.getUTCMonth() + 1).padStart(2, "0")}-01` +} + /** The first moment after a plan period's month ends, in Vietnam time (UTC+7, no DST). */ export function periodEnd(period: string): Date | null { const match = /^(\d{4})-(\d{2})-01$/.exec(period) diff --git a/apps/web/src/server/contributions/contributions.test.ts b/apps/web/src/server/contributions/contributions.test.ts index 40371f0c..6528834f 100644 --- a/apps/web/src/server/contributions/contributions.test.ts +++ b/apps/web/src/server/contributions/contributions.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest" +import { describe, expect, it, vi } from "vitest" import { fakeDb, op, type Call } from "@/test/fake-db" @@ -13,7 +13,9 @@ import { guestContribution, guestContributionAccess, ownContribution, + PAYMENT_ACCOUNT_BATCH_CONCURRENCY, paymentAccount, + paymentAccounts, statusTokenHash, } from "." @@ -302,6 +304,71 @@ describe("paymentAccount", () => { }) }) +describe("paymentAccounts", () => { + const row = (accountNumber: string) => ({ + channel: "statement_import", + bank_bin: "970436", + account_number: accountNumber, + account_name: "NGUYEN VAN A", + }) + + it("returns an empty map without touching the database for an empty list", async () => { + const calls: Call[] = [] + const db = fakeDb(() => ({ data: row("0000") }), calls) + expect(await paymentAccounts(db as never, [])).toEqual(new Map()) + expect(calls).toHaveLength(0) + }) + + it("deduplicates campaign ids and isolates per-campaign errors", async () => { + const c1 = "11111111-1111-4111-8111-111111111111" + const c2 = "22222222-2222-4222-8222-222222222222" + const c3 = "33333333-3333-4333-8333-333333333333" + const calls: Call[] = [] + const onError = vi.fn() + const db = fakeDb((call) => { + const id = (op(call, "args")?.[0] as { p_campaign_id: string }).p_campaign_id + if (id === c1) return { data: row("1111") } + if (id === c2) return { data: null } + return { error: { code: "57014", message: "timeout" } } + }, calls) + + const result = await paymentAccounts(db as never, [c1, c2, c1, c3, c2], { onError }) + expect(calls).toHaveLength(3) + expect(result.get(c1)).toEqual({ bank_bin: "970436", account_number: "1111", account_name: "NGUYEN VAN A" }) + expect(result.get(c2)).toBeNull() + expect(result.has(c3)).toBe(false) + expect(onError).toHaveBeenCalledTimes(1) + expect(onError).toHaveBeenCalledWith(c3, expect.objectContaining({ code: "57014" })) + }) + + it("bounds concurrent RPCs to the configured concurrency limit", async () => { + const ids = Array.from({ length: 12 }, (_, i) => `00000000-0000-4000-8000-${String(i).padStart(12, "0")}`) + let inFlight = 0 + let peak = 0 + const db = { + rpc: (_fn: string, args: { p_campaign_id: string }) => ({ + select: () => ({ + maybeSingle: async () => { + inFlight += 1 + peak = Math.max(peak, inFlight) + await new Promise((resolve) => setTimeout(resolve, 5)) + inFlight -= 1 + return { data: row(args.p_campaign_id.slice(-4)), error: null } + }, + }), + }), + } + + const result = await paymentAccounts(db as never, ids) + expect(result.size).toBe(12) + expect(peak).toBeGreaterThan(1) + expect(peak).toBeLessThanOrEqual(PAYMENT_ACCOUNT_BATCH_CONCURRENCY) + + const fallback = await paymentAccounts(db as never, ids.slice(0, 3), { concurrency: Number.NaN }) + expect(fallback.size).toBe(3) + }) +}) + describe("acceptsDonations", () => { const now = new Date("2026-11-10T00:00:00Z") const live = { diff --git a/apps/web/src/server/contributions/index.ts b/apps/web/src/server/contributions/index.ts index 3471e029..25348b53 100644 --- a/apps/web/src/server/contributions/index.ts +++ b/apps/web/src/server/contributions/index.ts @@ -411,3 +411,44 @@ export async function paymentAccount(service: Db, campaignId: string): Promise

void } = {} +): Promise> { + const ids = [...new Set(campaignIds)] + const accounts = new Map() + if (ids.length === 0) return accounts + + const rawConcurrency = options.concurrency + const limit = + typeof rawConcurrency === "number" && Number.isFinite(rawConcurrency) + ? Math.floor(rawConcurrency) + : PAYMENT_ACCOUNT_BATCH_CONCURRENCY + const workers = Math.max(1, Math.min(limit, ids.length)) + let next = 0 + await Promise.all( + Array.from({ length: workers }, async () => { + while (next < ids.length) { + const id = ids[next++]! + try { + accounts.set(id, await paymentAccount(service, id)) + } catch (error: unknown) { + options.onError?.(id, error) + } + } + }) + ) + return accounts +} diff --git a/apps/web/src/server/plans/jobs.test.ts b/apps/web/src/server/plans/jobs.test.ts index b0194bfe..a4c7f341 100644 --- a/apps/web/src/server/plans/jobs.test.ts +++ b/apps/web/src/server/plans/jobs.test.ts @@ -3,7 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest" import type { Db } from "@/lib/campaigns" import { fakeDb, op, type Call } from "@/test/fake-db" -import { expirePlanDonations, PlanJobError, sweepPlans } from "./jobs" +import { EXPIRE_BATCH_SIZE, expirePlanDonations, logPlanCronFailures, PlanJobError, sweepPlans } from "./jobs" const ids = [ "10000000-0000-4000-8000-000000000001", @@ -44,7 +44,7 @@ describe("sweepPlans", () => { expect(sweep.calls).toBe(2) }) - it("counts a plan the database answers as failed, logs it, and goes on to the plans after it", async () => { + it("counts a plan the database answers as failed, logs a structured zero-PII error, and goes on to the plans after it", async () => { const error = vi.spyOn(console, "error").mockImplementation(() => {}) const calls: Call[] = [] const sweep = await sweepPlans( @@ -53,13 +53,37 @@ describe("sweepPlans", () => { ) expect(sweep).toEqual({ outcomes: { issued: 2, failed: 1 }, calls: 4, done: true }) expect(calls.some((c) => c.target === "donation_plans")).toBe(false) - expect(error.mock.calls.some((args) => String(args[0]).includes(ids[1]))).toBe(true) + expect(error).toHaveBeenCalledTimes(1) + expect(JSON.parse(String(error.mock.calls[0][0]))).toEqual({ + event: "plan_job.plan_failed", + job: "issue_plan_donations", + plan_id: ids[1], + }) }) - it("throws when the call itself fails (connection, timeout) or answers unexpectedly", async () => { - vi.spyOn(console, "error").mockImplementation(() => {}) - const failing = fakeDb(() => ({ error: { code: "57014", message: "timeout" } })) as unknown as Db + it("throws when the call itself fails (connection, timeout, thrown) or answers unexpectedly without logging error.message PII", async () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}) + const failing = fakeDb(() => ({ + error: { code: "57014", message: "donor@example.com timeout" }, + })) as unknown as Db await expect(sweepPlans(failing, "plan_followups")).rejects.toBeInstanceOf(PlanJobError) + const logged = JSON.parse(String(error.mock.calls[0][0])) as Record + expect(logged).toEqual({ + event: "plan_job.rpc_error", + job: "plan_followups", + after: null, + code: "57014", + }) + expect(JSON.stringify(logged)).not.toContain("donor@example.com") + + const throwing = { + rpc: () => ({ + abortSignal: () => + Promise.reject(Object.assign(new Error("aborted donor@example.com"), { name: "TimeoutError" })), + }), + } as unknown as Db + await expect(sweepPlans(throwing, "plan_followups")).rejects.toBeInstanceOf(PlanJobError) + for (const data of [null, [{ plan_id: "x", outcome: "issued" }], [{ plan_id: ids[0], outcome: 3 }], "1"]) { const odd = fakeDb(() => ({ data })) as unknown as Db await expect(sweepPlans(odd, "plan_followups")).rejects.toBeInstanceOf(PlanJobError) @@ -73,16 +97,254 @@ describe("sweepPlans", () => { }) }) +describe("logPlanCronFailures", () => { + it("logs a structured zero-PII error only when failed outcomes are present", () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}) + logPlanCronFailures("issue-plan-donations", "issue_plan_donations", { + outcomes: { issued: 3, skipped: 1 }, + calls: 5, + done: true, + }) + expect(error).not.toHaveBeenCalled() + + logPlanCronFailures("issue-plan-donations", "issue_plan_donations", { + outcomes: { issued: 2, failed: 3 }, + calls: 6, + done: true, + }) + expect(error).toHaveBeenCalledTimes(1) + expect(JSON.parse(String(error.mock.calls[0][0]))).toEqual({ + event: "plan_cron.failed_outcomes", + route: "issue-plan-donations", + job: "issue_plan_donations", + failed: 3, + calls: 6, + done: true, + }) + }) +}) + describe("expirePlanDonations", () => { - it("returns the count and throws on failure", async () => { - vi.spyOn(console, "error").mockImplementation(() => {}) - expect(await expirePlanDonations(fakeDb(() => ({ data: 3 })) as unknown as Db)).toBe(3) - await expect(expirePlanDonations(fakeDb(() => ({ data: "3" })) as unknown as Db)).rejects.toBeInstanceOf( - PlanJobError - ) - await expect( - expirePlanDonations(fakeDb(() => ({ error: { code: "XX000", message: "x" } })) as unknown as Db) - ).rejects.toBeInstanceOf(PlanJobError) + it("expires a partial batch in one call and marks done", async () => { + const calls: Call[] = [] + const db = fakeDb(() => ({ data: 3 }), calls) as unknown as Db + expect(await expirePlanDonations(db)).toEqual({ + expired: 3, + batches: 1, + done: true, + stalled: false, + failed: false, + }) + expect(calls.map((c) => c.target)).toEqual(["rpc:expire_plan_donations"]) + }) + + it("drains multiple full batches within the budget", async () => { + const counts = [EXPIRE_BATCH_SIZE, EXPIRE_BATCH_SIZE, 12] + let i = 0 + const db = fakeDb(() => ({ data: counts[i++] ?? 0 })) as unknown as Db + expect(await expirePlanDonations(db)).toEqual({ + expired: EXPIRE_BATCH_SIZE * 2 + 12, + batches: 3, + done: true, + stalled: false, + failed: false, + }) + }) + + it("stops cleanly when the budget is spent between batches or starts at zero", async () => { + let clock = 0 + const db = fakeDb(() => ({ data: EXPIRE_BATCH_SIZE })) as unknown as Db + expect(await expirePlanDonations(db, () => (clock += 20), 50)).toEqual({ + expired: EXPIRE_BATCH_SIZE * 2, + batches: 2, + done: false, + stalled: false, + failed: false, + }) + + const calls: Call[] = [] + expect(await expirePlanDonations(fakeDb(() => ({ data: 1 }), calls) as unknown as Db, Date.now, 0)).toEqual({ + expired: 0, + batches: 0, + done: false, + stalled: false, + failed: false, + }) + expect(calls).toHaveLength(0) + }) + + it("isolates a failing or malformed batch without losing earlier batches and logs zero-PII JSON", async () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}) + let step = 0 + const db = fakeDb(() => { + step += 1 + if (step === 1) return { data: EXPIRE_BATCH_SIZE } + return { error: { code: "XX000", message: "donor@example.com boom" } } + }) as unknown as Db + expect(await expirePlanDonations(db)).toEqual({ + expired: EXPIRE_BATCH_SIZE, + batches: 1, + done: false, + stalled: false, + failed: true, + }) + const logged = JSON.parse(String(error.mock.calls[0][0])) as Record + expect(logged).toEqual({ + event: "plan_job.expire_batch_failed", + job: "expire_plan_donations", + batch: 2, + expired_so_far: EXPIRE_BATCH_SIZE, + code: "XX000", + }) + expect(JSON.stringify(logged)).not.toContain("donor@example.com") + + for (const bad of ["3", null, -1, 1.5]) { + expect(await expirePlanDonations(fakeDb(() => ({ data: bad })) as unknown as Db)).toEqual({ + expired: 0, + batches: 0, + done: false, + stalled: false, + failed: true, + }) + } + + const throwing = { + rpc: () => ({ + abortSignal: () => + Promise.reject(Object.assign(new Error("aborted donor@example.com"), { name: "TimeoutError" })), + }), + } as unknown as Db + expect(await expirePlanDonations(throwing)).toEqual({ + expired: 0, + batches: 0, + done: false, + stalled: false, + failed: true, + }) + const thrownLogged = JSON.parse(String(error.mock.calls.at(-1)?.[0])) as Record + expect(thrownLogged).toEqual({ + event: "plan_job.expire_batch_failed", + job: "expire_plan_donations", + batch: 1, + expired_so_far: 0, + error_name: "TimeoutError", + }) + expect(JSON.stringify(thrownLogged)).not.toContain("donor@example.com") + }) + + it("skips the overdue probe when the budget expired during expire_plan_donations so maxDuration is never breached", async () => { + let clock = 0 + const calls: Call[] = [] + const db = fakeDb((call) => { + if (call.target === "rpc:expire_plan_donations") { + clock = 50 + return { data: 0 } + } + return { data: [{ id: "99999999-9999-4999-8999-999999999999" }] } + }, calls) as unknown as Db + + expect(await expirePlanDonations(db, () => clock, 45)).toEqual({ + expired: 0, + batches: 1, + done: false, + stalled: false, + failed: false, + }) + expect(calls.map((c) => c.target)).toEqual(["rpc:expire_plan_donations"]) + }) + + it("detects a stalled bad batch when expire_plan_donations returns 0 while overdue plan donations remain", async () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}) + const calls: Call[] = [] + const db = fakeDb((call) => { + if (call.target === "rpc:expire_plan_donations") return { data: 0 } + if (call.target === "contributions") return { data: [{ id: "99999999-9999-4999-8999-999999999999" }] } + return undefined + }, calls) as unknown as Db + + expect(await expirePlanDonations(db, () => Date.UTC(2026, 10, 20, 3, 0, 0))).toEqual({ + expired: 0, + batches: 1, + done: false, + stalled: true, + failed: false, + }) + expect(calls.map((c) => c.target)).toEqual(["rpc:expire_plan_donations", "contributions"]) + expect(op(calls[1], "lt")).toEqual(["plan_period", "2026-11-01"]) + expect(error).toHaveBeenCalledTimes(1) + expect(JSON.parse(String(error.mock.calls[0][0]))).toEqual({ + event: "plan_job.expire_batch_stalled", + job: "expire_plan_donations", + batches: 1, + expired: 0, + }) + }) + + it("finishes cleanly when expire_plan_donations returns 0 and no overdue plan donations remain (or probe fails)", async () => { + const error = vi.spyOn(console, "error").mockImplementation(() => {}) + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}) + const cleanDb = fakeDb((call) => { + if (call.target === "rpc:expire_plan_donations") return { data: 0 } + if (call.target === "contributions") return { data: [] } + return undefined + }) as unknown as Db + expect(await expirePlanDonations(cleanDb)).toEqual({ + expired: 0, + batches: 1, + done: true, + stalled: false, + failed: false, + }) + expect(error).not.toHaveBeenCalled() + expect(warn).not.toHaveBeenCalled() + + const probeErrorDb = fakeDb((call) => { + if (call.target === "rpc:expire_plan_donations") return { data: 0 } + return { error: { code: "57014", message: "timeout" } } + }) as unknown as Db + expect(await expirePlanDonations(probeErrorDb)).toEqual({ + expired: 0, + batches: 1, + done: true, + stalled: false, + failed: false, + }) + expect(warn).toHaveBeenCalledTimes(1) + expect(JSON.parse(String(warn.mock.calls[0][0]))).toEqual({ + event: "plan_job.expire_probe_failed", + job: "expire_plan_donations", + code: "57014", + }) + + const probeThrowDb = { + rpc: () => ({ abortSignal: async () => ({ data: 0, error: null }) }), + from: () => ({ + select: () => ({ + not: () => ({ + eq: () => ({ + lt: () => ({ + limit: () => ({ + abortSignal: () => + Promise.reject(Object.assign(new Error("aborted donor@example.com"), { name: "TimeoutError" })), + }), + }), + }), + }), + }), + }), + } as unknown as Db + expect(await expirePlanDonations(probeThrowDb)).toEqual({ + expired: 0, + batches: 1, + done: true, + stalled: false, + failed: false, + }) + expect(JSON.parse(String(warn.mock.calls[1][0]))).toEqual({ + event: "plan_job.expire_probe_failed", + job: "expire_plan_donations", + error_name: "TimeoutError", + }) }) }) diff --git a/apps/web/src/server/plans/jobs.ts b/apps/web/src/server/plans/jobs.ts index ef2bc06e..e8f65d2c 100644 --- a/apps/web/src/server/plans/jobs.ts +++ b/apps/web/src/server/plans/jobs.ts @@ -2,6 +2,7 @@ import "server-only" import type { Db } from "@/lib/campaigns" import { isUuid } from "@/lib/ids" +import { vnMonthStart } from "@/lib/plans" /** * The monthly plan jobs (DOS/Give#276, design 4.3.4 and 5.3). Each database @@ -22,6 +23,8 @@ import { isUuid } from "@/lib/ids" export const PLAN_JOB_BUDGET_MS = 45_000 /** Each call is abandoned after this long; the next run picks the plan up again. */ export const PLAN_CALL_TIMEOUT_MS = 8_000 +/** Maximum rows give.expire_plan_donations() expires in a single RPC call. */ +export const EXPIRE_BATCH_SIZE = 500 /** What is left of a run's budget that started at `started`. */ export function remainingBudget(started: number, now: () => number = Date.now): number { @@ -38,6 +41,17 @@ export interface PlanSweep { done: boolean } +export interface ExpirePlansResult { + expired: number + batches: number + /** False when the budget ran out, a batch failed, or overdue rows stayed un-expired. */ + done: boolean + /** True when expire_plan_donations returned 0 while past-month unpaid plan donations still remain. */ + stalled: boolean + /** True when an expiry batch returned an RPC error, threw, or answered unexpectedly. */ + failed: boolean +} + export class PlanJobError extends Error { constructor( readonly job: PlanJob, @@ -48,6 +62,54 @@ export class PlanJobError extends Error { } } +function errorCode(error: unknown): string | undefined { + const code = (error as { code?: unknown } | null)?.code + return typeof code === "string" && code.length > 0 ? code : undefined +} + +function errorName(error: unknown): string { + return error instanceof Error && error.name ? error.name : "Error" +} + +/** Emits a structured zero-PII error log for Vercel log drains / alerts. */ +function logPlanError(fields: Record) { + console.error(JSON.stringify(fields)) +} + +/** Emits a structured zero-PII warning log for Vercel log drains / alerts. */ +function logPlanWarn(fields: Record) { + console.warn(JSON.stringify(fields)) +} + +/** + * Emits a structured zero-PII alert log whenever a monthly plan cron sweep + * recorded one or more `'failed'` outcomes, so Vercel logs/alerts surface + * isolated per-plan failures clearly. + */ +export function logPlanCronFailures(route: string, job: PlanJob, sweep: PlanSweep): void { + const failed = sweep.outcomes.failed ?? 0 + if (failed <= 0) return + logPlanError({ + event: "plan_cron.failed_outcomes", + route, + job, + failed, + calls: sweep.calls, + done: sweep.done, + }) +} + +/** Emits a structured zero-PII error log for unexpected non-PlanJobError throws in a cron route step. */ +export function logPlanStepFailure(route: string, job: PlanJob, error: unknown): void { + if (error instanceof PlanJobError) return + logPlanError({ + event: "plan_cron.step_failed", + route, + job, + error_name: errorName(error), + }) +} + function parseRow(data: unknown): { plan_id: string; outcome: string } | null | "bad" { if (!Array.isArray(data)) return "bad" if (data.length === 0) return null @@ -68,18 +130,34 @@ export async function sweepPlans( const sweep: PlanSweep = { outcomes: {}, calls: 0, done: false } let after: string | null = null while (now() - started < budgetMs) { - const { data, error } = await db.rpc(job, { p_after: after }).abortSignal(AbortSignal.timeout(PLAN_CALL_TIMEOUT_MS)) + let result: { data: unknown; error: { code?: string; message?: string } | null } + try { + result = await db.rpc(job, { p_after: after }).abortSignal(AbortSignal.timeout(PLAN_CALL_TIMEOUT_MS)) + } catch (thrown: unknown) { + sweep.calls += 1 + const code = errorCode(thrown) + logPlanError({ + event: "plan_job.rpc_threw", + job, + after, + error_name: errorName(thrown), + ...(code ? { code } : {}), + }) + throw new PlanJobError(job, code) + } sweep.calls += 1 + const { data, error } = result if (error) { // A plan's own failure comes back as outcome 'failed' (the database // isolates each plan); an error here is the call itself (connection, // timeout), so the run stops and the next one starts over. - console.error(`${job} failed after plan ${after ?? "(start)"}`, error.code, error.message) - throw new PlanJobError(job, error.code) + const code = errorCode(error) + logPlanError({ event: "plan_job.rpc_error", job, after, ...(code ? { code } : {}) }) + throw new PlanJobError(job, code) } const row = parseRow(data) if (row === "bad") { - console.error(`${job} answered unexpectedly`) + logPlanError({ event: "plan_job.unexpected_response", job, after }) throw new PlanJobError(job, undefined) } if (row === null) { @@ -88,26 +166,126 @@ export async function sweepPlans( } // Ids only grow, so a run never visits a plan twice. if (after !== null && row.plan_id <= after) { - console.error(`${job} did not move past the last plan`) + logPlanError({ event: "plan_job.cursor_stalled", job, after, plan_id: row.plan_id }) throw new PlanJobError(job, undefined) } after = row.plan_id - if (row.outcome === "failed") console.error(`${job}: plan ${row.plan_id} failed; the next run tries it again`) + if (row.outcome === "failed") { + logPlanError({ event: "plan_job.plan_failed", job, plan_id: row.plan_id }) + } sweep.outcomes[row.outcome] = (sweep.outcomes[row.outcome] ?? 0) + 1 } return sweep } -/** issue-plan-donations, second step: past months' unpaid plan donations become 'expired' (listing only). */ -export async function expirePlanDonations(db: Db): Promise { - const { data, error } = await db.rpc("expire_plan_donations").abortSignal(AbortSignal.timeout(PLAN_CALL_TIMEOUT_MS)) - if (error) { - console.error("expire_plan_donations failed", error.code, error.message) - throw new PlanJobError("issue_plan_donations", error.code) +/** + * Checks whether past-month unpaid plan donations still exist after + * give.expire_plan_donations() returned 0 (which happens if a bad row rolled + * back the 500-row SQL batch, or if remaining rows were locked). Never logs + * PII or throws. + */ +async function hasOverduePlanDonations(db: Db, nowMs: number): Promise { + try { + const { data, error } = await db + .from("contributions") + .select("id") + .not("plan_id", "is", null) + .eq("status", "awaiting_transfer") + .lt("plan_period", vnMonthStart(new Date(nowMs))) + .limit(1) + .abortSignal(AbortSignal.timeout(PLAN_CALL_TIMEOUT_MS)) + if (error) { + const code = errorCode(error) + logPlanWarn({ event: "plan_job.expire_probe_failed", job: "expire_plan_donations", ...(code ? { code } : {}) }) + return false + } + return Array.isArray(data) && data.length > 0 + } catch (thrown: unknown) { + const code = errorCode(thrown) + logPlanWarn({ + event: "plan_job.expire_probe_failed", + job: "expire_plan_donations", + error_name: errorName(thrown), + ...(code ? { code } : {}), + }) + return false } - if (typeof data !== "number") { - console.error("expire_plan_donations answered unexpectedly") - throw new PlanJobError("issue_plan_donations", undefined) +} + +/** + * issue-plan-donations, second step: past months' unpaid plan donations become + * 'expired' (listing only). Drains batches of EXPIRE_BATCH_SIZE within the + * remaining budget, isolates a failing/malformed batch without losing counts + * from earlier batches, and detects a stalled bad batch when the RPC returns 0 + * while overdue plan donations still remain. + */ +export async function expirePlanDonations( + db: Db, + now: () => number = Date.now, + budgetMs = PLAN_JOB_BUDGET_MS +): Promise { + const started = now() + let expired = 0 + let batches = 0 + while (now() - started < budgetMs) { + let result: { data: unknown; error: { code?: string; message?: string } | null } + try { + result = await db.rpc("expire_plan_donations").abortSignal(AbortSignal.timeout(PLAN_CALL_TIMEOUT_MS)) + } catch (thrown: unknown) { + const code = errorCode(thrown) + logPlanError({ + event: "plan_job.expire_batch_failed", + job: "expire_plan_donations", + batch: batches + 1, + expired_so_far: expired, + error_name: errorName(thrown), + ...(code ? { code } : {}), + }) + return { expired, batches, done: false, stalled: false, failed: true } + } + const { data, error } = result + if (error) { + const code = errorCode(error) + logPlanError({ + event: "plan_job.expire_batch_failed", + job: "expire_plan_donations", + batch: batches + 1, + expired_so_far: expired, + ...(code ? { code } : {}), + }) + return { expired, batches, done: false, stalled: false, failed: true } + } + if (typeof data !== "number" || !Number.isInteger(data) || data < 0) { + logPlanError({ + event: "plan_job.expire_batch_failed", + job: "expire_plan_donations", + batch: batches + 1, + expired_so_far: expired, + reason: "unexpected_response", + }) + return { expired, batches, done: false, stalled: false, failed: true } + } + batches += 1 + expired += data + if (data === 0) { + // Never start a second DB call after budgetMs so the run stays inside maxDuration. + if (now() - started >= budgetMs) { + return { expired, batches, done: false, stalled: false, failed: false } + } + if (await hasOverduePlanDonations(db, now())) { + logPlanError({ + event: "plan_job.expire_batch_stalled", + job: "expire_plan_donations", + batches, + expired, + }) + return { expired, batches, done: false, stalled: true, failed: false } + } + return { expired, batches, done: true, stalled: false, failed: false } + } + if (data < EXPIRE_BATCH_SIZE) { + return { expired, batches, done: true, stalled: false, failed: false } + } } - return data + return { expired, batches, done: false, stalled: false, failed: false } }