diff --git a/packages/hoppscotch-backend/src/admin/admin.service.ts b/packages/hoppscotch-backend/src/admin/admin.service.ts index 5c6b191b1..a6512e9e9 100644 --- a/packages/hoppscotch-backend/src/admin/admin.service.ts +++ b/packages/hoppscotch-backend/src/admin/admin.service.ts @@ -6,6 +6,7 @@ import * as E from 'fp-ts/Either'; import * as O from 'fp-ts/Option'; import { validateEmail } from '../utils'; import { + ADMIN_CAN_NOT_BE_DELETED, DUPLICATE_EMAIL, EMAIL_FAILED, INVALID_EMAIL, @@ -432,15 +433,30 @@ export class AdminService { } /** - * Remove user accounts by UIDs + * Remove user (not Admin) accounts by UIDs * @param userUid User UIDs * @returns an Either of boolean or error */ async removeUserAccounts(userUIDs: string[]) { - const users = await this.userService.findNonAdminUsersByIds(userUIDs); - if (users.length === 0) return E.left(USER_NOT_FOUND); + const allUsers = await this.userService.findUsersByIds(userUIDs); + if (allUsers.length === 0) return E.left(USER_NOT_FOUND); - const deletionPromises = users.map((user) => { + const userDeleteResult: UserDeleteData[] = []; + + // Admin user can not be deleted without removing admin status/role + allUsers.forEach((user) => { + if (user.isAdmin) { + userDeleteResult.push({ + userUID: user.uid, + success: false, + errorMessage: ADMIN_CAN_NOT_BE_DELETED, + }); + } + }); + + const normalUsers = allUsers.filter((user) => !user.isAdmin); + + const deletionPromises = normalUsers.map((user) => { return this.userService .deleteUserByUID(user)() .then((res) => { @@ -460,9 +476,9 @@ export class AdminService { }); const promiseResult = await Promise.allSettled(deletionPromises); - const userDeleteResult = promiseResult.map((result) => { + promiseResult.forEach((result) => { if (result.status === 'fulfilled') { - return result.value; + userDeleteResult.push(result.value); } }); diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 174a782c8..28a99b07c 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -10,6 +10,14 @@ export const DUPLICATE_EMAIL = 'email/both_emails_cannot_be_same' as const; export const ONLY_ONE_ADMIN_ACCOUNT = 'admin/only_one_admin_account_found' as const; +/** + * Admin user can not be deleted + * To delete the admin user, first make the Admin user a normal user + * (AdminService) + */ +export const ADMIN_CAN_NOT_BE_DELETED = + 'admin/admin_can_not_be_deleted' as const; + /** * Token Authorization failed (Check 'Authorization' Header) * (GqlAuthGuard) diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index 37d4edef8..647e4f5c6 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -94,11 +94,10 @@ export class UserService { * @param userUIDs User IDs * @returns Option of found Users */ - async findNonAdminUsersByIds(userUIDs: string[]): Promise { + async findUsersByIds(userUIDs: string[]): Promise { const users = await this.prisma.user.findMany({ where: { uid: { in: userUIDs }, - isAdmin: false, }, }); return users;