@@ -2,43 +2,23 @@ import crypto from 'crypto'
22import { eq , sql } from 'drizzle-orm'
33import { type NextRequest , NextResponse } from 'next/server'
44import { z } from 'zod'
5+ import { checkInternalApiKey } from '@/lib/copilot/utils'
56import { env } from '@/lib/env'
6- import { isProd } from '@/lib/environment'
7+ import { isBillingEnabled , isProd } from '@/lib/environment'
78import { createLogger } from '@/lib/logs/console/logger'
89import { db } from '@/db'
910import { userStats } from '@/db/schema'
1011import { calculateCost } from '@/providers/utils'
1112
1213const logger = createLogger ( 'billing-update-cost' )
1314
14- // Schema for the request body
1515const UpdateCostSchema = z . object ( {
1616 userId : z . string ( ) . min ( 1 , 'User ID is required' ) ,
1717 input : z . number ( ) . min ( 0 , 'Input tokens must be a non-negative number' ) ,
1818 output : z . number ( ) . min ( 0 , 'Output tokens must be a non-negative number' ) ,
1919 model : z . string ( ) . min ( 1 , 'Model is required' ) ,
2020} )
2121
22- // Authentication function (reused from copilot/methods route)
23- function checkInternalApiKey ( req : NextRequest ) {
24- const apiKey = req . headers . get ( 'x-api-key' )
25- const expectedApiKey = env . INTERNAL_API_SECRET
26-
27- if ( ! expectedApiKey ) {
28- return { success : false , error : 'Internal API key not configured' }
29- }
30-
31- if ( ! apiKey ) {
32- return { success : false , error : 'API key required' }
33- }
34-
35- if ( apiKey !== expectedApiKey ) {
36- return { success : false , error : 'Invalid API key' }
37- }
38-
39- return { success : true }
40- }
41-
4222/**
4323 * POST /api/billing/update-cost
4424 * Update user cost based on token usage with internal API key auth
@@ -50,6 +30,19 @@ export async function POST(req: NextRequest) {
5030 try {
5131 logger . info ( `[${ requestId } ] Update cost request started` )
5232
33+ if ( ! isBillingEnabled ) {
34+ logger . debug ( `[${ requestId } ] Billing is disabled, skipping cost update` )
35+ return NextResponse . json ( {
36+ success : true ,
37+ message : 'Billing disabled, cost update skipped' ,
38+ data : {
39+ billingEnabled : false ,
40+ processedAt : new Date ( ) . toISOString ( ) ,
41+ requestId,
42+ } ,
43+ } )
44+ }
45+
5346 // Check authentication (internal API key)
5447 const authResult = checkInternalApiKey ( req )
5548 if ( ! authResult . success ) {
0 commit comments