From c3de4f56e17a382bb5ec7cc0edc02323a54f72aa Mon Sep 17 00:00:00 2001 From: ilham Date: Thu, 16 Dec 2021 17:28:56 +0700 Subject: [PATCH] fix: add user deetail in get all user --- src/users/users.controller.ts | 8 ++----- src/users/users.service.ts | 43 ++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 4b377ee..c7d614b 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -149,14 +149,10 @@ export class UsersController { @Get('find-by-supperior') async findBySuperrior(@Request() req, @Query('page') page: number) { - const [data, count] = await this.usersService.findBySuperrior( - req.user.userId, - page, - ); + const data = await this.usersService.findBySuperrior(req.user.userId, page); return { - data, - count, + ...data, statusCode: HttpStatus.OK, message: 'success', }; diff --git a/src/users/users.service.ts b/src/users/users.service.ts index a4c1aa2..d6f36a1 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -160,18 +160,39 @@ export class UsersService { }); } - findBySuperrior(superriorId: string, page: number) { - return this.usersRepository.findAndCount({ - skip: page * 10, - take: 10, - relations: ['roles'], - where: { + async findBySuperrior(superriorId: string, page: number) { + const baseQuery = this.usersRepository + .createQueryBuilder('user') + .where('user.id != :id and user.superior_id = :superior', { + id: superriorId, superior: superriorId, - }, - order: { - updatedAt: 'DESC', - }, - }); + }) + .leftJoinAndSelect('user.roles', 'roles', `roles.id = user.roles_id`) + .leftJoinAndMapOne( + 'user.user_detail', + UserDetail, + 'user_detail', + `user_detail.user = user.id`, + ) + .select([ + 'user.id', + 'user.username', + 'user.isActive', + 'roles.name', + 'user_detail', + ]); + + const data = await baseQuery + .skip(page * 10) + .take(10) + .getMany(); + + const totalData = await baseQuery.getCount(); + + return { + data, + count: totalData, + }; } async findExist(id: string) {