fix user input
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||
import { Connection, EntityNotFoundError, Repository } from 'typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
|
||||
@@ -10,6 +10,7 @@ import { CoaService } from 'src/transaction/coa.service';
|
||||
import { balanceType, coaType } from 'src/helper/enum-list';
|
||||
import { RoleService } from 'src/configurable/roles.service';
|
||||
import { InputCoaDto } from 'src/transaction/dto/input-coa.dto';
|
||||
import * as uuid from "uuid";
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
@@ -18,47 +19,56 @@ export class UsersService {
|
||||
private usersRepository: Repository<User>,
|
||||
@Inject(forwardRef(() => CoaService))
|
||||
private coaService: CoaService,
|
||||
private roleService: RoleService
|
||||
private roleService: RoleService,
|
||||
private connection: Connection,
|
||||
) {}
|
||||
|
||||
async create(createUserDto: CreateUserDto, currentUser: any) {
|
||||
const roles = await this.roleService.findOne(createUserDto.roleId);
|
||||
const superior = await this.findByUsername(currentUser.username);
|
||||
|
||||
const salt = randomStringGenerator();
|
||||
const result = await this.usersRepository.insert({
|
||||
username: createUserDto.username,
|
||||
password: await hashPassword(createUserDto.password, salt),
|
||||
salt,
|
||||
superior:superior,
|
||||
roles:roles
|
||||
});
|
||||
|
||||
let userData = new User();
|
||||
userData.id = uuid.v4();
|
||||
userData.username = createUserDto.username,
|
||||
userData.password = await hashPassword(createUserDto.password, salt),
|
||||
userData.salt = salt,
|
||||
userData.superior = superior,
|
||||
userData.roles = roles
|
||||
|
||||
let dataCoaWallet = new InputCoaDto();
|
||||
dataCoaWallet.userId = result.identifiers[0].id;
|
||||
dataCoaWallet.balanceType = balanceType.CREDIT;
|
||||
dataCoaWallet.type = coaType.WALLET;
|
||||
await this.connection.transaction(async (manager) => {
|
||||
const result = await manager.insert(User,userData);
|
||||
|
||||
let dataCoaWallet = new InputCoaDto();
|
||||
dataCoaWallet.user = userData;
|
||||
dataCoaWallet.balanceType = balanceType.CREDIT;
|
||||
dataCoaWallet.type = coaType.WALLET;
|
||||
dataCoaWallet.coaEntityManager = manager;
|
||||
|
||||
if(createUserDto.superior){
|
||||
let dataCoaAP = new InputCoaDto();
|
||||
dataCoaAP.user = userData;
|
||||
dataCoaAP.balanceType = balanceType.CREDIT;
|
||||
dataCoaAP.relatedUserId = superior.id;
|
||||
dataCoaAP.type = coaType.ACCOUNT_PAYABLE;
|
||||
dataCoaAP.coaEntityManager = manager;
|
||||
|
||||
let dataCoaAR = new InputCoaDto();
|
||||
dataCoaAR.user = userData;
|
||||
dataCoaAR.balanceType = balanceType.DEBIT;
|
||||
dataCoaAR.relatedUserId = superior.id;
|
||||
dataCoaAR.type = coaType.ACCOUNT_RECEIVABLE;
|
||||
dataCoaAR.coaEntityManager = manager;
|
||||
|
||||
await this.coaService.create(dataCoaAP);
|
||||
await this.coaService.create(dataCoaAR);
|
||||
}
|
||||
|
||||
await this.coaService.create(dataCoaWallet);
|
||||
|
||||
if(createUserDto.superior){
|
||||
let dataCoaAP = new InputCoaDto();
|
||||
dataCoaAP.userId = result.identifiers[0].id;
|
||||
dataCoaAP.balanceType = balanceType.CREDIT;
|
||||
dataCoaAP.relatedUserId = superior.id;
|
||||
dataCoaAP.type = coaType.ACCOUNT_PAYABLE;
|
||||
})
|
||||
|
||||
let dataCoaAR = new InputCoaDto();
|
||||
dataCoaAR.userId = result.identifiers[0].id;
|
||||
dataCoaAR.balanceType = balanceType.DEBIT;
|
||||
dataCoaAR.relatedUserId = superior.id;
|
||||
dataCoaAR.type = coaType.ACCOUNT_RECEIVABLE;
|
||||
|
||||
await this.coaService.create(dataCoaAP);
|
||||
await this.coaService.create(dataCoaAR);
|
||||
}
|
||||
|
||||
await this.coaService.create(dataCoaWallet);
|
||||
|
||||
return this.usersRepository.findOneOrFail(result.identifiers[0].id);
|
||||
return userData;
|
||||
}
|
||||
|
||||
findAll(page:number) {
|
||||
|
||||
Reference in New Issue
Block a user