From 412874a057aa4947e128f7c03cda0ec7b69b5c67 Mon Sep 17 00:00:00 2001 From: ilham Date: Fri, 31 Dec 2021 12:00:44 +0700 Subject: [PATCH] fix: profile --- src/users/users.service.ts | 74 +++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/src/users/users.service.ts b/src/users/users.service.ts index ba2e965..e84a914 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -1,4 +1,10 @@ -import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; +import { + forwardRef, + HttpException, + HttpStatus, + Inject, + Injectable, +} from '@nestjs/common'; import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; import { Connection, EntityNotFoundError, Not, Repository } from 'typeorm'; @@ -58,24 +64,29 @@ export class UsersService { userData.username = createUserDto.username; userData.password = await hashPassword(createUserDto.password, salt); userData.salt = salt; + if (createUserDto.superior) { userData.superior = superior; } else { userData.superior = null; userData.partner = createUserDto.partner; } + userData.roles = roles; await this.connection.transaction(async (manager) => { const result = await manager.insert(User, userData); const userDetailData = new UserDetail(); + userDetailData.name = createUserDto.name; userDetailData.phone_number = createUserDto.phone_number; userDetailData.user = userData; + const user_detail = await manager.insert(UserDetail, userDetailData); const dataCoaWallet = new InputCoaDto(); + dataCoaWallet.user = userData; dataCoaWallet.balanceType = balanceType.CREDIT; dataCoaWallet.type = coaType.WALLET; @@ -83,6 +94,7 @@ export class UsersService { await this.coaService.create(dataCoaWallet); const dataCoaAR = new InputCoaDto(); + dataCoaAR.user = userData; dataCoaAR.balanceType = balanceType.DEBIT; dataCoaAR.relatedUserId = superior.id; @@ -92,6 +104,7 @@ export class UsersService { if (roles.name == 'Supervisor' || roles.name == 'Sales') { const dataCOAProfit = new InputCoaDto(); + dataCOAProfit.user = userData; dataCOAProfit.balanceType = balanceType.CREDIT; dataCOAProfit.type = coaType.PROFIT; @@ -101,6 +114,7 @@ export class UsersService { if (createUserDto.superior) { const dataCoaAP = new InputCoaDto(); + dataCoaAP.user = userData; dataCoaAP.balanceType = balanceType.CREDIT; dataCoaAP.relatedUserId = superior.id; @@ -180,28 +194,37 @@ export class UsersService { superior: id, }, }); - const listId = baseQuery.map((x) => x.id); + const listId = baseQuery.map((x) => { + return x.id; + }); + listUser = listUser.concat(listId); listToFind = listId; + if (role == 1) { return listUser; - } else { - for (let it = 2; it <= role; it++) { - let newListToFind = []; - await mapSeries(listToFind, async (ltf) => { - const getListUser = await this.usersRepository.find({ - where: { - superior: ltf, - }, - }); - if (getListUser.length > 0) { - const listId = getListUser.map((x) => x.id); - newListToFind = newListToFind.concat(listId); - listUser = listUser.concat(listId); - } + } + + for (let it = 2; it <= role; it++) { + let newListToFind = []; + + await mapSeries(listToFind, async (ltf) => { + const getListUser = await this.usersRepository.find({ + where: { + superior: ltf, + }, }); - listToFind = newListToFind; - } + + if (getListUser.length > 0) { + const listId = getListUser.map((x) => { + return x.id; + }); + + newListToFind = newListToFind.concat(listId); + listUser = listUser.concat(listId); + } + }); + listToFind = newListToFind; } return listUser; @@ -335,10 +358,13 @@ export class UsersService { .getOne(); const coa = await this.coaService.findByUser(id, coaType.WALLET); let coaProfit; - if(userData.roles.id != 'e4dfb6a3-2338-464a-8fb8-5cbc089d4209'){ - coaProfit = await this.coaService.findByUser(id, coaType.PROFIT); - }; + if ( + userData.roles.id != 'e4dfb6a3-2338-464a-8fb8-5cbc089d4209' && + userData.roles.id != '21dceea2-416e-4b55-b74c-12605e1f8d1b' + ) { + coaProfit = await this.coaService.findByUser(id, coaType.PROFIT); + } return { ...userData, @@ -416,11 +442,14 @@ export class UsersService { async updatePassword(id: string, updateUserDto: UpdateUserDto) { try { const dataUser = await this.usersRepository.findOneOrFail(id); + dataUser.password = await hashPassword( updateUserDto.password, dataUser.salt, ); + const result = await this.usersRepository.save(dataUser); + return dataUser; } catch (e) { if (e instanceof EntityNotFoundError) { @@ -444,11 +473,14 @@ export class UsersService { partner: id, }, }); + dataUser.password = await hashPassword( updateUserDto.password, dataUser.salt, ); + const result = await this.usersRepository.save(dataUser); + return dataUser; } catch (e) { if (e instanceof EntityNotFoundError) {