diff --git a/src/helper/enum-list.ts b/src/helper/enum-list.ts index e8ffaaf..502dc83 100644 --- a/src/helper/enum-list.ts +++ b/src/helper/enum-list.ts @@ -44,3 +44,9 @@ export enum accountType { PARTNER, CUSTOMER, } + +export enum statusApproval { + ADMIN, + SUPERVISOR, + RETAIL, +} diff --git a/src/users/entities/user.entity.ts b/src/users/entities/user.entity.ts index 9829a54..b16c9c4 100644 --- a/src/users/entities/user.entity.ts +++ b/src/users/entities/user.entity.ts @@ -4,6 +4,7 @@ import { BaseModel } from '../../config/basemodel.entity'; import { Partner } from './partner.entity'; import { UserDetail } from './user_detail.entity'; import { COA } from '../../transaction/entities/coa.entity'; +import { statusApproval, typeTransaction } from '../../helper/enum-list'; @Entity() export class User extends BaseModel { @@ -22,6 +23,14 @@ export class User extends BaseModel { @Column({ default: true }) isActive: boolean; + @Column({ + nullable: true, + }) + status_approval: string; + + @Column({ default: false }) + isRejected: boolean; + @ManyToOne( () => { return User; diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 2b80891..ef9997a 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -187,7 +187,26 @@ export class UsersController { @Query('page') page: number, @Query('pageSize') pageSize: number, ) { - const data = await this.usersService.findBySuperrior(req.user.userId, page, pageSize); + const data = await this.usersService.findBySuperrior( + req.user.userId, + page, + pageSize, + ); + + return { + ...data, + statusCode: HttpStatus.OK, + message: 'success', + }; + } + + @Get('find-by-approval') + async findByApproval( + @Request() req, + @Query('page') page: number, + @Query('pageSize') pageSize: number, + ) { + const data = await this.usersService.findApproval(req.user, page, pageSize); return { ...data, @@ -250,6 +269,33 @@ export class UsersController { }; } + @Put('approve-user/:id') + async approveUser(@Param('id', ParseUUIDPipe) id: string, @Request() req) { + return { + data: await this.usersService.confirmationUser(id, req.user, 'approved'), + statusCode: HttpStatus.OK, + message: 'success', + }; + } + + @Put('reject-user/:id') + async rejectUser(@Param('id', ParseUUIDPipe) id: string, @Request() req) { + return { + data: await this.usersService.confirmationUser(id, req.user, 'rejected'), + statusCode: HttpStatus.OK, + message: 'success', + }; + } + + @Put('resend-user/:id') + async resendUser(@Param('id', ParseUUIDPipe) id: string, @Request() req) { + return { + data: await this.usersService.confirmationUser(id, req.user, 'resend'), + statusCode: HttpStatus.OK, + message: 'success', + }; + } + @Put('change-password/:id') async updatePassword( @Param('id', ParseUUIDPipe) id: string, diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 3de08e8..22c40f6 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -67,6 +67,7 @@ export class UsersService { if (createUserDto.superior) { userData.superior = superior; + userData.status_approval = superior.roles.name; } else { userData.superior = null; userData.partner = createUserDto.partner; @@ -130,7 +131,13 @@ export class UsersService { return userData; } - async findAll(page: number, pageSize: number, id: string, superior: string, type: string) { + async findAll( + page: number, + pageSize: number, + id: string, + superior: string, + type: string, + ) { const baseQuery = this.usersRepository .createQueryBuilder('user') .where('user.id != :id', { @@ -289,6 +296,55 @@ export class UsersService { }; } + async findApproval(superrior, page: number, pageSize: number) { + const dataUser = await this.findByUsername(superrior.username); + + const baseQuery = this.usersRepository + .createQueryBuilder('user') + .where( + 'user.id != :id and user.superior_id = :superior and status_approval = :status and isRejected is false', + { + id: superrior.userId, + superior: superrior.userId, + status: dataUser.roles.name, + }, + ) + .leftJoinAndSelect('user.roles', 'roles', `roles.id = user.roles_id`) + .leftJoinAndMapOne( + 'user.user_detail', + UserDetail, + 'user_detail', + `user_detail.user = user.id`, + ) + .leftJoinAndMapOne( + 'user.coa', + COA, + 'coa', + `coa.user = user.id and coa.type = '0'`, + ) + .select([ + 'user.id', + 'user.username', + 'user.isActive', + 'roles.id', + 'roles.name', + 'user_detail', + 'coa.amount', + ]); + + const data = await baseQuery + .skip(page * pageSize) + .take(pageSize) + .getMany(); + + const totalData = await baseQuery.getCount(); + + return { + data, + count: totalData, + }; + } + async findExist(id: string) { try { return await this.usersRepository.findOneOrFail({ @@ -503,6 +559,37 @@ export class UsersService { } } + async confirmationUser(id: string, user: string, status: string) { + try { + const dataUser = await this.usersRepository.findOneOrFail(id); + const supervisorUser = await this.usersRepository.findOneOrFail(id); + + if (status == 'approved') { + dataUser.status_approval = supervisorUser.roles.name; + } else if (status == 'resend') { + dataUser.isRejected = false; + } else { + dataUser.isRejected = true; + } + + const result = await this.usersRepository.save(dataUser); + + return dataUser; + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'User not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } + setStatus = async (id: string, type: string) => { const userData = new User();