4586 fix workspace member feature (#4680)

* Fix import

* Handle delete workspace member consequences

* Add a patch to request deleted workspace member's userId

* Remove useless relations

* Handle delete workspace + refactor

* Add missing migration

* Fix test

* Code review returns

* Add missing operation in migration file

* Fix code review return update

* Fix workspaceMember<>ConnectedAccount relation
This commit is contained in:
martmull
2024-03-28 17:59:48 +01:00
committed by GitHub
parent 00eee3158e
commit 27fdb00d07
14 changed files with 298 additions and 127 deletions

View File

@ -113,4 +113,46 @@ export class UserService extends TypeOrmQueryService<User> {
return user;
}
async handleRemoveWorkspaceMember(workspaceId: string, userId: string) {
await this.userWorkspaceRepository.delete({
userId,
workspaceId,
});
await this.reassignOrRemoveUserDefaultWorkspace(workspaceId, userId);
}
private async reassignOrRemoveUserDefaultWorkspace(
workspaceId: string,
userId: string,
) {
const userWorkspaces = await this.userWorkspaceRepository.find({
where: { userId: userId },
});
if (userWorkspaces.length === 0) {
await this.userRepository.delete({ id: userId });
return;
}
const user = await this.userRepository.findOne({
where: {
id: userId,
},
});
if (!user) {
throw new Error(`User ${userId} not found in workspace ${workspaceId}`);
}
if (user.defaultWorkspaceId === workspaceId) {
await this.userRepository.update(
{ id: userId },
{
defaultWorkspaceId: userWorkspaces[0].workspaceId,
},
);
}
}
}