feat(billing): add billing_mode to providers (postpaid support) & UI/migration
Some checks failed
CI / Check (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Formatting (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Release Build (push) Has been cancelled

This commit is contained in:
2026-03-03 15:37:19 -05:00
parent 75b68521c8
commit 1453e64d4b
4 changed files with 77 additions and 35 deletions

View File

@@ -100,13 +100,18 @@ impl RequestLogger {
.execute(&mut *tx)
.await?;
// Deduct from provider balance if successful (skip postpaid like Gemini)
if log.cost > 0.0 && log.provider != "gemini" {
sqlx::query("UPDATE provider_configs SET credit_balance = credit_balance - ? WHERE id = ?")
.bind(log.cost)
.bind(&log.provider)
.execute(&mut *tx)
.await?;
// Deduct from provider balance if successful.
// Providers configured with billing_mode = 'postpaid' will not have their
// credit_balance decremented. Use a conditional UPDATE so we don't need
// a prior SELECT and avoid extra round-trips.
if log.cost > 0.0 {
sqlx::query(
"UPDATE provider_configs SET credit_balance = credit_balance - ? WHERE id = ? AND (billing_mode IS NULL OR billing_mode != 'postpaid')",
)
.bind(log.cost)
.bind(&log.provider)
.execute(&mut *tx)
.await?;
}
tx.commit().await?;