- handle when product is not active cannot hit api transaction order prod

This commit is contained in:
Fadli 2022-08-03 22:47:59 +07:00
parent c184fdddc5
commit 245e876144

View File

@ -456,44 +456,59 @@ export class TransactionService {
orderTransactionDto: OrderTransactionDto,
currentUser: any,
) {
const productData = await this.productService.findOne(
orderTransactionDto.productCode,
'prepaid',
);
if (productData.status == 'NOT ACTIVE') {
throw new HttpException(
{
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
error: `Transaction Failed because product is not active`,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
} else {
let status;
const amount = 0;
//GET USER DATA
const userData = await this.userService.findByUsername(
currentUser.username,
currentUser.username,
);
//GET PRODUCT AND PRICE
const product = await this.productService.findOne(
orderTransactionDto.productCode,
'prepaid',
orderTransactionDto.productCode,
'prepaid',
);
const supplier = await this.supplierService.findByCode(
product.supplier.code,
product.supplier.code,
);
let product_price = await this.productHistoryPriceService.findOne(
product.id,
userData.partner?.id,
product.id,
userData.partner?.id,
);
//GET COA
const coaAccount = await this.coaService.findByUser(
userData.id,
coaType.WALLET,
userData.id,
coaType.WALLET,
);
const coaInventory = await this.coaService.findByName(
`${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
`${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
);
const coaCostOfSales = await this.coaService.findByName(
`${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
`${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
);
const coaSales = await this.coaService.findByName(
`${coaType[coaType.SALES]}-SYSTEM`,
`${coaType[coaType.SALES]}-SYSTEM`,
);
if (orderTransactionDto.bill_trx_id) {
@ -507,11 +522,11 @@ export class TransactionService {
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Bill not found',
},
HttpStatus.NOT_FOUND,
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Bill not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
@ -521,27 +536,27 @@ export class TransactionService {
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,
{
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
error: `Transaction Failed because saldo not enough`,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
//HIT API SUPPLIER
const trxId = Array(6)
.fill(null)
.map(() => {
return Math.round(Math.random() * 16).toString(16);
})
.join('');
.fill(null)
.map(() => {
return Math.round(Math.random() * 16).toString(16);
})
.join('');
let hitSupplier = await doTransaction(
orderTransactionDto.productCode,
orderTransactionDto.destination,
trxId,
supplier,
orderTransactionDto.productCode,
orderTransactionDto.destination,
trxId,
supplier,
);
// let hitSupplier;
@ -550,12 +565,12 @@ export class TransactionService {
const newHitSupplier = {
success: hitSupplier.includes('diproses'),
harga: parseInt(
parsingResponse[parsingResponse.length - 2].replace(/\./g,' '),
parsingResponse[parsingResponse.length - 2].replace(/\./g, ' '),
),
msg: hitSupplier,
};
hitSupplier = newHitSupplier;
if(orderTransactionDto.bill_trx_id !== null){
if (orderTransactionDto.bill_trx_id !== null) {
hitSupplier.harga = product_price.price;
}
}
@ -575,9 +590,9 @@ export class TransactionService {
product_price.endDate = new Date();
costInventory = hitSupplier.harga;
const listActivePrice =
await this.productHistoryPriceService.getAllActivePriceByProduct(
product.id,
);
await this.productHistoryPriceService.getAllActivePriceByProduct(
product.id,
);
await this.productHistoryPriceService.updateEndDate(product.id);
@ -604,7 +619,7 @@ export class TransactionService {
transactionData.id = uuid.v4();
transactionData.amount =
product_price.mark_up_price + product_price.price;
product_price.mark_up_price + product_price.price;
transactionData.user = userData.id;
transactionData.type = typeTransaction.ORDER;
transactionData.product_price = product_price;
@ -618,11 +633,11 @@ export class TransactionService {
status = statusTransaction[transactionData.status];
await this.transactionRepository.insert(transactionData);
throw new HttpException(
{
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
error: hitSupplier.msg,
},
HttpStatus.INTERNAL_SERVER_ERROR,
{
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
error: hitSupplier.msg,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
} else {
transactionData.status = statusTransaction.PENDING;
@ -669,6 +684,7 @@ export class TransactionService {
status: status,
};
}
}
async orderTransactionBillProd(
orderTransactionDto: OrderTransactionDto,