diff --git a/packages/hoppscotch-backend/src/admin/admin.resolver.ts b/packages/hoppscotch-backend/src/admin/admin.resolver.ts index 5f1308898..23e3fa9d8 100644 --- a/packages/hoppscotch-backend/src/admin/admin.resolver.ts +++ b/packages/hoppscotch-backend/src/admin/admin.resolver.ts @@ -109,6 +109,7 @@ export class AdminResolver { if (E.isLeft(invitedUser)) throwErr(invitedUser.left); return invitedUser.right; } + @Mutation(() => Boolean, { description: 'Make user an admin', }) @@ -126,6 +127,23 @@ export class AdminResolver { return admin.right; } + @Mutation(() => Boolean, { + description: 'Make users an admin', + }) + @UseGuards(GqlAuthGuard, GqlAdminGuard) + async makeUsersAdmin( + @Args({ + name: 'userUIDs', + description: 'users UID', + type: () => [ID], + }) + userUIDs: string[], + ): Promise { + const isUpdated = await this.adminService.makeUsersAdmin(userUIDs); + if (E.isLeft(isUpdated)) throwErr(isUpdated.left); + return isUpdated.right; + } + @Mutation(() => Boolean, { description: 'Update user display name', }) diff --git a/packages/hoppscotch-backend/src/admin/admin.service.ts b/packages/hoppscotch-backend/src/admin/admin.service.ts index 672a829fb..cfdb9cef4 100644 --- a/packages/hoppscotch-backend/src/admin/admin.service.ts +++ b/packages/hoppscotch-backend/src/admin/admin.service.ts @@ -429,6 +429,17 @@ export class AdminService { return E.right(true); } + /** + * Make users to admin + * @param userUid User UIDs + * @returns an Either of boolean or error + */ + async makeUsersAdmin(userUIDs: string[]) { + const isUpdated = await this.userService.makeAdmins(userUIDs); + if (E.isLeft(isUpdated)) return E.left(isUpdated.left); + return E.right(true); + } + /** * Remove user as admin * @param userUid User UID diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index 68437a5d4..73d1fb996 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -373,6 +373,23 @@ export class UserService { } } + /** + * Change users to admins by toggling isAdmin param to true + * @param userUID user UIDs + * @returns a Either of true or error + */ + async makeAdmins(userUIDs: string[]) { + try { + await this.prisma.user.updateMany({ + where: { uid: { in: userUIDs } }, + data: { isAdmin: true }, + }); + return E.right(true); + } catch (error) { + return E.left(USER_NOT_FOUND); + } + } + /** * Fetch all the admin users * @returns an array of admin users