chore: user deletetion if not solo team owner (#4508) (HSB-497)

This commit is contained in:
Mir Arif Hasan 2024-11-05 21:48:23 +06:00 committed by GitHub
parent 6ad2b5a3bb
commit d3999b9510
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 19 deletions

View File

@ -231,9 +231,8 @@ export class AdminService {
* @returns a count of team members
*/
async membersCountInTeam(teamID: string) {
const teamMembersCount = await this.teamService.getCountOfMembersInTeam(
teamID,
);
const teamMembersCount =
await this.teamService.getCountOfMembersInTeam(teamID);
return teamMembersCount;
}
@ -276,9 +275,8 @@ export class AdminService {
* @returns an array team invitations
*/
async pendingInvitationCountInTeam(teamID: string) {
const invitations = await this.teamInvitationService.getTeamInvitations(
teamID,
);
const invitations =
await this.teamInvitationService.getTeamInvitations(teamID);
return invitations;
}
@ -614,9 +612,8 @@ export class AdminService {
* @returns an Either of boolean or error
*/
async revokeTeamInviteByID(inviteID: string) {
const teamInvite = await this.teamInvitationService.revokeInvitation(
inviteID,
);
const teamInvite =
await this.teamInvitationService.revokeInvitation(inviteID);
if (E.isLeft(teamInvite)) return E.left(teamInvite.left);

View File

@ -38,7 +38,7 @@ export class TeamService implements UserDataHandler, OnModuleInit {
canAllowUserDeletion(user: AuthUser): TO.TaskOption<string> {
return pipe(
this.isUserOwnerRoleInTeams(user.uid),
this.isUserSoleOwnerInAnyTeam(user.uid),
TO.fromTask,
TO.chain((isOwner) => (isOwner ? TO.some(USER_IS_OWNER) : TO.none)),
);
@ -396,18 +396,34 @@ export class TeamService implements UserDataHandler, OnModuleInit {
return teamMember ? teamMember.role : null;
}
isUserOwnerRoleInTeams(uid: string): T.Task<boolean> {
return pipe(
() =>
this.prisma.teamMember.count({
isUserSoleOwnerInAnyTeam(uid: string): T.Task<boolean> {
return async () => {
// Find all teams where the user is an OWNER
const userOwnedTeams = await this.prisma.teamMember.findMany({
where: {
userUid: uid,
role: TeamMemberRole.OWNER,
},
select: {
teamID: true,
},
});
for (const userOwnedTeam of userOwnedTeams) {
const ownerCount = await this.prisma.teamMember.count({
where: {
userUid: uid,
teamID: userOwnedTeam.teamID,
role: TeamMemberRole.OWNER,
},
take: 1,
}),
T.map((count) => count > 0),
);
});
// early return true if the user is the sole owner
if (ownerCount === 1) return true;
}
// return false if the user is not the sole owner in any team
return false;
};
}
deleteUserFromAllTeams(uid: string) {