Merge branch 'development' of https://gitlab.com/empatnusabangsa/ppob/ppob-backend into devops-staging

This commit is contained in:
Fadli 2022-10-21 17:27:25 +07:00
commit de41172c5f
2 changed files with 264 additions and 274 deletions

View File

@ -10,7 +10,6 @@ export const doTransaction = async (
productCode, productCode,
destination, destination,
idtrx, idtrx,
idtrxhemat,
supplier, supplier,
authorization, authorization,
typePaid, typePaid,
@ -36,7 +35,7 @@ export const doTransaction = async (
'Authorization': 'Bearer ' + authorization} 'Authorization': 'Bearer ' + authorization}
}; };
const data = { const data = {
idtransaction: idtrxhemat, idtransaction: idtrx,
destination: `${destination}`, destination: `${destination}`,
code: `${productCode}`, code: `${productCode}`,
type: `${typePaid}`, type: `${typePaid}`,

View File

@ -523,271 +523,267 @@ export class TransactionService {
); );
const statusProduct = await this.productHistoryStatusService.findOneActive( const statusProduct = await this.productHistoryStatusService.findOneActive(
product.id, product.id,
userData.partner == null ? null : userData.partner.id, userData.partner == null ? null : userData.partner.id,
); );
if (statusProduct.status == 'NOT ACTIVE') { 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( throw new HttpException(
{ {
statusCode: HttpStatus.INTERNAL_SERVER_ERROR, statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
error: `Transaction Failed because saldo not enough`, error: `Transaction Failed because product is not active`,
}, },
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} } else {
console.log('userdatapartnerid', userData.partner?.id);
//HIT API SUPPLIER let product_price = await this.productHistoryPriceService.findOne(
const trxId = Array(6) product.id,
.fill(null) userData.partner?.id,
.map(() => { );
return Math.round(Math.random() * 16).toString(16);
})
.join('');
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') { const coaInventory = await this.coaService.findByName(
hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); `${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
} );
let hitSupplier = supplier.code == 'Hemat' ? const coaCostOfSales = await this.coaService.findByName(
await doTransaction( `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
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',
);
// let hitSupplier; const coaSales = await this.coaService.findByName(
`${coaType[coaType.SALES]}-SYSTEM`,
);
console.log('iniresponsupplierharga', hitSupplier) if (orderTransactionDto.bill_trx_id) {
console.log('iniresponsupplier', hitSupplier.harga) try {
console.log(supplier.code, 'sdkfjsd'); const billId = await this.checkBillHistoryRepository.findOneOrFail({
console.log(product_price, 'price'); where: {
trx_id: orderTransactionDto.bill_trx_id,
},
});
product_price.price = billId.amount;
if (supplier.code == 'metro') { } catch (e) {
const parsingResponse = hitSupplier.split(' '); if (e instanceof EntityNotFoundError) {
console.log('parsingResponse', parsingResponse) throw new HttpException(
const newHitSupplier = { {
success: hitSupplier.includes('diproses'), statusCode: HttpStatus.NOT_FOUND,
harga: parseInt( error: 'Bill not found',
parsingResponse[parsingResponse.length - 2].replace(/\./g, ' '), },
), HttpStatus.NOT_FOUND,
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,
); );
} else {
await this.productHistoryPriceService.updateEndDate(product.id); throw e;
}
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); 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({ //HIT API SUPPLIER
createTransaction: false, const trxId = Array(6)
transactionalEntityManager: manager, .fill(null)
transaction: transactionData, .map(() => {
amount: transactionData.amount, return Math.round(Math.random() * 16).toString(16);
journals: [ })
{ .join('');
coa_id: coaInventory.id,
credit: costInventory, let hitLoginHemat;
},
{ if (supplier.code == 'Hemat') {
coa_id: coaCostOfSales.id, hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier);
debit: costInventory, }
},
{ let hitSupplier = supplier.code == 'Hemat' ?
coa_id: coaAccount.id, await doTransaction(
debit: product_price.mark_up_price + costInventory, orderTransactionDto.productCode,
}, orderTransactionDto.destination,
{ trxId,
// eslint-disable-next-line camelcase supplier,
coa_id: coaSales.id, hitLoginHemat.data,
credit: product_price.mark_up_price + costInventory, 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 { let costInventory = product_price.price;
trx_id: trxId,
client_trx_id: orderTransactionDto.trx_id, if (hitSupplier.harga != undefined) {
product: orderTransactionDto.productCode, if (hitSupplier.harga != product_price.price) {
amount: product_price.mark_up_price + product_price.price, console.log('priceupdate1supplier', hitSupplier.harga);
status: status, 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(''); .join('');
const trxIdHemat = orderTransactionDto.trx_id;
let hitLoginHemat; let hitLoginHemat;
if (supplier.code == 'Hemat') { if (supplier.code == 'Hemat') {
@ -868,7 +863,6 @@ export class TransactionService {
orderTransactionDto.productCode, orderTransactionDto.productCode,
orderTransactionDto.destination, orderTransactionDto.destination,
trxId, trxId,
trxIdHemat,
supplier, supplier,
hitLoginHemat.data, hitLoginHemat.data,
product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT',
@ -876,7 +870,6 @@ export class TransactionService {
orderTransactionDto.productCode, orderTransactionDto.productCode,
orderTransactionDto.destination, orderTransactionDto.destination,
trxId, trxId,
trxIdHemat,
supplier, supplier,
"", "",
product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT',
@ -1010,7 +1003,7 @@ export class TransactionService {
return Math.round(Math.random() * 16).toString(16); return Math.round(Math.random() * 16).toString(16);
}) })
.join(''); .join('');
const trxIdHemat = orderTransactionDto.trx_id;
let status; let status;
try { try {
@ -1018,14 +1011,13 @@ export class TransactionService {
let hitLoginHemat; let hitLoginHemat;
if (supplier.code == 'Hemat') { 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( let hitSupplier = supplier.code == 'Hemat' ? await doTransaction(
orderTransactionDto.productCode, orderTransactionDto.productCode,
orderTransactionDto.destination, orderTransactionDto.destination,
trxId, trxId,
trxIdHemat,
supplier, supplier,
hitLoginHemat.data, hitLoginHemat.data,
'INQUIRY' 'INQUIRY'
@ -1033,7 +1025,6 @@ export class TransactionService {
`CEK${orderTransactionDto.productCode.slice(3)}`, `CEK${orderTransactionDto.productCode.slice(3)}`,
orderTransactionDto.destination, orderTransactionDto.destination,
trxId, trxId,
trxIdHemat,
supplier, supplier,
"", "",
'INQUIRY' 'INQUIRY'
@ -1266,19 +1257,19 @@ export class TransactionService {
// } else { // } else {
const updateTransaction = await this.callbackOrderFailed( const updateTransaction = await this.callbackOrderFailed(
supplier_trx_id, supplier_trx_id,
callback, callback,
); );
throw new HttpException( throw new HttpException(
{ {
updateTransaction, updateTransaction,
statusCode: HttpStatus.BAD_REQUEST, statusCode: HttpStatus.BAD_REQUEST,
error: 'updated transaction to failed', error: 'updated transaction to failed',
}, },
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
); );
// } // }
} }
@ -1304,19 +1295,19 @@ export class TransactionService {
// HttpStatus.BAD_REQUEST, // HttpStatus.BAD_REQUEST,
// ); // );
// } else { // } else {
const updateTransaction = await this.callbackOrderSuccess( const updateTransaction = await this.callbackOrderSuccess(
supplier_trx_id, supplier_trx_id,
callback, callback,
); );
throw new HttpException( throw new HttpException(
{ {
updateTransaction, updateTransaction,
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
error: 'success', error: 'success',
}, },
HttpStatus.OK, HttpStatus.OK,
); );
// } // }
} }