@@ -56,6 +56,7 @@ async function createOrganizationWithOwner(
5656 metadata : Record < string , any > = { }
5757) : Promise < string > {
5858 const orgId = `org_${ crypto . randomUUID ( ) } `
59+ let sessionsUpdated = 0
5960
6061 await db . transaction ( async ( tx ) => {
6162 await tx . insert ( organization ) . values ( {
@@ -71,19 +72,21 @@ async function createOrganizationWithOwner(
7172 organizationId : orgId ,
7273 role : 'owner' ,
7374 } )
74- } )
7575
76- const updatedSessions = await db
77- . update ( session )
78- . set ( { activeOrganizationId : orgId } )
79- . where ( eq ( session . userId , userId ) )
80- . returning ( { id : session . id } )
76+ const updatedSessions = await tx
77+ . update ( session )
78+ . set ( { activeOrganizationId : orgId } )
79+ . where ( eq ( session . userId , userId ) )
80+ . returning ( { id : session . id } )
81+
82+ sessionsUpdated = updatedSessions . length
83+ } )
8184
8285 logger . info ( 'Created organization with owner' , {
8386 userId,
8487 organizationId : orgId ,
8588 organizationName,
86- sessionsUpdated : updatedSessions . length ,
89+ sessionsUpdated,
8790 } )
8891
8992 return orgId
@@ -161,15 +164,17 @@ export async function ensureOrganizationForTeamSubscription(
161164 organizationId : membership . organizationId ,
162165 } )
163166
164- await db
165- . update ( subscriptionTable )
166- . set ( { referenceId : membership . organizationId } )
167- . where ( eq ( subscriptionTable . id , subscription . id ) )
167+ await db . transaction ( async ( tx ) => {
168+ await tx
169+ . update ( subscriptionTable )
170+ . set ( { referenceId : membership . organizationId } )
171+ . where ( eq ( subscriptionTable . id , subscription . id ) )
168172
169- await db
170- . update ( session )
171- . set ( { activeOrganizationId : membership . organizationId } )
172- . where ( eq ( session . userId , userId ) )
173+ await tx
174+ . update ( session )
175+ . set ( { activeOrganizationId : membership . organizationId } )
176+ . where ( eq ( session . userId , userId ) )
177+ } )
173178
174179 return { ...subscription , referenceId : membership . organizationId }
175180 }
0 commit comments