feat: removeUsersAsAdmin mutation added

This commit is contained in:
mirarifhasan
2024-01-29 19:39:56 +06:00
committed by Andrew Bastin
parent da606f5a96
commit 2bde3f8b02
3 changed files with 54 additions and 0 deletions

View File

@@ -186,6 +186,23 @@ export class AdminResolver {
return admin.right;
}
@Mutation(() => Boolean, {
description: 'Remove users as admin',
})
@UseGuards(GqlAuthGuard, GqlAdminGuard)
async removeUsersAsAdmin(
@Args({
name: 'userUIDs',
description: 'users UID',
type: () => [ID],
})
userUIDs: string[],
): Promise<boolean> {
const isUpdated = await this.adminService.removeUsersAsAdmin(userUIDs);
if (E.isLeft(isUpdated)) throwErr(isUpdated.left);
return isUpdated.right;
}
@Mutation(() => Team, {
description:
'Create a new team by providing the user uid to nominate as Team owner',

View File

@@ -454,6 +454,26 @@ export class AdminService {
return E.right(true);
}
/**
* Remove users as admin
* @param userUIDs User UIDs
* @returns an Either of boolean or error
*/
async removeUsersAsAdmin(userUIDs: string[]) {
const adminUsers = await this.userService.fetchAdminUsers();
const adminsNotInArray = adminUsers.filter(
(adminUser) => !userUIDs.includes(adminUser.uid),
);
if (adminsNotInArray.length < 1) {
return E.left(ONLY_ONE_ADMIN_ACCOUNT);
}
const isUpdated = await this.userService.removeUsersAsAdmin(userUIDs);
if (E.isLeft(isUpdated)) return E.left(isUpdated.left);
return E.right(true);
}
/**
* Fetch list of all the Users in org
* @returns number of users in the org

View File

@@ -508,4 +508,21 @@ export class UserService {
return E.left(USER_NOT_FOUND);
}
}
/**
* Change users from an admin by toggling isAdmin param to false
* @param userUIDs user UIDs
* @returns a Either of true or error
*/
async removeUsersAsAdmin(userUIDs: string[]) {
try {
await this.prisma.user.updateMany({
where: { uid: { in: userUIDs } },
data: { isAdmin: false },
});
return E.right(true);
} catch (error) {
return E.left(USER_NOT_FOUND);
}
}
}