diff --git a/src/users/entities/user.entity.ts b/src/users/entities/user.entity.ts index 4655b14..4f6427f 100644 --- a/src/users/entities/user.entity.ts +++ b/src/users/entities/user.entity.ts @@ -1,15 +1,15 @@ import { Roles } from 'src/configurable/entities/roles.entity'; import { - Entity, Column, - PrimaryGeneratedColumn, + Entity, ManyToOne, OneToOne, + PrimaryGeneratedColumn, } from 'typeorm'; import { BaseModel } from '../../config/basemodel.entity'; -import { hashPassword } from '../../helper/hash_password'; import { Partner } from './partner.entity'; import { UserDetail } from './user_detail.entity'; +import { COA } from '../../transaction/entities/coa.entity'; @Entity() export class User extends BaseModel { @@ -57,4 +57,9 @@ export class User extends BaseModel { }, ) partner: Partner; + + @OneToOne(() => UserDetail, (userDetail) => userDetail.user) + userDetail: UserDetail; + + coa: COA; } diff --git a/src/users/entities/user_detail.entity.ts b/src/users/entities/user_detail.entity.ts index 2628685..cb67b8c 100644 --- a/src/users/entities/user_detail.entity.ts +++ b/src/users/entities/user_detail.entity.ts @@ -1,12 +1,10 @@ import { - Entity, Column, - PrimaryGeneratedColumn, - OneToOne, - ManyToOne, + Entity, JoinColumn, + OneToOne, + PrimaryGeneratedColumn, } from 'typeorm'; -import { accountType } from '../../helper/enum-list'; import { User } from './user.entity'; @Entity() @@ -20,7 +18,7 @@ export class UserDetail { @Column() phone_number: string; - @OneToOne(() => User) + @OneToOne(() => User, (user) => user.userDetail) @JoinColumn() user: User; } diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 6151726..4b377ee 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -114,14 +114,10 @@ export class UsersController { @Get() async findAll(@Request() req, @Query('page') page: number) { - const [data, count] = await this.usersService.findAll( - page, - req.user.userId, - ); + const data = await this.usersService.findAll(page, req.user.userId); return { - data, - count, + ...data, statusCode: HttpStatus.OK, message: 'success', }; diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 21132cc..d26d7ef 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -24,6 +24,7 @@ import { RoleService } from 'src/configurable/roles.service'; import { InputCoaDto } from 'src/transaction/dto/input-coa.dto'; import * as uuid from 'uuid'; import { UserDetail } from './entities/user_detail.entity'; +import { COA } from '../transaction/entities/coa.entity'; @Injectable() export class UsersService { @@ -112,17 +113,38 @@ export class UsersService { return userData; } - findAll(page: number, id: string) { - return this.usersRepository.findAndCount({ - skip: page * 10, - take: 10, - order: { - version: 'DESC', - }, - where: { - id: Not(Equal(id)), - }, - }); + async findAll(page: number, id: string) { + const baseQuery = this.usersRepository + .createQueryBuilder('user') + .where('user.id != :id', { + id: id, + }) + .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, + }; } findByRoles(relationId: string, page: number) { @@ -201,7 +223,7 @@ export class UsersService { where: { id: id, }, - relations: ['roles', 'superior'], + relations: ['roles', 'superior', 'userDetail'], }); return {