This commit is contained in:
nate 2026-03-24 21:54:41 +04:00
parent 0b1ffa8b3b
commit 1a00d49be2
1 changed files with 25 additions and 19 deletions

View File

@ -320,23 +320,28 @@ export function computeExpiry(acc: AccountState, now: Date): AccountUpdate | nul
// ── DB wrappers ────────────────────────────────────────────────── // ── DB wrappers ──────────────────────────────────────────────────
async function applyPlan(payment: any) { async function applyPlan(payment: any) {
await sql.begin(async (tx) => { try {
const [acc] = await tx` await sql.begin(async (tx) => {
SELECT plan, plan_expires_at, plan_stack FROM accounts WHERE id = ${payment.account_id} FOR UPDATE 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 || [] }, const stack = typeof acc.plan_stack === "string" ? JSON.parse(acc.plan_stack) : (acc.plan_stack || []);
{ plan: payment.plan, months: payment.months }, const update = computeApplyPlan(
new Date() { plan: acc.plan, plan_expires_at: acc.plan_expires_at, plan_stack: stack },
); { plan: payment.plan, months: payment.months },
await tx` new Date()
UPDATE accounts SET plan = ${update.plan}, );
plan_expires_at = ${update.plan_expires_at?.toISOString() ?? null}, await tx`
plan_stack = ${JSON.stringify(update.plan_stack)} UPDATE accounts SET plan = ${update.plan},
WHERE id = ${payment.account_id} 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}`); `;
});
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() { export async function expireProPlans() {
@ -348,15 +353,16 @@ export async function expireProPlans() {
const now = new Date(); const now = new Date();
for (const acc of expired) { for (const acc of expired) {
const stack = typeof acc.plan_stack === "string" ? JSON.parse(acc.plan_stack) : (acc.plan_stack || []);
const update = computeExpiry( 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 now
); );
if (!update) continue; if (!update) continue;
await sql` await sql`
UPDATE accounts SET plan = ${update.plan}, UPDATE accounts SET plan = ${update.plan},
plan_expires_at = ${update.plan_expires_at?.toISOString() ?? null}, 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} WHERE id = ${acc.id}
`; `;
console.log(`Account ${acc.id}: ${acc.plan} expired → ${update.plan}`); console.log(`Account ${acc.id}: ${acc.plan} expired → ${update.plan}`);