refactor: rules now accept partial transaction object instead of TransactionInput

pull/8/head
Inga 🏳‍🌈 11 months ago
parent 279e5230e5
commit ac7aabb7c4
  1. 4
      src/transaction/transaction.controller.spec.ts
  2. 43
      src/transaction/transaction.controller.ts

@ -70,9 +70,9 @@ describe('TransactionController Unit Tests', () => {
});
it('calling applyRules method minimum result of applied rules should be got', async () => {
const mockTransactionInput: TransactionInput = {
const mockTransactionInput = {
date: '2021-01-05',
amount: '1000.00',
amount: 1000.0,
currency: 'EUR',
client_id: 1,
};

@ -12,6 +12,7 @@ import {
DefaultCommissionAmount,
HighTurnoverDiscount,
} from './transaction.dto';
import { Transaction } from './transaction.entity';
@Controller('transaction')
export class TransactionController {
@ -38,7 +39,9 @@ export class TransactionController {
});
}
getClientDeposit = async (transactionInput: TransactionInput) => {
getClientDeposit = async (
transactionInput: Pick<Transaction, 'client_id'>,
) => {
try {
const deposit =
await this.transactionService.findByClientIdWithinActualMonth(
@ -61,7 +64,7 @@ export class TransactionController {
return 0;
};
turnoverRule = async (transactionInput: TransactionInput) => {
turnoverRule = async (transactionInput: Pick<Transaction, 'client_id'>) => {
try {
const clientDeposit = await this.getClientDeposit(transactionInput);
if (clientDeposit) {
@ -72,24 +75,25 @@ export class TransactionController {
}
};
discountRule(transactionInput: TransactionInput) {
discountRule(transactionInput: Pick<Transaction, 'client_id'>) {
return transactionInput.client_id === 42
? DiscountRuleForClientById.client_42
: false;
}
defaultRule(transactionInput: TransactionInput) {
defaultRule(transactionInput: Pick<Transaction, 'amount'>) {
const commissionAmount =
(parseInt(transactionInput.amount) / 100) *
DefaultCommissionPercentage.percentage;
(transactionInput.amount * DefaultCommissionPercentage.percentage) / 100;
return commissionAmount < DefaultCommissionAmount.amount
? DefaultCommissionAmount.amount
: commissionAmount;
}
async applyRules(
rules: ((transactionInput: TransactionInput) => any)[],
transactionInput: TransactionInput,
rules: ((
transactionInput: Pick<Transaction, 'client_id' | 'amount'>,
) => any)[],
transactionInput: Pick<Transaction, 'client_id' | 'amount'>,
) {
let commissionAmount;
for (let i = 0; i < rules.length; i++) {
@ -120,9 +124,16 @@ export class TransactionController {
}
getAmountWithExchange(transactionInput: TransactionInput) {
const transactionData = {
date: transactionInput.date,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
};
const commissionAmount = this.applyRules(
[this.turnoverRule, this.discountRule],
transactionInput,
transactionData,
);
const exhangeRateInput = {
@ -133,10 +144,7 @@ export class TransactionController {
.then((exchangeRateResponse) =>
commissionAmount.then((commission) =>
this.transactionService.insertOne({
date: transactionInput.date,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
...transactionData,
commission,
base_currency: Currency.EUR,
base_amount:
@ -151,9 +159,16 @@ export class TransactionController {
}
async getAmountWithoutExchange(transactionInput: TransactionInput) {
const transactionData = {
date: transactionInput.date,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
};
const commissionAmount = await this.applyRules(
[this.turnoverRule, this.discountRule],
transactionInput,
transactionData,
);
try {
this.transactionService

Loading…
Cancel
Save