From a30a81cbb6d6338ad908948c4c1d2ffcda09c3aa Mon Sep 17 00:00:00 2001 From: ilham Date: Fri, 31 Dec 2021 00:59:49 +0700 Subject: [PATCH] fix: transaction order product --- src/transaction/transaction.service.ts | 30 ++++++++++++++++++++++++-- src/users/users.service.ts | 11 ++++++---- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index be3a07a..3993474 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -10,7 +10,12 @@ import { CoaService } from './coa.service'; import * as uuid from 'uuid'; import { uniq } from 'lodash'; import { Decimal } from 'decimal.js'; -import { balanceType, coaType, statusTransaction, typeTransaction } from '../helper/enum-list'; +import { + balanceType, + coaType, + statusTransaction, + typeTransaction, +} from '../helper/enum-list'; import { ProductService } from '../product/product.service'; import { CreateJournalDto } from './dto/create-journal.dto'; import { UsersService } from 'src/users/users.service'; @@ -362,7 +367,10 @@ export class TransactionService { ]); } - if (coaAccount.amount <= product.price) { + if ( + coaAccount.amount <= + product_price.mark_up_price + product_price.price + ) { throw new HttpException( { statusCode: HttpStatus.INTERNAL_SERVER_ERROR, @@ -446,6 +454,24 @@ export class TransactionService { userData.partner?.id, ); + const coaAccount = await this.coaService.findByUser( + userData.id, + coaType.WALLET, + ); + + if ( + coaAccount.amount <= + product_price.mark_up_price + product_price.price + ) { + throw new HttpException( + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: `Transaction Failed because saldo not enough`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + //TODO HIT API SUPPLIER const trxId = Array(6) .fill(null) diff --git a/src/users/users.service.ts b/src/users/users.service.ts index e5fcb5a..ba2e965 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -310,9 +310,6 @@ export class UsersService { } async findOne(id: string) { - const coa = await this.coaService.findByUser(id, coaType.WALLET); - const coaProfit = await this.coaService.findByUser(id, coaType.PROFIT); - try { const userData = await this.usersRepository .createQueryBuilder('users') @@ -336,11 +333,17 @@ export class UsersService { 'userDetail.phone_number', ]) .getOne(); + const coa = await this.coaService.findByUser(id, coaType.WALLET); + let coaProfit; + if(userData.roles.id != 'e4dfb6a3-2338-464a-8fb8-5cbc089d4209'){ + coaProfit = await this.coaService.findByUser(id, coaType.PROFIT); + }; + return { ...userData, wallet: coa.amount, - profit: coaProfit.amount, + profit: coaProfit ? coaProfit.amount : coaProfit, }; } catch (e) { if (e instanceof EntityNotFoundError) {