Skip to content

Commit 1d62ece

Browse files
fix(billing): copilot should directly deduct credit balance (#2294)
1 parent bbab2ff commit 1d62ece

File tree

1 file changed

+4
-11
lines changed
  • apps/sim/app/api/billing/update-cost

1 file changed

+4
-11
lines changed

apps/sim/app/api/billing/update-cost/route.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { userStats } from '@sim/db/schema'
33
import { eq, sql } from 'drizzle-orm'
44
import { type NextRequest, NextResponse } from 'next/server'
55
import { z } from 'zod'
6-
import { deductFromCredits } from '@/lib/billing/credits/balance'
76
import { checkAndBillOverageThreshold } from '@/lib/billing/threshold-billing'
87
import { checkInternalApiKey } from '@/lib/copilot/utils'
98
import { isBillingEnabled } from '@/lib/core/config/environment'
@@ -92,17 +91,11 @@ export async function POST(req: NextRequest) {
9291
return NextResponse.json({ error: 'User stats record not found' }, { status: 500 })
9392
}
9493

95-
const { creditsUsed, overflow } = await deductFromCredits(userId, cost)
96-
if (creditsUsed > 0) {
97-
logger.info(`[${requestId}] Deducted cost from credits`, { userId, creditsUsed, overflow })
98-
}
99-
const costToStore = overflow
100-
10194
const updateFields = {
102-
totalCost: sql`total_cost + ${costToStore}`,
103-
currentPeriodCost: sql`current_period_cost + ${costToStore}`,
104-
totalCopilotCost: sql`total_copilot_cost + ${costToStore}`,
105-
currentPeriodCopilotCost: sql`current_period_copilot_cost + ${costToStore}`,
95+
totalCost: sql`total_cost + ${cost}`,
96+
currentPeriodCost: sql`current_period_cost + ${cost}`,
97+
totalCopilotCost: sql`total_copilot_cost + ${cost}`,
98+
currentPeriodCopilotCost: sql`current_period_copilot_cost + ${cost}`,
10699
totalCopilotCalls: sql`total_copilot_calls + 1`,
107100
lastActive: new Date(),
108101
}

0 commit comments

Comments
 (0)