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 ──────────────────────────────────────────────────
|
// ── DB wrappers ──────────────────────────────────────────────────
|
||||||
|
|
||||||
async function applyPlan(payment: any) {
|
async function applyPlan(payment: any) {
|
||||||
|
try {
|
||||||
await sql.begin(async (tx) => {
|
await sql.begin(async (tx) => {
|
||||||
const [acc] = await tx`
|
const [acc] = await tx`
|
||||||
SELECT plan, plan_expires_at, plan_stack FROM accounts WHERE id = ${payment.account_id} FOR UPDATE
|
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(
|
const update = computeApplyPlan(
|
||||||
{ 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 },
|
||||||
{ plan: payment.plan, months: payment.months },
|
{ plan: payment.plan, months: payment.months },
|
||||||
new Date()
|
new Date()
|
||||||
);
|
);
|
||||||
await tx`
|
await tx`
|
||||||
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 = ${payment.account_id}
|
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}`);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue