feat: added error obj for admin status users on deletion
This commit is contained in:
committed by
Andrew Bastin
parent
3c7a2401ae
commit
a0d40c8776
@@ -6,6 +6,7 @@ import * as E from 'fp-ts/Either';
|
|||||||
import * as O from 'fp-ts/Option';
|
import * as O from 'fp-ts/Option';
|
||||||
import { validateEmail } from '../utils';
|
import { validateEmail } from '../utils';
|
||||||
import {
|
import {
|
||||||
|
ADMIN_CAN_NOT_BE_DELETED,
|
||||||
DUPLICATE_EMAIL,
|
DUPLICATE_EMAIL,
|
||||||
EMAIL_FAILED,
|
EMAIL_FAILED,
|
||||||
INVALID_EMAIL,
|
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
|
* @param userUid User UIDs
|
||||||
* @returns an Either of boolean or error
|
* @returns an Either of boolean or error
|
||||||
*/
|
*/
|
||||||
async removeUserAccounts(userUIDs: string[]) {
|
async removeUserAccounts(userUIDs: string[]) {
|
||||||
const users = await this.userService.findNonAdminUsersByIds(userUIDs);
|
const allUsers = await this.userService.findUsersByIds(userUIDs);
|
||||||
if (users.length === 0) return E.left(USER_NOT_FOUND);
|
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
|
return this.userService
|
||||||
.deleteUserByUID(user)()
|
.deleteUserByUID(user)()
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
@@ -460,9 +476,9 @@ export class AdminService {
|
|||||||
});
|
});
|
||||||
const promiseResult = await Promise.allSettled(deletionPromises);
|
const promiseResult = await Promise.allSettled(deletionPromises);
|
||||||
|
|
||||||
const userDeleteResult = promiseResult.map((result) => {
|
promiseResult.forEach((result) => {
|
||||||
if (result.status === 'fulfilled') {
|
if (result.status === 'fulfilled') {
|
||||||
return result.value;
|
userDeleteResult.push(result.value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,14 @@ export const DUPLICATE_EMAIL = 'email/both_emails_cannot_be_same' as const;
|
|||||||
export const ONLY_ONE_ADMIN_ACCOUNT =
|
export const ONLY_ONE_ADMIN_ACCOUNT =
|
||||||
'admin/only_one_admin_account_found' as const;
|
'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)
|
* Token Authorization failed (Check 'Authorization' Header)
|
||||||
* (GqlAuthGuard)
|
* (GqlAuthGuard)
|
||||||
|
|||||||
@@ -94,11 +94,10 @@ export class UserService {
|
|||||||
* @param userUIDs User IDs
|
* @param userUIDs User IDs
|
||||||
* @returns Option of found Users
|
* @returns Option of found Users
|
||||||
*/
|
*/
|
||||||
async findNonAdminUsersByIds(userUIDs: string[]): Promise<AuthUser[]> {
|
async findUsersByIds(userUIDs: string[]): Promise<AuthUser[]> {
|
||||||
const users = await this.prisma.user.findMany({
|
const users = await this.prisma.user.findMany({
|
||||||
where: {
|
where: {
|
||||||
uid: { in: userUIDs },
|
uid: { in: userUIDs },
|
||||||
isAdmin: false,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return users;
|
return users;
|
||||||
|
|||||||
Reference in New Issue
Block a user