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 * @returns a count of team members
*/ */
async membersCountInTeam(teamID: string) { async membersCountInTeam(teamID: string) {
const teamMembersCount = await this.teamService.getCountOfMembersInTeam( const teamMembersCount =
teamID, await this.teamService.getCountOfMembersInTeam(teamID);
);
return teamMembersCount; return teamMembersCount;
} }
@ -276,9 +275,8 @@ export class AdminService {
* @returns an array team invitations * @returns an array team invitations
*/ */
async pendingInvitationCountInTeam(teamID: string) { async pendingInvitationCountInTeam(teamID: string) {
const invitations = await this.teamInvitationService.getTeamInvitations( const invitations =
teamID, await this.teamInvitationService.getTeamInvitations(teamID);
);
return invitations; return invitations;
} }
@ -614,9 +612,8 @@ export class AdminService {
* @returns an Either of boolean or error * @returns an Either of boolean or error
*/ */
async revokeTeamInviteByID(inviteID: string) { async revokeTeamInviteByID(inviteID: string) {
const teamInvite = await this.teamInvitationService.revokeInvitation( const teamInvite =
inviteID, await this.teamInvitationService.revokeInvitation(inviteID);
);
if (E.isLeft(teamInvite)) return E.left(teamInvite.left); 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> { canAllowUserDeletion(user: AuthUser): TO.TaskOption<string> {
return pipe( return pipe(
this.isUserOwnerRoleInTeams(user.uid), this.isUserSoleOwnerInAnyTeam(user.uid),
TO.fromTask, TO.fromTask,
TO.chain((isOwner) => (isOwner ? TO.some(USER_IS_OWNER) : TO.none)), 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; return teamMember ? teamMember.role : null;
} }
isUserOwnerRoleInTeams(uid: string): T.Task<boolean> { isUserSoleOwnerInAnyTeam(uid: string): T.Task<boolean> {
return pipe( return async () => {
() => // Find all teams where the user is an OWNER
this.prisma.teamMember.count({ 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: { where: {
userUid: uid, teamID: userOwnedTeam.teamID,
role: TeamMemberRole.OWNER, 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) { deleteUserFromAllTeams(uid: string) {