@@ -10,7 +10,6 @@ import {
1010 account ,
1111 credential ,
1212 invitation ,
13- knowledgeBase ,
1413 member ,
1514 organization ,
1615 permissionGroupMember ,
@@ -19,7 +18,6 @@ import {
1918 user ,
2019 userStats ,
2120 workspace ,
22- workspaceFiles ,
2321} from '@sim/db/schema'
2422import { createLogger } from '@sim/logger'
2523import { getErrorMessage } from '@sim/utils/errors'
@@ -39,6 +37,7 @@ import {
3937import { acquireUserBillingIdentityLock } from '@/lib/billing/organizations/billing-identity-lock'
4038import { setOrgMemberUsageLimit } from '@/lib/billing/organizations/member-limits'
4139import { MEMBER_BILLING_RECONCILIATION_EVENT_TYPE } from '@/lib/billing/organizations/membership-reconciliation-event'
40+ import { reassignOrganizationSharedResourcesTx } from '@/lib/billing/organizations/resource-handoff'
4241import { isPaid , sqlIsPro } from '@/lib/billing/plan-helpers'
4342import { changeOrganizationWorkspaceBilledAccountsInTx } from '@/lib/billing/storage/payer-transfer'
4443import {
@@ -67,10 +66,8 @@ import {
6766} from '@/lib/organizations/members/revocation'
6867import { reassignOrganizationProjects } from '@/lib/projects/membership'
6968import { removeWorkspaceSkillMembershipsTx } from '@/lib/skills/access'
70- import {
71- reassignWorkflowOwnershipForWorkspaceMemberRemovalTx ,
72- WorkspaceBillingAccountRemovalError ,
73- } from '@/lib/workspaces/utils'
69+ import { reassignSharedResourceOwnershipForWorkspaceMemberRemovalTx } from '@/lib/workspaces/resource-handoff'
70+ import { WorkspaceBillingAccountRemovalError } from '@/lib/workspaces/utils'
7471import { endDirectoryMembershipTx } from '@/ee/scim/lib/identity/end-directory-membership'
7572
7673export { acquireUserBillingIdentityLock } from '@/lib/billing/organizations/billing-identity-lock'
@@ -571,14 +568,8 @@ async function reassignOwnedOrganizationResourcesTx({
571568 organizationId : string
572569 workspaceIds : string [ ]
573570} ) {
574- const [ ownerMembership ] = await tx
575- . select ( { userId : member . userId } )
576- . from ( member )
577- . where ( and ( eq ( member . organizationId , organizationId ) , eq ( member . role , 'owner' ) ) )
578- . limit ( 1 )
579-
580- const ownerId = ownerMembership ?. userId
581- if ( ! ownerId || ownerId === userId ) return 0
571+ const ownerId = await reassignOrganizationSharedResourcesTx ( tx , organizationId , userId )
572+ if ( ! ownerId ) return 0
582573
583574 await reassignOrganizationProjects ( tx , {
584575 organizationId,
@@ -587,28 +578,6 @@ async function reassignOwnedOrganizationResourcesTx({
587578 workspaceIds,
588579 } )
589580
590- /** Creator attribution must survive account deletion without changing document ACLs. */
591- await tx
592- . update ( knowledgeBase )
593- . set ( { userId : ownerId , updatedAt : new Date ( ) } )
594- . where (
595- and (
596- eq ( knowledgeBase . organizationId , organizationId ) ,
597- isNull ( knowledgeBase . workspaceId ) ,
598- eq ( knowledgeBase . userId , userId )
599- )
600- )
601- await tx
602- . update ( workspaceFiles )
603- . set ( { userId : ownerId , updatedAt : new Date ( ) } )
604- . where (
605- and (
606- eq ( workspaceFiles . organizationId , organizationId ) ,
607- isNull ( workspaceFiles . workspaceId ) ,
608- eq ( workspaceFiles . userId , userId )
609- )
610- )
611-
612581 if ( workspaceIds . length === 0 ) return 0
613582
614583 const reassignedWorkspaces = await tx
@@ -1239,13 +1208,13 @@ export async function transferUserBetweenOrganizations(
12391208 workspaceIds,
12401209 } )
12411210 if ( workspaceIds . length > 0 ) {
1242- const workflowOwnershipReassignment =
1243- await reassignWorkflowOwnershipForWorkspaceMemberRemovalTx ( {
1211+ const resourceOwnershipReassignment =
1212+ await reassignSharedResourceOwnershipForWorkspaceMemberRemovalTx ( {
12441213 tx,
12451214 workspaceIds,
12461215 departingUserId : params . userId ,
12471216 } )
1248- if ( workflowOwnershipReassignment . unresolved . length > 0 ) {
1217+ if ( resourceOwnershipReassignment . unresolved . length > 0 ) {
12491218 throw new WorkspaceBillingAccountRemovalError ( )
12501219 }
12511220 const deletedPermissions = await tx
@@ -1468,13 +1437,13 @@ export async function removeUserFromOrganization(
14681437 }
14691438 }
14701439
1471- const workflowOwnershipReassignment =
1472- await reassignWorkflowOwnershipForWorkspaceMemberRemovalTx ( {
1440+ const resourceOwnershipReassignment =
1441+ await reassignSharedResourceOwnershipForWorkspaceMemberRemovalTx ( {
14731442 tx,
14741443 workspaceIds,
14751444 departingUserId : userId ,
14761445 } )
1477- if ( workflowOwnershipReassignment . unresolved . length > 0 ) {
1446+ if ( resourceOwnershipReassignment . unresolved . length > 0 ) {
14781447 throw new WorkspaceBillingAccountRemovalError ( )
14791448 }
14801449
@@ -1685,13 +1654,13 @@ export async function removeExternalUserFromOrganizationWorkspaces(params: {
16851654 }
16861655 }
16871656
1688- const workflowOwnershipReassignment =
1689- await reassignWorkflowOwnershipForWorkspaceMemberRemovalTx ( {
1657+ const resourceOwnershipReassignment =
1658+ await reassignSharedResourceOwnershipForWorkspaceMemberRemovalTx ( {
16901659 tx,
16911660 workspaceIds,
16921661 departingUserId : userId ,
16931662 } )
1694- if ( workflowOwnershipReassignment . unresolved . length > 0 ) {
1663+ if ( resourceOwnershipReassignment . unresolved . length > 0 ) {
16951664 throw new WorkspaceBillingAccountRemovalError ( )
16961665 }
16971666
0 commit comments