fix create membership

This commit is contained in:
2021-12-09 01:52:44 +07:00
parent d65af44a52
commit d030624765
10 changed files with 78 additions and 44 deletions

View File

@@ -10,9 +10,12 @@ export class CreateUserDto {
@IsUUID()
roleId: string;
@ValidateIf((o) => {
return !!o.superior;
})
@IsUUID()
superior: string;
@IsNotEmpty()
superior: boolean;
// @ValidateIf((o) => {
// return !!o.superior;
// })
// @IsUUID()
// superior: string;
}

View File

@@ -9,6 +9,7 @@ import {
ParseUUIDPipe,
HttpStatus,
Query,
Request
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
@@ -23,11 +24,12 @@ export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Post()
async create(@Body() createUserDto: CreateUserDto) {
async create(
@Request() req,
@Body() createUserDto: CreateUserDto
) {
return {
data: await this.usersService.create(createUserDto),
data: await this.usersService.create(createUserDto,req.user),
statusCode: HttpStatus.CREATED,
message: 'success',
};

View File

@@ -21,9 +21,9 @@ export class UsersService {
private roleService: RoleService
) {}
async create(createUserDto: CreateUserDto) {
async create(createUserDto: CreateUserDto, currentUser: any) {
const roles = await this.roleService.findOne(createUserDto.roleId);
const superior = await this.findExist(createUserDto.superior);
const superior = await this.findByUsername(currentUser.username);
const salt = randomStringGenerator();
const result = await this.usersRepository.insert({
@@ -39,19 +39,24 @@ export class UsersService {
dataCoaWallet.balanceType = balanceType.CREDIT;
dataCoaWallet.type = coaType.WALLET;
let dataCoaAR = new InputCoaDto();
dataCoaAR.userId = result.identifiers[0].id;
dataCoaAR.balanceType = balanceType.CREDIT;
dataCoaAR.type = coaType.ACCOUNT_RECEIVABLE;
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 dataCoaPayable = new InputCoaDto();
dataCoaPayable.userId = result.identifiers[0].id;
dataCoaPayable.balanceType = balanceType.CREDIT;
dataCoaPayable.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);
await this.coaService.create(dataCoaAR);
await this.coaService.create(dataCoaPayable);
return this.usersRepository.findOneOrFail(result.identifiers[0].id);
}
@@ -97,6 +102,26 @@ export class UsersService {
}
}
async findByUsername(username: string) {
try {
return await this.usersRepository.findOneOrFail({
username:username
});
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Data not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
async findOne(id: string) {
const coa = await this.coaService.findByUser(id,coaType.WALLET);
try {