11import { db } from '@sim/db'
22import {
33 member ,
4- organization ,
54 userStats ,
65 user as userTable ,
76 workflow ,
@@ -10,7 +9,11 @@ import {
109import { eq , sql } from 'drizzle-orm'
1110import { v4 as uuidv4 } from 'uuid'
1211import { getHighestPrioritySubscription } from '@/lib/billing/core/subscription'
13- import { checkUsageStatus , maybeSendUsageThresholdEmail } from '@/lib/billing/core/usage'
12+ import {
13+ checkUsageStatus ,
14+ getOrgUsageLimit ,
15+ maybeSendUsageThresholdEmail ,
16+ } from '@/lib/billing/core/usage'
1417import { checkAndBillOverageThreshold } from '@/lib/billing/threshold-billing'
1518import { isBillingEnabled } from '@/lib/core/config/environment'
1619import { redactApiKeys } from '@/lib/core/security/redaction'
@@ -386,29 +389,8 @@ export class ExecutionLogger implements IExecutionLoggerService {
386389 limit,
387390 } )
388391 } else if ( sub ?. referenceId ) {
389- // Fetch org usage limit
390- const orgRows = await db
391- . select ( { orgUsageLimit : organization . orgUsageLimit } )
392- . from ( organization )
393- . where ( eq ( organization . id , sub . referenceId ) )
394- . limit ( 1 )
395-
396- const configured =
397- orgRows . length > 0 && orgRows [ 0 ] . orgUsageLimit
398- ? Number . parseFloat ( orgRows [ 0 ] . orgUsageLimit )
399- : null
400-
401- let orgLimit : number
402- if ( sub . plan === 'enterprise' ) {
403- // Enterprise: Use configured limit directly (no per-seat minimum)
404- orgLimit = configured ?? 0
405- } else {
406- // Team: Use configured limit but never below seats × basePrice
407- const { getPlanPricing } = await import ( '@/lib/billing/core/billing' )
408- const { basePrice } = getPlanPricing ( sub . plan )
409- const minimum = ( sub . seats || 1 ) * basePrice
410- orgLimit = configured !== null ? Math . max ( configured , minimum ) : minimum
411- }
392+ // Get org usage limit using shared helper
393+ const { limit : orgLimit } = await getOrgUsageLimit ( sub . referenceId , sub . plan , sub . seats )
412394
413395 const [ { sum : orgUsageBefore } ] = await db
414396 . select ( { sum : sql `COALESCE(SUM(${ userStats . currentPeriodCost } ), 0)` } )
0 commit comments