Skip to content

Commit 1233b06

Browse files
fix: use supabase REST for credit check/deduction in proxy
Drizzle ORM requires a direct Postgres connection which may not be available in all Vercel serverless environments. Switch to supabaseAdmin REST client which always works. Made-with: Cursor
1 parent 192f879 commit 1233b06

1 file changed

Lines changed: 42 additions & 25 deletions

File tree

src/app/api/gateway/proxy/route.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { NextRequest, NextResponse } from 'next/server'
22
import { supabaseAdmin } from '@/lib/supabase/admin'
3-
import { deductCredits, checkSufficientBalance } from '@/lib/credits/balance'
4-
import { maybeAutoTopUp } from '@/lib/credits/auto-topup'
53
import { TOKEN_PRICING_EUR, DEFAULT_TOKEN_PRICE_EUR } from '@/lib/constants'
64

75
export const runtime = 'nodejs'
@@ -107,19 +105,16 @@ export async function POST(request: NextRequest) {
107105

108106
const { instanceId, orgId } = resolved
109107

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) {
120115
return NextResponse.json(
121-
{ error: 'Billing service unavailable' },
122-
{ status: 503 },
116+
{ error: 'Insufficient credit balance. Please add credits.' },
117+
{ status: 402 },
123118
)
124119
}
125120

@@ -256,21 +251,43 @@ function handleStreamingResponse(
256251
})
257252
}
258253

259-
function deductAndMaybeTopUp(
254+
async function deductAndMaybeTopUp(
260255
orgId: string,
261256
instanceId: string,
262257
model: string,
263258
inputTokens: number,
264259
outputTokens: number,
265260
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+
}
276293
}

0 commit comments

Comments
 (0)