From 2bde3f8b02320c0b1d4238329ea520c89c6a293b Mon Sep 17 00:00:00 2001 From: mirarifhasan Date: Mon, 29 Jan 2024 19:39:56 +0600 Subject: [PATCH] feat: removeUsersAsAdmin mutation added --- .../src/admin/admin.resolver.ts | 17 ++++++++++++++++ .../src/admin/admin.service.ts | 20 +++++++++++++++++++ .../src/user/user.service.ts | 17 ++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/packages/hoppscotch-backend/src/admin/admin.resolver.ts b/packages/hoppscotch-backend/src/admin/admin.resolver.ts index 23e3fa9d8..409b0b580 100644 --- a/packages/hoppscotch-backend/src/admin/admin.resolver.ts +++ b/packages/hoppscotch-backend/src/admin/admin.resolver.ts @@ -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 { + 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', diff --git a/packages/hoppscotch-backend/src/admin/admin.service.ts b/packages/hoppscotch-backend/src/admin/admin.service.ts index cfdb9cef4..6df2a4e82 100644 --- a/packages/hoppscotch-backend/src/admin/admin.service.ts +++ b/packages/hoppscotch-backend/src/admin/admin.service.ts @@ -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 diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index 73d1fb996..ff3b238d9 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -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); + } + } }