@@ -29,6 +29,7 @@ import {
2929} from '@/lib/api/contracts/v1/admin'
3030import { parseRequest } from '@/lib/api/server'
3131import { getHighestPrioritySubscription } from '@/lib/billing/core/subscription'
32+ import { getUserUsageData } from '@/lib/billing/core/usage'
3233import { isOrgScopedSubscription } from '@/lib/billing/subscriptions/utils'
3334import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
3435import { withAdminAuthParams } from '@/app/api/v1/admin/middleware'
@@ -78,6 +79,11 @@ export const GET = withRouteHandler(
7879
7980 const [ stats ] = await db . select ( ) . from ( userStats ) . where ( eq ( userStats . userId , userId ) ) . limit ( 1 )
8081
82+ // currentPeriodCost is now only a baseline; canonical current-period usage
83+ // (baseline + attributed usage_log, refresh-adjusted) comes from the same
84+ // helper users see, so admin reflects real usage instead of a stale 0.
85+ const usage = await getUserUsageData ( userId )
86+
8187 const memberOrgs = await db
8288 . select ( {
8389 organizationId : member . organizationId ,
@@ -107,19 +113,14 @@ export const GET = withRouteHandler(
107113 userName : userData . name ,
108114 userEmail : userData . email ,
109115 stripeCustomerId : userData . stripeCustomerId ,
110- totalCost : stats ?. totalCost ?? '0' ,
111116 currentUsageLimit : stats ?. currentUsageLimit ?? null ,
112- currentPeriodCost : stats ?. currentPeriodCost ?? '0' ,
117+ currentPeriodCost : usage . currentUsage . toString ( ) ,
113118 lastPeriodCost : stats ?. lastPeriodCost ?? null ,
114119 billedOverageThisPeriod : stats ?. billedOverageThisPeriod ?? '0' ,
115120 storageUsedBytes : stats ?. storageUsedBytes ?? 0 ,
116- lastActive : stats ?. lastActive ?. toISOString ( ) ?? null ,
117121 billingBlocked : stats ?. billingBlocked ?? false ,
118- totalCopilotCost : stats ?. totalCopilotCost ?? '0' ,
119122 currentPeriodCopilotCost : stats ?. currentPeriodCopilotCost ?? '0' ,
120123 lastPeriodCopilotCost : stats ?. lastPeriodCopilotCost ?? null ,
121- totalCopilotTokens : stats ?. totalCopilotTokens ?? 0 ,
122- totalCopilotCalls : stats ?. totalCopilotCalls ?? 0 ,
123124 subscriptions : subscriptions . map ( toAdminSubscription ) ,
124125 organizationMemberships : memberOrgs . map ( ( m ) => ( {
125126 organizationId : m . organizationId ,
0 commit comments