@@ -6,8 +6,9 @@ import { and, eq, inArray, ne } from 'drizzle-orm'
66import { calculateSubscriptionOverage , isSubscriptionOrgScoped } from '@/lib/billing/core/billing'
77import { syncUsageLimitsFromSubscription } from '@/lib/billing/core/usage'
88import { restoreUserProSubscription } from '@/lib/billing/organizations/membership'
9- import { isEnterprise , isPaid , isPro , isTeam } from '@/lib/billing/plan-helpers'
9+ import { isEnterprise , isPaid , isPro } from '@/lib/billing/plan-helpers'
1010import { requireStripeClient } from '@/lib/billing/stripe-client'
11+ import { hasOtherEntitledOrganizationSubscription } from '@/lib/billing/subscriptions/organization-coverage'
1112import { ENTITLED_SUBSCRIPTION_STATUSES } from '@/lib/billing/subscriptions/utils'
1213import { stripeWebhookIdempotency } from '@/lib/billing/webhooks/idempotency'
1314import {
@@ -82,32 +83,6 @@ export interface OrganizationDormantTransitionResult {
8283 organizationRetainsTeamOrEnterprise : boolean
8384}
8485
85- /**
86- * Returns true when the organization is still covered by an **active
87- * Team or Enterprise** subscription other than `excludeSubscriptionId`.
88- * The org keeps its team-owned workspaces only while such a sub exists;
89- * a Pro sub on the org does not count.
90- */
91- async function hasOtherActiveTeamOrEnterpriseSubscription (
92- organizationId : string ,
93- excludeSubscriptionId : string | null
94- ) : Promise < boolean > {
95- const filters = [
96- eq ( subscription . referenceId , organizationId ) ,
97- inArray ( subscription . status , ENTITLED_SUBSCRIPTION_STATUSES ) ,
98- ]
99- if ( excludeSubscriptionId ) {
100- filters . push ( ne ( subscription . id , excludeSubscriptionId ) )
101- }
102-
103- const rows = await db
104- . select ( { plan : subscription . plan } )
105- . from ( subscription )
106- . where ( and ( ...filters ) )
107-
108- return rows . some ( ( row ) => isTeam ( row . plan ) || isEnterprise ( row . plan ) )
109- }
110-
11186async function transitionOrganizationToDormantState (
11287 organizationId : string ,
11388 triggeringSubscriptionId : string | null
@@ -117,7 +92,7 @@ async function transitionOrganizationToDormantState(
11792 . from ( member )
11893 . where ( eq ( member . organizationId , organizationId ) )
11994
120- if ( await hasOtherActiveTeamOrEnterpriseSubscription ( organizationId , triggeringSubscriptionId ) ) {
95+ if ( await hasOtherEntitledOrganizationSubscription ( organizationId , triggeringSubscriptionId ) ) {
12196 logger . info (
12297 'Skipping dormant transition - another Team/Enterprise subscription still covers this organization' ,
12398 { organizationId, triggeringSubscriptionId }
0 commit comments