fix create membership

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

View File

@ -7,6 +7,8 @@ export enum statusTransaction {
export enum typeTransaction {
DISTRIBUTION,
ORDER,
DEPOSIT_IRS,
}
export enum productType {

View File

@ -25,7 +25,10 @@ export class ProductHistoryPrice extends BaseModel {
@Column({ type: 'date' })
startDate: Date;
@Column({ type: 'date' })
@Column({
type: 'date',
nullable:true
})
endDate: Date;
@Column('text')

View File

@ -2,7 +2,7 @@ import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nest
import { EntityNotFoundError, Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { COA } from './entities/coa.entity';
import { coaType } from '../helper/enum-list';
import { balanceType, coaType } from '../helper/enum-list';
import { InputCoaDto } from './dto/input-coa.dto';
import { UsersService } from 'src/users/users.service';
@ -16,17 +16,25 @@ 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: inputCoaDto.balanceType + '-' + user.username,
name: coaType[inputCoaDto.type] + '-' + user.username,
balanceType:inputCoaDto.balanceType,
type:inputCoaDto.type
type:inputCoaDto.type,
amount:0
});
return this.coaRepository.findOneOrFail(
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)
}
return coaData;
}
async findByUser(id: string, typeOfCoa: coaType) {

View File

@ -10,4 +10,7 @@ export class InputCoaDto {
@IsNotEmpty()
balanceType: balanceType;
@IsUUID()
relatedUserId: string;
}

View File

@ -1,15 +0,0 @@
import {
Entity,
Column,
} from 'typeorm';
import { BaseModel } from '../../config/basemodel.entity';
import { balanceType } from '../../helper/enum-list';
@Entity()
export class CoaType extends BaseModel {
@Column()
name: string;
@Column('text')
normalBalance: balanceType;
}

View File

@ -21,4 +21,9 @@ export class COA extends BaseModel {
@Column()
user: string;
@Column({
nullable:true
})
relatedUser: string;
}

View File

@ -4,7 +4,6 @@ import { TransactionController } from './transaction.controller';
import { PpobCallbackController } from './ppob_callback.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { COA } from './entities/coa.entity';
import { CoaType } from './entities/coa-type.entity';
import { TransactionType } from './entities/transaction-type.entity';
import { TransactionJournal } from './entities/transaction-journal.entity';
import { Transactions } from './entities/transactions.entity';
@ -15,7 +14,6 @@ import { UsersModule } from 'src/users/users.module';
@Module({
imports: [
TypeOrmModule.forFeature([
CoaType,
TransactionType,
COA,
TransactionJournal,

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 {