The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { Injectable } from '@nestjs/common';
import { isDefined } from 'twenty-shared/utils';
import { type UserWorkspaceEntity } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
import { ConnectedAccountMetadataService } from 'src/engine/metadata-modules/connected-account/connected-account-metadata.service';
import { UserRoleService } from 'src/engine/permissions/role-assignment/user-role.service';
@Injectable()
export class ConnectedAccountOwnershipTransferService {
constructor(
private readonly userRoleService: UserRoleService,
private readonly connectedAccountMetadataService: ConnectedAccountMetadataService,
) {}
// Reassigns app connections owned by a departing member to another member
// instead of leaving them orphaned against a userWorkspaceId that's about
// to be deleted. Every path that removes a member from a workspace while
// it stays active must call this before deleting the userWorkspace.
async transferConnectedAccountsOwnershipToCustodian({
removedUserWorkspace,
actingUserWorkspaceId,
}: {
removedUserWorkspace: UserWorkspaceEntity;
actingUserWorkspaceId?: string;
}) {
const custodianUserWorkspace =
await this.userRoleService.resolveCustodianUserWorkspace({
removedUserWorkspace,
actingUserWorkspaceId,
});
if (isDefined(custodianUserWorkspace)) {
await this.connectedAccountMetadataService.transferOwnership({
fromUserWorkspaceId: removedUserWorkspace.id,
toUserWorkspaceId: custodianUserWorkspace.id,
workspaceId: removedUserWorkspace.workspaceId,
});
}
}
}