update: lifetime discounts based on spent

This commit is contained in:
2026-03-22 06:11:49 +04:00
parent b014cbf98a
commit a1d37c6215
3 changed files with 26 additions and 3 deletions
+8 -1
View File
@@ -74,7 +74,14 @@ export const routes = new Elysia()
// Calculate amount
const planDef = PLANS[plan];
if (!planDef) { set.status = 400; return { error: `Unknown plan: ${plan}` }; }
const amountUsd = planDef.priceUsd ?? (planDef.monthlyUsd! * (months ?? 1));
let amountUsd = planDef.priceUsd ?? (planDef.monthlyUsd! * (months ?? 1));
// Lifetime discount: credit up to 50% of lifetime price from previous payments
if (plan === "lifetime" && planDef.priceUsd) {
const [{ total }] = await sql`SELECT COALESCE(SUM(amount_usd), 0)::numeric as total FROM payments WHERE account_id = ${accountId} AND status = 'paid'`;
const credit = Math.min(Number(total), planDef.priceUsd * 0.5);
amountUsd = Math.max(amountUsd - credit, 1);
}
const rates = await getExchangeRates();
const rate = rates[coin];
if (!rate) { set.status = 500; return { error: "Could not fetch exchange rate" }; }