diff --git a/src/helper/irs-api.ts b/src/helper/irs-api.ts index 063c9e6..0e5fa6e 100644 --- a/src/helper/irs-api.ts +++ b/src/helper/irs-api.ts @@ -10,7 +10,6 @@ export const doTransaction = async ( productCode, destination, idtrx, - idtrxhemat, supplier, authorization, typePaid, @@ -36,7 +35,7 @@ export const doTransaction = async ( 'Authorization': 'Bearer ' + authorization} }; const data = { - idtransaction: idtrxhemat, + idtransaction: idtrx, destination: `${destination}`, code: `${productCode}`, type: `${typePaid}`, diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index cdb7815..8f70ea8 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -523,271 +523,267 @@ export class TransactionService { ); const statusProduct = await this.productHistoryStatusService.findOneActive( - product.id, + product.id, userData.partner == null ? null : userData.partner.id, ); if (statusProduct.status == 'NOT ACTIVE') { - throw new HttpException( - { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: `Transaction Failed because product is not active`, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } else { - console.log('userdatapartnerid', userData.partner?.id); - - let product_price = await this.productHistoryPriceService.findOne( - product.id, - userData.partner?.id, - ); - - //GET COA - console.log('coamsk5', product_price.id); - - const coaAccount = await this.coaService.findByUser( - userData.id, - coaType.WALLET, - ); - - const coaInventory = await this.coaService.findByName( - `${coaType[coaType.INVENTORY]}-${product.supplier.code}`, - ); - - const coaCostOfSales = await this.coaService.findByName( - `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`, - ); - - const coaSales = await this.coaService.findByName( - `${coaType[coaType.SALES]}-SYSTEM`, - ); - - if (orderTransactionDto.bill_trx_id) { - try { - const billId = await this.checkBillHistoryRepository.findOneOrFail({ - where: { - trx_id: orderTransactionDto.bill_trx_id, - }, - }); - - product_price.price = billId.amount; - } catch (e) { - if (e instanceof EntityNotFoundError) { - throw new HttpException( - { - statusCode: HttpStatus.NOT_FOUND, - error: 'Bill not found', - }, - HttpStatus.NOT_FOUND, - ); - } else { - throw e; - } - } - } - - 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`, + error: `Transaction Failed because product is not active`, }, HttpStatus.INTERNAL_SERVER_ERROR, ); - } + } else { + console.log('userdatapartnerid', userData.partner?.id); - //HIT API SUPPLIER - const trxId = Array(6) - .fill(null) - .map(() => { - return Math.round(Math.random() * 16).toString(16); - }) - .join(''); + let product_price = await this.productHistoryPriceService.findOne( + product.id, + userData.partner?.id, + ); - const trxIdHemat = orderTransactionDto.trx_id; + //GET COA + console.log('coamsk5', product_price.id); - let hitLoginHemat; + const coaAccount = await this.coaService.findByUser( + userData.id, + coaType.WALLET, + ); - if (supplier.code == 'Hemat') { - hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); - } + const coaInventory = await this.coaService.findByName( + `${coaType[coaType.INVENTORY]}-${product.supplier.code}`, + ); - let hitSupplier = supplier.code == 'Hemat' ? - await doTransaction( - orderTransactionDto.productCode, - orderTransactionDto.destination, - trxId, - trxIdHemat, - supplier, - hitLoginHemat.data, - product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', - ) : await doTransaction( - orderTransactionDto.productCode, - orderTransactionDto.destination, - trxId, - trxIdHemat, - supplier, - "", - product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', - ); + const coaCostOfSales = await this.coaService.findByName( + `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`, + ); - // let hitSupplier; + const coaSales = await this.coaService.findByName( + `${coaType[coaType.SALES]}-SYSTEM`, + ); - console.log('iniresponsupplierharga', hitSupplier) - console.log('iniresponsupplier', hitSupplier.harga) - console.log(supplier.code, 'sdkfjsd'); - console.log(product_price, 'price'); + if (orderTransactionDto.bill_trx_id) { + try { + const billId = await this.checkBillHistoryRepository.findOneOrFail({ + where: { + trx_id: orderTransactionDto.bill_trx_id, + }, + }); - - if (supplier.code == 'metro') { - const parsingResponse = hitSupplier.split(' '); - console.log('parsingResponse', parsingResponse) - const newHitSupplier = { - success: hitSupplier.includes('diproses'), - harga: parseInt( - parsingResponse[parsingResponse.length - 2].replace(/\./g, ' '), - ), - msg: hitSupplier, - }; - - hitSupplier = newHitSupplier; - - if (orderTransactionDto.bill_trx_id !== null) { - hitSupplier.harga = product_price.price; - } - } else if (supplier.code == 'Hemat') { - const newHitSupplier = { - success: hitSupplier.success, - harga: product_price.price, - msg: hitSupplier.msg, - }; - - hitSupplier = newHitSupplier; - - if (orderTransactionDto.bill_trx_id !== null) { - hitSupplier.harga = product_price.price; - } - } - - console.log(hitSupplier, 'newhitSupplier'); - - // const hitSupplier = { - // harga: 2000, - // success: true, - // msg: 'Berhasil', - // }; - - this.logger.log({ - responseAPISupplier: hitSupplier, - }); - - let costInventory = product_price.price; - - if (hitSupplier.harga != undefined) { - if (hitSupplier.harga != product_price.price) { - console.log('priceupdate1supplier', hitSupplier.harga); - console.log('priceupdate1normal', product_price.price); - product_price.endDate = new Date(); - costInventory = hitSupplier.harga; - - const listActivePrice = - await this.productHistoryPriceService.getAllActivePriceByProduct( - product.id, + product_price.price = billId.amount; + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Bill not found', + }, + HttpStatus.NOT_FOUND, ); - - await this.productHistoryPriceService.updateEndDate(product.id); - - listActivePrice.map(async (x) => { - const newProductPrice = new ProductHistoryPrice(); - - newProductPrice.id = uuid.v4(); - newProductPrice.type = x.type; - newProductPrice.price = hitSupplier.harga; - newProductPrice.mark_up_price = x.mark_up_price; - newProductPrice.startDate = new Date(); - newProductPrice.product = product; - newProductPrice.partner = x.partner; - await this.productHistoryPriceService.create(newProductPrice); - - product_price = newProductPrice; - }); - } - } - - try { - //TRANSACTION DATA - await this.connection.transaction(async (manager) => { - const transactionData = new Transactions(); - - transactionData.id = uuid.v4(); - transactionData.amount = - product_price.mark_up_price + product_price.price; - transactionData.user = userData.id; - transactionData.type = typeTransaction.ORDER; - transactionData.product_price = product_price; - transactionData.destination = orderTransactionDto.destination; - transactionData.partner_trx_id = orderTransactionDto.trx_id; - transactionData.supplier_trx_id = trxId; - transactionData.check_bill = orderTransactionDto.bill_trx_id; - transactionData.balance_remaining = - coaAccount.amount - product_price.mark_up_price - costInventory; - - if (!hitSupplier.success) { - transactionData.status = statusTransaction.FAILED; - status = statusTransaction[transactionData.status]; - await this.transactionRepository.insert(transactionData); - throw new HttpException( - { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: hitSupplier.msg, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } else { - transactionData.status = statusTransaction.PENDING; - status = statusTransaction[transactionData.status]; + } else { + throw e; + } } + } - await manager.insert(Transactions, transactionData); + 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, + ); + } - await this.accountingTransaction({ - createTransaction: false, - transactionalEntityManager: manager, - transaction: transactionData, - amount: transactionData.amount, - journals: [ - { - coa_id: coaInventory.id, - credit: costInventory, - }, - { - coa_id: coaCostOfSales.id, - debit: costInventory, - }, - { - coa_id: coaAccount.id, - debit: product_price.mark_up_price + costInventory, - }, - { - // eslint-disable-next-line camelcase - coa_id: coaSales.id, - credit: product_price.mark_up_price + costInventory, - }, - ], - }); + //HIT API SUPPLIER + const trxId = Array(6) + .fill(null) + .map(() => { + return Math.round(Math.random() * 16).toString(16); + }) + .join(''); + + let hitLoginHemat; + + if (supplier.code == 'Hemat') { + hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); + } + + let hitSupplier = supplier.code == 'Hemat' ? + await doTransaction( + orderTransactionDto.productCode, + orderTransactionDto.destination, + trxId, + supplier, + hitLoginHemat.data, + product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', + ) : await doTransaction( + orderTransactionDto.productCode, + orderTransactionDto.destination, + trxId, + supplier, + "", + product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', + ); + + // let hitSupplier; + + console.log('iniresponsupplierharga', hitSupplier) + console.log('iniresponsupplier', hitSupplier.harga) + console.log(supplier.code, 'sdkfjsd'); + console.log(product_price, 'price'); + + + if (supplier.code == 'metro') { + const parsingResponse = hitSupplier.split(' '); + console.log('parsingResponse', parsingResponse) + const newHitSupplier = { + success: hitSupplier.includes('diproses'), + harga: parseInt( + parsingResponse[parsingResponse.length - 2].replace(/\./g, ' '), + ), + msg: hitSupplier, + }; + + hitSupplier = newHitSupplier; + + if (orderTransactionDto.bill_trx_id !== null) { + hitSupplier.harga = product_price.price; + } + } else if (supplier.code == 'Hemat') { + const newHitSupplier = { + success: hitSupplier.success, + harga: product_price.price, + msg: hitSupplier.msg, + }; + + hitSupplier = newHitSupplier; + + if (orderTransactionDto.bill_trx_id !== null) { + hitSupplier.harga = product_price.price; + } + } + + console.log(hitSupplier, 'newhitSupplier'); + + // const hitSupplier = { + // harga: 2000, + // success: true, + // msg: 'Berhasil', + // }; + + this.logger.log({ + responseAPISupplier: hitSupplier, }); - } catch (e) { - throw e; - } - return { - trx_id: trxId, - client_trx_id: orderTransactionDto.trx_id, - product: orderTransactionDto.productCode, - amount: product_price.mark_up_price + product_price.price, - status: status, - }; + let costInventory = product_price.price; + + if (hitSupplier.harga != undefined) { + if (hitSupplier.harga != product_price.price) { + console.log('priceupdate1supplier', hitSupplier.harga); + console.log('priceupdate1normal', product_price.price); + product_price.endDate = new Date(); + costInventory = hitSupplier.harga; + + const listActivePrice = + await this.productHistoryPriceService.getAllActivePriceByProduct( + product.id, + ); + + await this.productHistoryPriceService.updateEndDate(product.id); + + listActivePrice.map(async (x) => { + const newProductPrice = new ProductHistoryPrice(); + + newProductPrice.id = uuid.v4(); + newProductPrice.type = x.type; + newProductPrice.price = hitSupplier.harga; + newProductPrice.mark_up_price = x.mark_up_price; + newProductPrice.startDate = new Date(); + newProductPrice.product = product; + newProductPrice.partner = x.partner; + await this.productHistoryPriceService.create(newProductPrice); + + product_price = newProductPrice; + }); + } + } + + try { + //TRANSACTION DATA + await this.connection.transaction(async (manager) => { + const transactionData = new Transactions(); + + transactionData.id = uuid.v4(); + transactionData.amount = + product_price.mark_up_price + product_price.price; + transactionData.user = userData.id; + transactionData.type = typeTransaction.ORDER; + transactionData.product_price = product_price; + transactionData.destination = orderTransactionDto.destination; + transactionData.partner_trx_id = orderTransactionDto.trx_id; + transactionData.supplier_trx_id = trxId; + transactionData.check_bill = orderTransactionDto.bill_trx_id; + transactionData.balance_remaining = + coaAccount.amount - product_price.mark_up_price - costInventory; + + if (!hitSupplier.success) { + transactionData.status = statusTransaction.FAILED; + status = statusTransaction[transactionData.status]; + await this.transactionRepository.insert(transactionData); + throw new HttpException( + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: hitSupplier.msg, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } else { + transactionData.status = statusTransaction.PENDING; + status = statusTransaction[transactionData.status]; + } + + await manager.insert(Transactions, transactionData); + + await this.accountingTransaction({ + createTransaction: false, + transactionalEntityManager: manager, + transaction: transactionData, + amount: transactionData.amount, + journals: [ + { + coa_id: coaInventory.id, + credit: costInventory, + }, + { + coa_id: coaCostOfSales.id, + debit: costInventory, + }, + { + coa_id: coaAccount.id, + debit: product_price.mark_up_price + costInventory, + }, + { + // eslint-disable-next-line camelcase + coa_id: coaSales.id, + credit: product_price.mark_up_price + costInventory, + }, + ], + }); + }); + } catch (e) { + throw e; + } + + return { + trx_id: trxId, + client_trx_id: orderTransactionDto.trx_id, + product: orderTransactionDto.productCode, + amount: product_price.mark_up_price + product_price.price, + status: status, + }; } } @@ -856,7 +852,6 @@ export class TransactionService { }) .join(''); - const trxIdHemat = orderTransactionDto.trx_id; let hitLoginHemat; if (supplier.code == 'Hemat') { @@ -868,7 +863,6 @@ export class TransactionService { orderTransactionDto.productCode, orderTransactionDto.destination, trxId, - trxIdHemat, supplier, hitLoginHemat.data, product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', @@ -876,7 +870,6 @@ export class TransactionService { orderTransactionDto.productCode, orderTransactionDto.destination, trxId, - trxIdHemat, supplier, "", product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', @@ -1010,7 +1003,7 @@ export class TransactionService { return Math.round(Math.random() * 16).toString(16); }) .join(''); - const trxIdHemat = orderTransactionDto.trx_id; + let status; try { @@ -1018,14 +1011,13 @@ export class TransactionService { let hitLoginHemat; if (supplier.code == 'Hemat') { - hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); + hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); } let hitSupplier = supplier.code == 'Hemat' ? await doTransaction( orderTransactionDto.productCode, orderTransactionDto.destination, trxId, - trxIdHemat, supplier, hitLoginHemat.data, 'INQUIRY' @@ -1033,7 +1025,6 @@ export class TransactionService { `CEK${orderTransactionDto.productCode.slice(3)}`, orderTransactionDto.destination, trxId, - trxIdHemat, supplier, "", 'INQUIRY' @@ -1266,19 +1257,19 @@ export class TransactionService { // } else { - const updateTransaction = await this.callbackOrderFailed( - supplier_trx_id, - callback, - ); + const updateTransaction = await this.callbackOrderFailed( + supplier_trx_id, + callback, + ); - throw new HttpException( - { - updateTransaction, - statusCode: HttpStatus.BAD_REQUEST, - error: 'updated transaction to failed', - }, - HttpStatus.BAD_REQUEST, - ); + throw new HttpException( + { + updateTransaction, + statusCode: HttpStatus.BAD_REQUEST, + error: 'updated transaction to failed', + }, + HttpStatus.BAD_REQUEST, + ); // } } @@ -1304,19 +1295,19 @@ export class TransactionService { // HttpStatus.BAD_REQUEST, // ); // } else { - const updateTransaction = await this.callbackOrderSuccess( - supplier_trx_id, - callback, - ); + const updateTransaction = await this.callbackOrderSuccess( + supplier_trx_id, + callback, + ); - throw new HttpException( - { - updateTransaction, - statusCode: HttpStatus.OK, - error: 'success', - }, - HttpStatus.OK, - ); + throw new HttpException( + { + updateTransaction, + statusCode: HttpStatus.OK, + error: 'success', + }, + HttpStatus.OK, + ); // } }