fix: pay
This commit is contained in:
parent
0b1ffa8b3b
commit
1a00d49be2
|
|
@ -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}`);
|
||||
|
|
|
|||
Loading…
Reference in New Issue