@it_kyryl/arbitrage_admin_schema
Version:
Schema for arbitrage admin panel
70 lines (69 loc) • 3.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const big_js_1 = __importDefault(require("big.js"));
class TransactionService {
dependency;
constructor(dependency) {
this.dependency = dependency;
}
async currentToOutstandingTransaction({ currentBalance, financeProfileId, outstandingBalance, payoutPercent, staticPayoutAmount, currency, balanceSettingId, balanceSettingTitle, monthlyReportId, todayDate, dependency: { financeReportRepository, balanceSettingRepository } }) {
let current = new big_js_1.default(currentBalance);
let outstanding = new big_js_1.default(outstandingBalance);
const percent = new big_js_1.default(payoutPercent);
const staticPayout = new big_js_1.default(staticPayoutAmount);
let transactionAmount = new big_js_1.default(0);
// calculate percent value
const percentAmount = current.times(percent);
transactionAmount = transactionAmount.add(percentAmount);
outstanding = outstanding.add(percentAmount);
current = current.minus(percentAmount);
// calculate static payout value
transactionAmount = transactionAmount.add(staticPayout);
outstanding = outstanding.add(staticPayout);
current = current.minus(staticPayout);
//create transaction
await this.dependency.transactionRepository.addTransaction({
transactionType: 'CURRENT_TO_OUTSTANDING',
amount: transactionAmount.toNumber(),
title: balanceSettingTitle,
currency,
financeProfileFromId: financeProfileId,
financeProfileToId: financeProfileId,
monthlyReportId
});
// update finance profile
await this.dependency.financeProfileRepository.updateById(financeProfileId, {
currentBalance: current.toNumber(),
outstandingBalance: outstanding.toNumber()
});
// update out cache
let transactionAmountToSplitBetweenFianceReports = new big_js_1.default(transactionAmount);
const financeReports = await financeReportRepository.getFinanceReportsByFinanceProfileIdForBalanceResolver(financeProfileId);
for (const financeReport of financeReports) {
if (financeReport.remainedBalance > 0) {
const remainedBalance = new big_js_1.default(financeReport.remainedBalance);
let newRemainedBalance = new big_js_1.default(0);
if (remainedBalance.gt(transactionAmountToSplitBetweenFianceReports)) {
newRemainedBalance = remainedBalance.minus(transactionAmountToSplitBetweenFianceReports);
transactionAmountToSplitBetweenFianceReports = new big_js_1.default(0);
}
else {
transactionAmountToSplitBetweenFianceReports = transactionAmountToSplitBetweenFianceReports.minus(remainedBalance);
}
await financeReportRepository.updateById(financeReport.id, {
remainedBalance: newRemainedBalance.toNumber()
});
await balanceSettingRepository.updateBalanceSetting(balanceSettingId, {
currentToOutstandingLastActionDate: todayDate
});
}
}
if (transactionAmountToSplitBetweenFianceReports.gt(0)) {
throw new Error(`There is an issue with remained transaction amount to split, it can't be more then 0. Current amount is ${transactionAmountToSplitBetweenFianceReports.toNumber()}. Finance profile ${financeProfileId}`);
}
}
}
exports.default = TransactionService;