|
1 | 1 | import { NextRequest, NextResponse } from 'next/server' |
2 | 2 | import { supabaseAdmin } from '@/lib/supabase/admin' |
3 | | -import { deductCredits, checkSufficientBalance } from '@/lib/credits/balance' |
4 | | -import { maybeAutoTopUp } from '@/lib/credits/auto-topup' |
5 | 3 | import { TOKEN_PRICING_EUR, DEFAULT_TOKEN_PRICE_EUR } from '@/lib/constants' |
6 | 4 |
|
7 | 5 | export const runtime = 'nodejs' |
@@ -107,19 +105,16 @@ export async function POST(request: NextRequest) { |
107 | 105 |
|
108 | 106 | const { instanceId, orgId } = resolved |
109 | 107 |
|
110 | | - try { |
111 | | - const hasFunds = await checkSufficientBalance(orgId, 0.001) |
112 | | - if (!hasFunds) { |
113 | | - return NextResponse.json( |
114 | | - { error: 'Insufficient credit balance. Please add credits.' }, |
115 | | - { status: 402 }, |
116 | | - ) |
117 | | - } |
118 | | - } catch (err) { |
119 | | - console.error('Credit check error:', err) |
| 108 | + const { data: org } = await supabaseAdmin |
| 109 | + .from('organizations') |
| 110 | + .select('credit_balance_eur') |
| 111 | + .eq('id', orgId) |
| 112 | + .single() |
| 113 | + |
| 114 | + if (org && Number(org.credit_balance_eur) < 0.001) { |
120 | 115 | return NextResponse.json( |
121 | | - { error: 'Billing service unavailable' }, |
122 | | - { status: 503 }, |
| 116 | + { error: 'Insufficient credit balance. Please add credits.' }, |
| 117 | + { status: 402 }, |
123 | 118 | ) |
124 | 119 | } |
125 | 120 |
|
@@ -256,21 +251,43 @@ function handleStreamingResponse( |
256 | 251 | }) |
257 | 252 | } |
258 | 253 |
|
259 | | -function deductAndMaybeTopUp( |
| 254 | +async function deductAndMaybeTopUp( |
260 | 255 | orgId: string, |
261 | 256 | instanceId: string, |
262 | 257 | model: string, |
263 | 258 | inputTokens: number, |
264 | 259 | outputTokens: number, |
265 | 260 | cost: number, |
266 | | -): void { |
267 | | - deductCredits(orgId, cost, { instanceId, model, inputTokens, outputTokens }) |
268 | | - .then(({ newBalance }) => { |
269 | | - maybeAutoTopUp(orgId, newBalance).catch((err) => |
270 | | - console.error(`Auto top-up error for org ${orgId}:`, err), |
271 | | - ) |
272 | | - }) |
273 | | - .catch((err) => { |
274 | | - console.error(`Credit deduction failed for org ${orgId}:`, err) |
275 | | - }) |
| 261 | +): Promise<void> { |
| 262 | + try { |
| 263 | + const { data: org } = await supabaseAdmin |
| 264 | + .from('organizations') |
| 265 | + .select('credit_balance_eur') |
| 266 | + .eq('id', orgId) |
| 267 | + .single() |
| 268 | + |
| 269 | + if (!org) return |
| 270 | + |
| 271 | + const newBalance = Number(org.credit_balance_eur) - cost |
| 272 | + |
| 273 | + await supabaseAdmin |
| 274 | + .from('organizations') |
| 275 | + .update({ credit_balance_eur: newBalance.toFixed(6) }) |
| 276 | + .eq('id', orgId) |
| 277 | + |
| 278 | + await supabaseAdmin |
| 279 | + .from('credit_transactions') |
| 280 | + .insert({ |
| 281 | + org_id: orgId, |
| 282 | + instance_id: instanceId, |
| 283 | + type: 'usage', |
| 284 | + amount_eur: (-cost).toFixed(6), |
| 285 | + balance_after_eur: newBalance.toFixed(6), |
| 286 | + model, |
| 287 | + input_tokens: inputTokens, |
| 288 | + output_tokens: outputTokens, |
| 289 | + }) |
| 290 | + } catch (err) { |
| 291 | + console.error(`Credit deduction failed for org ${orgId}:`, err) |
| 292 | + } |
276 | 293 | } |
0 commit comments