mirror of
https://github.com/hoppscotch/hoppscotch
synced 2024-11-21 14:38:47 +00:00
chore: user deletetion if not solo team owner (#4508) (HSB-497)
This commit is contained in:
parent
6ad2b5a3bb
commit
d3999b9510
@ -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);
|
||||
|
||||
|
@ -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,
|
||||
},
|
||||
take: 1,
|
||||
}),
|
||||
T.map((count) => count > 0),
|
||||
);
|
||||
select: {
|
||||
teamID: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const userOwnedTeam of userOwnedTeams) {
|
||||
const ownerCount = await this.prisma.teamMember.count({
|
||||
where: {
|
||||
teamID: userOwnedTeam.teamID,
|
||||
role: TeamMemberRole.OWNER,
|
||||
},
|
||||
});
|
||||
|
||||
// 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) {
|
||||
|
Loading…
Reference in New Issue
Block a user