fix user input
This commit is contained in:
parent
d030624765
commit
84fda27e2d
|
@ -15,23 +15,19 @@ export class CoaService {
|
|||
) {}
|
||||
|
||||
async create(inputCoaDto: InputCoaDto) {
|
||||
const user = await this.userService.findExist(inputCoaDto.userId)
|
||||
console.log(coaType[inputCoaDto.type])
|
||||
console.log(inputCoaDto.type)
|
||||
const result = await this.coaRepository.insert({
|
||||
user:user.id,
|
||||
name: coaType[inputCoaDto.type] + '-' + user.username,
|
||||
balanceType:inputCoaDto.balanceType,
|
||||
type:inputCoaDto.type,
|
||||
amount:0
|
||||
});
|
||||
const user = inputCoaDto.user
|
||||
let coaData = new COA();
|
||||
coaData.user = user.id;
|
||||
coaData.name = coaType[inputCoaDto.type] + '-' + user.username;
|
||||
coaData.balanceType = inputCoaDto.balanceType;
|
||||
coaData.type = inputCoaDto.type;
|
||||
coaData.amount = 0;
|
||||
|
||||
const result = await inputCoaDto.coaEntityManager.insert(COA,coaData);
|
||||
|
||||
const coaData = await this.coaRepository.findOneOrFail(
|
||||
result.identifiers[0].id,
|
||||
);
|
||||
if(inputCoaDto.type == coaType.ACCOUNT_RECEIVABLE || inputCoaDto.type == coaType.ACCOUNT_PAYABLE){
|
||||
coaData.relatedUser = inputCoaDto.relatedUserId;
|
||||
await this.coaRepository.save(coaData)
|
||||
await inputCoaDto.coaEntityManager.save(coaData)
|
||||
}
|
||||
|
||||
return coaData;
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
import { balanceType, coaType } from 'src/helper/enum-list';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { EntityManager } from 'typeorm';
|
||||
|
||||
export class InputCoaDto {
|
||||
@IsUUID()
|
||||
userId: string;
|
||||
user: User;
|
||||
|
||||
@IsNotEmpty()
|
||||
type: coaType;
|
||||
|
@ -13,4 +15,7 @@ export class InputCoaDto {
|
|||
|
||||
@IsUUID()
|
||||
relatedUserId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
coaEntityManager: EntityManager;
|
||||
}
|
||||
|
|
|
@ -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 dataCoaWallet = new InputCoaDto();
|
||||
dataCoaWallet.userId = result.identifiers[0].id;
|
||||
dataCoaWallet.balanceType = balanceType.CREDIT;
|
||||
dataCoaWallet.type = coaType.WALLET;
|
||||
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
|
||||
|
||||
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;
|
||||
await this.connection.transaction(async (manager) => {
|
||||
const result = await manager.insert(User,userData);
|
||||
|
||||
let dataCoaAR = new InputCoaDto();
|
||||
dataCoaAR.userId = result.identifiers[0].id;
|
||||
dataCoaAR.balanceType = balanceType.DEBIT;
|
||||
dataCoaAR.relatedUserId = superior.id;
|
||||
dataCoaAR.type = coaType.ACCOUNT_RECEIVABLE;
|
||||
let dataCoaWallet = new InputCoaDto();
|
||||
dataCoaWallet.user = userData;
|
||||
dataCoaWallet.balanceType = balanceType.CREDIT;
|
||||
dataCoaWallet.type = coaType.WALLET;
|
||||
dataCoaWallet.coaEntityManager = manager;
|
||||
|
||||
await this.coaService.create(dataCoaAP);
|
||||
await this.coaService.create(dataCoaAR);
|
||||
}
|
||||
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;
|
||||
|
||||
await this.coaService.create(dataCoaWallet);
|
||||
let dataCoaAR = new InputCoaDto();
|
||||
dataCoaAR.user = userData;
|
||||
dataCoaAR.balanceType = balanceType.DEBIT;
|
||||
dataCoaAR.relatedUserId = superior.id;
|
||||
dataCoaAR.type = coaType.ACCOUNT_RECEIVABLE;
|
||||
dataCoaAR.coaEntityManager = manager;
|
||||
|
||||
return this.usersRepository.findOneOrFail(result.identifiers[0].id);
|
||||
await this.coaService.create(dataCoaAP);
|
||||
await this.coaService.create(dataCoaAR);
|
||||
}
|
||||
|
||||
await this.coaService.create(dataCoaWallet);
|
||||
|
||||
})
|
||||
|
||||
return userData;
|
||||
}
|
||||
|
||||
findAll(page:number) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user