diff --git a/src/helper/enum-list.ts b/src/helper/enum-list.ts index aa0600c..7477e13 100644 --- a/src/helper/enum-list.ts +++ b/src/helper/enum-list.ts @@ -7,6 +7,8 @@ export enum statusTransaction { export enum typeTransaction { DISTRIBUTION, ORDER, + DEPOSIT_IRS, + } export enum productType { diff --git a/src/product/entities/product-history-price.entity.ts b/src/product/entities/product-history-price.entity.ts index 3b28ccb..e7d7d16 100644 --- a/src/product/entities/product-history-price.entity.ts +++ b/src/product/entities/product-history-price.entity.ts @@ -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') diff --git a/src/transaction/coa.service.ts b/src/transaction/coa.service.ts index 679ae12..fcfda29 100644 --- a/src/transaction/coa.service.ts +++ b/src/transaction/coa.service.ts @@ -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) { diff --git a/src/transaction/dto/input-coa.dto.ts b/src/transaction/dto/input-coa.dto.ts index 40b70ae..217ec2d 100644 --- a/src/transaction/dto/input-coa.dto.ts +++ b/src/transaction/dto/input-coa.dto.ts @@ -10,4 +10,7 @@ export class InputCoaDto { @IsNotEmpty() balanceType: balanceType; + + @IsUUID() + relatedUserId: string; } diff --git a/src/transaction/entities/coa-type.entity.ts b/src/transaction/entities/coa-type.entity.ts deleted file mode 100644 index bcbd19f..0000000 --- a/src/transaction/entities/coa-type.entity.ts +++ /dev/null @@ -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; -} diff --git a/src/transaction/entities/coa.entity.ts b/src/transaction/entities/coa.entity.ts index 937c84b..a463b68 100644 --- a/src/transaction/entities/coa.entity.ts +++ b/src/transaction/entities/coa.entity.ts @@ -21,4 +21,9 @@ export class COA extends BaseModel { @Column() user: string; + + @Column({ + nullable:true + }) + relatedUser: string; } diff --git a/src/transaction/transaction.module.ts b/src/transaction/transaction.module.ts index d9cef39..8b1f631 100644 --- a/src/transaction/transaction.module.ts +++ b/src/transaction/transaction.module.ts @@ -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, diff --git a/src/users/dto/create-user.dto.ts b/src/users/dto/create-user.dto.ts index 0f42d58..d9f00b8 100644 --- a/src/users/dto/create-user.dto.ts +++ b/src/users/dto/create-user.dto.ts @@ -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; } diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 3bd249e..4b64e5f 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -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', }; diff --git a/src/users/users.service.ts b/src/users/users.service.ts index c8a33da..845f3a7 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -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 {