update: lifetime discounts based on spent
This commit is contained in:
@@ -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" }; }
|
||||
|
||||
Reference in New Issue
Block a user