@@ -403,54 +403,43 @@ export class ExecutionLogger implements IExecutionLoggerService {
403403 // Apply cost multiplier only to model costs, not base execution charge
404404 const costToStore = costSummary . baseExecutionCharge + costSummary . modelCost * costMultiplier
405405
406- // Upsert user stats record - insert if doesn't exist, update if it does
407- const { getFreeTierLimit } = await import ( '@/lib/billing/subscriptions/utils' )
408- const defaultLimit = getFreeTierLimit ( )
406+ const existing = await db . select ( ) . from ( userStats ) . where ( eq ( userStats . userId , userId ) )
407+ if ( existing . length === 0 ) {
408+ logger . error ( 'User stats record not found - should be created during onboarding' , {
409+ userId,
410+ trigger,
411+ } )
412+ return
413+ }
414+
415+ const updateFields : any = {
416+ totalTokensUsed : sql `total_tokens_used + ${ costSummary . totalTokens } ` ,
417+ totalCost : sql `total_cost + ${ costToStore } ` ,
418+ currentPeriodCost : sql `current_period_cost + ${ costToStore } ` ,
419+ lastActive : new Date ( ) ,
420+ }
409421
410- const triggerIncrements : any = { }
411422 switch ( trigger ) {
412423 case 'manual' :
413- triggerIncrements . totalManualExecutions = sql `total_manual_executions + 1`
424+ updateFields . totalManualExecutions = sql `total_manual_executions + 1`
414425 break
415426 case 'api' :
416- triggerIncrements . totalApiCalls = sql `total_api_calls + 1`
427+ updateFields . totalApiCalls = sql `total_api_calls + 1`
417428 break
418429 case 'webhook' :
419- triggerIncrements . totalWebhookTriggers = sql `total_webhook_triggers + 1`
430+ updateFields . totalWebhookTriggers = sql `total_webhook_triggers + 1`
420431 break
421432 case 'schedule' :
422- triggerIncrements . totalScheduledExecutions = sql `total_scheduled_executions + 1`
433+ updateFields . totalScheduledExecutions = sql `total_scheduled_executions + 1`
423434 break
424435 case 'chat' :
425- triggerIncrements . totalChatExecutions = sql `total_chat_executions + 1`
436+ updateFields . totalChatExecutions = sql `total_chat_executions + 1`
426437 break
427438 }
428439
429- await db
430- . insert ( userStats )
431- . values ( {
432- id : uuidv4 ( ) ,
433- userId : userId ,
434- currentUsageLimit : defaultLimit . toString ( ) ,
435- usageLimitUpdatedAt : new Date ( ) ,
436- totalTokensUsed : costSummary . totalTokens ,
437- totalCost : costToStore ,
438- currentPeriodCost : costToStore ,
439- lastActive : new Date ( ) ,
440- ...triggerIncrements ,
441- } )
442- . onConflictDoUpdate ( {
443- target : userStats . userId ,
444- set : {
445- totalTokensUsed : sql `total_tokens_used + ${ costSummary . totalTokens } ` ,
446- totalCost : sql `total_cost + ${ costToStore } ` ,
447- currentPeriodCost : sql `current_period_cost + ${ costToStore } ` ,
448- lastActive : new Date ( ) ,
449- ...triggerIncrements ,
450- } ,
451- } )
440+ await db . update ( userStats ) . set ( updateFields ) . where ( eq ( userStats . userId , userId ) )
452441
453- logger . debug ( 'Upserted user stats record with cost data' , {
442+ logger . debug ( 'Updated user stats record with cost data' , {
454443 userId,
455444 trigger,
456445 addedCost : costToStore ,
0 commit comments