From 1a00d49be28646f7be3cf8a1843c4dabf2ba4427 Mon Sep 17 00:00:00 2001 From: nate Date: Tue, 24 Mar 2026 21:54:41 +0400 Subject: [PATCH] fix: pay --- apps/pay/src/monitor.ts | 44 +++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/apps/pay/src/monitor.ts b/apps/pay/src/monitor.ts index 51c16ed..02c8e6f 100644 --- a/apps/pay/src/monitor.ts +++ b/apps/pay/src/monitor.ts @@ -320,23 +320,28 @@ export function computeExpiry(acc: AccountState, now: Date): AccountUpdate | nul // ── DB wrappers ────────────────────────────────────────────────── async function applyPlan(payment: any) { - await sql.begin(async (tx) => { - const [acc] = await tx` - SELECT plan, plan_expires_at, plan_stack FROM accounts WHERE id = ${payment.account_id} FOR UPDATE - `; - const update = computeApplyPlan( - { plan: acc.plan, plan_expires_at: acc.plan_expires_at, plan_stack: acc.plan_stack || [] }, - { plan: payment.plan, months: payment.months }, - new Date() - ); - await tx` - UPDATE accounts SET plan = ${update.plan}, - plan_expires_at = ${update.plan_expires_at?.toISOString() ?? null}, - plan_stack = ${JSON.stringify(update.plan_stack)} - WHERE id = ${payment.account_id} - `; - }); - console.log(`Payment ${payment.id} activated: ${payment.plan} for account ${payment.account_id}`); + try { + await sql.begin(async (tx) => { + const [acc] = await tx` + SELECT plan, plan_expires_at, plan_stack FROM accounts WHERE id = ${payment.account_id} FOR UPDATE + `; + const stack = typeof acc.plan_stack === "string" ? JSON.parse(acc.plan_stack) : (acc.plan_stack || []); + const update = computeApplyPlan( + { plan: acc.plan, plan_expires_at: acc.plan_expires_at, plan_stack: stack }, + { plan: payment.plan, months: payment.months }, + new Date() + ); + await tx` + UPDATE accounts SET plan = ${update.plan}, + plan_expires_at = ${update.plan_expires_at?.toISOString() ?? null}, + plan_stack = ${sql.json(update.plan_stack)} + WHERE id = ${payment.account_id} + `; + }); + console.log(`Payment ${payment.id} activated: ${payment.plan} for account ${payment.account_id}`); + } catch (e) { + console.error(`applyPlan failed for payment ${payment.id}:`, e); + } } export async function expireProPlans() { @@ -348,15 +353,16 @@ export async function expireProPlans() { const now = new Date(); for (const acc of expired) { + const stack = typeof acc.plan_stack === "string" ? JSON.parse(acc.plan_stack) : (acc.plan_stack || []); const update = computeExpiry( - { plan: acc.plan, plan_expires_at: acc.plan_expires_at, plan_stack: acc.plan_stack || [] }, + { plan: acc.plan, plan_expires_at: acc.plan_expires_at, plan_stack: stack }, now ); if (!update) continue; await sql` UPDATE accounts SET plan = ${update.plan}, plan_expires_at = ${update.plan_expires_at?.toISOString() ?? null}, - plan_stack = ${JSON.stringify(update.plan_stack)} + plan_stack = ${sql.json(update.plan_stack)} WHERE id = ${acc.id} `; console.log(`Account ${acc.id}: ${acc.plan} expired → ${update.plan}`);