@@ -12,9 +12,6 @@ const CONSTANTS = {
1212 INITIAL_TEAM_SEATS : 1 ,
1313} as const
1414
15- /**
16- * Handles organization creation for team plans and proper referenceId management
17- */
1815export function useSubscriptionUpgrade ( ) {
1916 const { data : session } = useSession ( )
2017 const betterAuthSubscription = useSubscription ( )
@@ -40,83 +37,43 @@ export function useSubscriptionUpgrade() {
4037
4138 let referenceId = userId
4239
43- // For team plans, create organization first and use its ID as referenceId
4440 if ( targetPlan === 'team' ) {
4541 try {
46- // Check if user already has an organization where they are owner/admin
4742 const orgsResponse = await fetch ( '/api/organizations' )
48- if ( orgsResponse . ok ) {
49- const orgsData = await orgsResponse . json ( )
50- const existingOrg = orgsData . organizations ?. find (
51- ( org : any ) => org . role === 'owner' || org . role === 'admin'
52- )
53-
54- if ( existingOrg ) {
55- logger . info ( 'Using existing organization for team plan upgrade' , {
56- userId,
57- organizationId : existingOrg . id ,
58- } )
59- referenceId = existingOrg . id
60- }
43+ if ( ! orgsResponse . ok ) {
44+ throw new Error ( 'Failed to check organization status' )
6145 }
6246
63- // Only create new organization if no suitable one exists
64- if ( referenceId === userId ) {
65- logger . info ( 'Creating organization for team plan upgrade' , {
66- userId,
67- } )
68-
69- const response = await fetch ( '/api/organizations' , {
70- method : 'POST' ,
71- headers : {
72- 'Content-Type' : 'application/json' ,
73- } ,
74- } )
75-
76- if ( ! response . ok ) {
77- const errorData = await response . json ( ) . catch ( ( ) => ( { } ) )
78- if ( response . status === 409 ) {
79- throw new Error (
80- 'You are already a member of an organization. Please leave it or ask an admin to upgrade.'
81- )
82- }
83- throw new Error (
84- errorData . message || `Failed to create organization: ${ response . statusText } `
85- )
86- }
87- const result = await response . json ( )
47+ const orgsData = await orgsResponse . json ( )
48+ const existingOrg = orgsData . organizations ?. find (
49+ ( org : any ) => org . role === 'owner' || org . role === 'admin'
50+ )
8851
89- logger . info ( 'Organization API response' , {
90- result ,
91- success : result . success ,
92- organizationId : result . organizationId ,
52+ if ( existingOrg ) {
53+ logger . info ( 'Using existing organization for team plan upgrade' , {
54+ userId ,
55+ organizationId : existingOrg . id ,
9356 } )
57+ referenceId = existingOrg . id
9458
95- if ( ! result . success || ! result . organizationId ) {
96- throw new Error ( 'Failed to create organization for team plan' )
59+ try {
60+ await client . organization . setActive ( { organizationId : referenceId } )
61+ logger . info ( 'Set organization as active' , { organizationId : referenceId } )
62+ } catch ( error ) {
63+ logger . warn ( 'Failed to set organization as active, proceeding with upgrade' , {
64+ organizationId : referenceId ,
65+ error : error instanceof Error ? error . message : 'Unknown error' ,
66+ } )
9767 }
98-
99- referenceId = result . organizationId
100- }
101-
102- // Set the organization as active so Better Auth recognizes it
103- try {
104- await client . organization . setActive ( { organizationId : referenceId } )
105-
106- logger . info ( 'Set organization as active' , {
107- organizationId : referenceId ,
108- oldReferenceId : userId ,
109- newReferenceId : referenceId ,
110- } )
111- } catch ( error ) {
112- logger . warn ( 'Failed to set organization as active, but proceeding with upgrade' , {
113- organizationId : referenceId ,
114- error : error instanceof Error ? error . message : 'Unknown error' ,
115- } )
116- // Continue with upgrade even if setting active fails
68+ } else if ( orgsData . isMemberOfAnyOrg ) {
69+ throw new Error (
70+ 'You are already a member of an organization. Please leave it or ask an admin to upgrade.'
71+ )
72+ } else {
73+ logger . info ( 'Will create organization after payment succeeds' , { userId } )
11774 }
11875 } catch ( error ) {
119- logger . error ( 'Failed to prepare organization for team plan' , error )
76+ logger . error ( 'Failed to prepare for team plan upgrade ' , error )
12077 throw error instanceof Error
12178 ? error
12279 : new Error ( 'Failed to prepare team workspace. Please try again or contact support.' )
@@ -134,23 +91,17 @@ export function useSubscriptionUpgrade() {
13491 ...( targetPlan === 'team' && { seats : CONSTANTS . INITIAL_TEAM_SEATS } ) ,
13592 } as const
13693
137- // Add subscriptionId for existing subscriptions to ensure proper plan switching
13894 const finalParams = currentSubscriptionId
13995 ? { ...upgradeParams , subscriptionId : currentSubscriptionId }
14096 : upgradeParams
14197
14298 logger . info (
14399 currentSubscriptionId ? 'Upgrading existing subscription' : 'Creating new subscription' ,
144- {
145- targetPlan,
146- currentSubscriptionId,
147- referenceId,
148- }
100+ { targetPlan, currentSubscriptionId, referenceId }
149101 )
150102
151103 await betterAuthSubscription . upgrade ( finalParams )
152104
153- // If upgrading to team plan, ensure the subscription is transferred to the organization
154105 if ( targetPlan === 'team' && currentSubscriptionId && referenceId !== userId ) {
155106 try {
156107 logger . info ( 'Transferring subscription to organization after upgrade' , {
@@ -174,7 +125,6 @@ export function useSubscriptionUpgrade() {
174125 organizationId : referenceId ,
175126 error : text ,
176127 } )
177- // We don't throw here because the upgrade itself succeeded
178128 } else {
179129 logger . info ( 'Successfully transferred subscription to organization' , {
180130 subscriptionId : currentSubscriptionId ,
@@ -186,21 +136,16 @@ export function useSubscriptionUpgrade() {
186136 }
187137 }
188138
189- // For team plans, refresh organization data to ensure UI updates
190139 if ( targetPlan === 'team' ) {
191140 try {
192141 await queryClient . invalidateQueries ( { queryKey : organizationKeys . lists ( ) } )
193142 logger . info ( 'Refreshed organization data after team upgrade' )
194143 } catch ( error ) {
195144 logger . warn ( 'Failed to refresh organization data after upgrade' , error )
196- // Don't fail the entire upgrade if data refresh fails
197145 }
198146 }
199147
200- logger . info ( 'Subscription upgrade completed successfully' , {
201- targetPlan,
202- referenceId,
203- } )
148+ logger . info ( 'Subscription upgrade completed successfully' , { targetPlan, referenceId } )
204149 } catch ( error ) {
205150 logger . error ( 'Failed to initiate subscription upgrade:' , error )
206151
0 commit comments