@lomray/microservice-payment-stripe
Version:
Stripe payment microservice based on NodeJS & inverted json.
94 lines (90 loc) • 4.05 kB
JavaScript
;
var tslib = require('tslib');
var microserviceNodejsLib = require('@lomray/microservice-nodejs-lib');
var typeorm = require('typeorm');
var refundStatus = require('../constants/refund-status.js');
var transactionStatus = require('../constants/transaction-status.js');
var transactionType = require('../constants/transaction-type.js');
var refund = require('../entities/refund.js');
var transaction = require('../entities/transaction.js');
var messages = require('../helpers/validators/messages.js');
/**
* Refund repository
*/
let Refund = class Refund extends typeorm.Repository {
/**
* Get is entity refund
* @example Cast types avoid from ObjectLiteral or undefined in after update
*/
static getIsEntityRefund(entity) {
return entity instanceof refund;
}
/**
* Update transactions refund status
* @description Will cause transaction update
*/
static updateTransactionsRefundStatus(transactionId, manager,
// Refunded charge amount
chargeRefundedAmount) {
var _a;
return tslib.__awaiter(this, void 0, void 0, function* () {
const transactionRepository = manager.getRepository(transaction);
const refundRepository = manager.getRepository(refund);
// Get transaction and refunds
const where = {
transactionId,
};
const [refunds, transactions] = yield Promise.all([
refundRepository.find({ where }),
transactionRepository.find({ where }),
]);
const totalTransactionAmount = (_a = transactions.find(({ type }) => type === transactionType.CREDIT)) === null || _a === void 0 ? void 0 : _a.amount;
if (!transactions.length || !totalTransactionAmount) {
throw new microserviceNodejsLib.BaseException({
status: 500,
message: messages.getNotFoundMessage('Failed to update transaction status on updated refund. Debit or credit transaction'),
});
}
/**
* Pure refunded amount based on refunds, not charge refunded amount
* @description If refund failed - charge will contain refunded amount that will not be refunded
*/
const failedRefundedAmount = refunds.reduce((res, { amount, status }) => {
if (status !== refundStatus.ERROR) {
return res;
}
return res + amount;
}, 0);
// Update transactions status
transactions.forEach((transaction) => {
// Get updated or existing refunded amount
const refundedAmount = chargeRefundedAmount !== null && chargeRefundedAmount !== void 0 ? chargeRefundedAmount : transaction.params.refundedTransactionAmount;
// Calculate real refunded amount
transaction.params.successRefundedAmount = refundedAmount - failedRefundedAmount;
// Update refunded amount if it occurs in charge
if (chargeRefundedAmount) {
transaction.params.refundedTransactionAmount = chargeRefundedAmount;
}
// If refund failed - status should be failed refund
if (failedRefundedAmount) {
transaction.status = transactionStatus.REFUND_FAILED;
return;
}
// If refunded amount is 0
if (!refundedAmount) {
return;
}
transaction.status =
refundedAmount === totalTransactionAmount
? transactionStatus.REFUNDED
: transactionStatus.PARTIAL_REFUNDED;
});
yield transactionRepository.save(transactions);
});
}
};
Refund = tslib.__decorate([
typeorm.EntityRepository(refund)
], Refund);
var RefundRepostory = Refund;
module.exports = RefundRepostory;