feat: removeUsersByAdmin mutation added

This commit is contained in:
mirarifhasan
2024-01-29 21:20:56 +06:00
committed by Andrew Bastin
parent 2bde3f8b02
commit b53cbb093c
4 changed files with 96 additions and 6 deletions

View File

@@ -27,9 +27,7 @@ import {
} from './input-types.args'; } from './input-types.args';
import { GqlThrottlerGuard } from 'src/guards/gql-throttler.guard'; import { GqlThrottlerGuard } from 'src/guards/gql-throttler.guard';
import { SkipThrottle } from '@nestjs/throttler'; import { SkipThrottle } from '@nestjs/throttler';
import { User } from 'src/user/user.model'; import { UserDeleteData } from 'src/user/user.model';
import { PaginationArgs } from 'src/types/input-types.args';
import { TeamInvitation } from 'src/team-invitation/team-invitation.model';
@UseGuards(GqlThrottlerGuard) @UseGuards(GqlThrottlerGuard)
@Resolver(() => Admin) @Resolver(() => Admin)
@@ -105,9 +103,28 @@ export class AdminResolver {
}) })
userUID: string, userUID: string,
): Promise<boolean> { ): Promise<boolean> {
const invitedUser = await this.adminService.removeUserAccount(userUID); const removedUser = await this.adminService.removeUserAccount(userUID);
if (E.isLeft(invitedUser)) throwErr(invitedUser.left); if (E.isLeft(removedUser)) throwErr(removedUser.left);
return invitedUser.right; return removedUser.right;
}
@Mutation(() => [UserDeleteData], {
description: 'Delete user accounts from infra',
})
@UseGuards(GqlAuthGuard, GqlAdminGuard)
async removeUsersByAdmin(
@Args({
name: 'userUIDs',
description: 'users UID',
type: () => [ID],
})
userUIDs: string[],
): Promise<UserDeleteData[]> {
const deletionResults = await this.adminService.removeUserAccounts(
userUIDs,
);
if (E.isLeft(deletionResults)) throwErr(deletionResults.left);
return deletionResults.right;
} }
@Mutation(() => Boolean, { @Mutation(() => Boolean, {

View File

@@ -28,6 +28,7 @@ import { TeamMemberRole } from '../team/team.model';
import { ShortcodeService } from 'src/shortcode/shortcode.service'; import { ShortcodeService } from 'src/shortcode/shortcode.service';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { OffsetPaginationArgs } from 'src/types/input-types.args'; import { OffsetPaginationArgs } from 'src/types/input-types.args';
import { UserDeleteData } from 'src/user/user.model';
@Injectable() @Injectable()
export class AdminService { export class AdminService {
@@ -418,6 +419,44 @@ export class AdminService {
return E.right(delUser.right); return E.right(delUser.right);
} }
/**
* Remove user 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 deletionPromises = users.map((user) => {
return this.userService
.deleteUserByUID(user)()
.then((res) => {
if (E.isLeft(res)) {
return {
userUID: user.uid,
success: false,
errorMessage: res.left,
} as UserDeleteData;
}
return {
userUID: user.uid,
success: true,
errorMessage: null,
} as UserDeleteData;
});
});
const promiseResult = await Promise.allSettled(deletionPromises);
const userDeleteResult = promiseResult.map((result) => {
if (result.status === 'fulfilled') {
return result.value;
}
});
return E.right(userDeleteResult);
}
/** /**
* Make a user an admin * Make a user an admin
* @param userUid User UID * @param userUid User UID

View File

@@ -56,3 +56,22 @@ export enum SessionType {
registerEnumType(SessionType, { registerEnumType(SessionType, {
name: 'SessionType', name: 'SessionType',
}); });
@ObjectType()
export class UserDeleteData {
@Field(() => ID, {
description: 'UID of the user',
})
userUID: string;
@Field(() => Boolean, {
description: 'Flag to determine if user deletion was successful or not',
})
success: Boolean;
@Field({
nullable: true,
description: 'Error message if user deletion was not successful',
})
errorMessage: String;
}

View File

@@ -89,6 +89,21 @@ export class UserService {
} }
} }
/**
* Find Non-Admin Users with given IDs
* @param userUIDs User IDs
* @returns Option of found Users
*/
async findNonAdminUsersByIds(userUIDs: string[]): Promise<AuthUser[]> {
const users = await this.prisma.user.findMany({
where: {
uid: { in: userUIDs },
isAdmin: false,
},
});
return users;
}
/** /**
* Update User with new generated hashed refresh token * Update User with new generated hashed refresh token
* *