Fixed test checking commission for non-EUR original currencies #8

Merged
inga-lovinde merged 6 commits from fix-6-non-eur-currencies into main 10 months ago
  1. 4
      src/transaction/transaction.controller.spec.ts
  2. 109
      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 () => { it('calling applyRules method minimum result of applied rules should be got', async () => {
const mockTransactionInput: TransactionInput = { const mockTransactionInput = {
date: '2021-01-05', date: '2021-01-05',
amount: '1000.00', base_amount: 1000.0,
currency: 'EUR', currency: 'EUR',
client_id: 1, client_id: 1,
}; };

@ -1,4 +1,5 @@
import { Controller, Post, UsePipes, Body } from '@nestjs/common'; import { Controller, Post, UsePipes, Body } from '@nestjs/common';
import { ExchangeRateInput, ExchangeRateResponse } from 'src/exchange-rate/exchange-rate.dto';
import { ExchangeRateService } from 'src/exchange-rate/exchange-rate.service'; import { ExchangeRateService } from 'src/exchange-rate/exchange-rate.service';
import { TransactionService } from './transaction.service'; import { TransactionService } from './transaction.service';
import { transactionBodySchema } from './transaction.validation'; import { transactionBodySchema } from './transaction.validation';
@ -11,6 +12,9 @@ import {
DefaultCommissionAmount, DefaultCommissionAmount,
HighTurnoverDiscount, HighTurnoverDiscount,
} from './transaction.dto'; } from './transaction.dto';
import { Transaction } from './transaction.entity';
type TransactionRuleData = Pick<Transaction, 'base_amount' | 'client_id'>;
@Controller('transaction') @Controller('transaction')
export class TransactionController { export class TransactionController {
@ -37,7 +41,7 @@ export class TransactionController {
}); });
} }
getClientDeposit = async (transactionInput: TransactionInput) => { getClientDeposit = async (transactionInput: TransactionRuleData) => {
try { try {
const deposit = const deposit =
await this.transactionService.findByClientIdWithinActualMonth( await this.transactionService.findByClientIdWithinActualMonth(
@ -60,7 +64,7 @@ export class TransactionController {
return 0; return 0;
}; };
turnoverRule = async (transactionInput: TransactionInput) => { turnoverRule = async (transactionInput: TransactionRuleData) => {
try { try {
const clientDeposit = await this.getClientDeposit(transactionInput); const clientDeposit = await this.getClientDeposit(transactionInput);
if (clientDeposit) { if (clientDeposit) {
@ -71,24 +75,26 @@ export class TransactionController {
} }
}; };
discountRule(transactionInput: TransactionInput) { discountRule(transactionInput: TransactionRuleData) {
return transactionInput.client_id === 42 return transactionInput.client_id === 42
? DiscountRuleForClientById.client_42 ? DiscountRuleForClientById.client_42
: false; : false;
} }
defaultRule(transactionInput: TransactionInput) { defaultRule(transactionInput: TransactionRuleData) {
const commissionAmount = const commissionAmount =
(parseInt(transactionInput.amount) / 100) * (transactionInput.base_amount * DefaultCommissionPercentage.percentage) /
DefaultCommissionPercentage.percentage; 100;
return commissionAmount < DefaultCommissionAmount.amount return commissionAmount < DefaultCommissionAmount.amount
? DefaultCommissionAmount.amount ? DefaultCommissionAmount.amount
: commissionAmount; : commissionAmount;
} }
async applyRules( async applyRules(
rules: ((transactionInput: TransactionInput) => any)[], rules: ((
transactionInput: TransactionInput, transactionInput: TransactionRuleData,
) => number | false | undefined | Promise<number | false | undefined>)[],
transactionInput: TransactionRuleData,
) { ) {
let commissionAmount; let commissionAmount;
for (let i = 0; i < rules.length; i++) { for (let i = 0; i < rules.length; i++) {
@ -105,60 +111,71 @@ export class TransactionController {
: this.defaultRule(transactionInput); : this.defaultRule(transactionInput);
} }
getAmountWithExchange(transactionInput: TransactionInput) { getExchangeRate(exchangeRateInput: ExchangeRateInput) {
const commissionAmount = this.applyRules( return new Promise<ExchangeRateResponse>((resolve, reject) => {
[this.turnoverRule, this.discountRule], try {
transactionInput, this.exchangeRateService.convertCurrency(exchangeRateInput).subscribe({
); next: resolve,
error: reject,
});
} catch (error) {
reject(error);
}
});
}
const exhangeRateInput = { async getAmountWithExchange(transactionInput: TransactionInput) {
const exchangeRateResponse = await this.getExchangeRate({
date: transactionInput.date, date: transactionInput.date,
});
const transactionAmount = parseInt(transactionInput.amount);
const transactionData = {
date: transactionInput.date,
amount: transactionAmount,
currency: transactionInput.currency,
client_id: transactionInput.client_id,
base_currency: Currency.EUR,
base_amount:
transactionAmount / exchangeRateResponse[transactionInput.currency],
}; };
try { const commissionAmount = this.applyRules(
this.exchangeRateService.convertCurrency(exhangeRateInput).subscribe({ [this.turnoverRule, this.discountRule],
next: (exchangeRateResponse) => transactionData,
commissionAmount );
.then((commission) =>
this.transactionService.insertOne({ commissionAmount
date: transactionInput.date, .then((commission) =>
amount: parseInt(transactionInput.amount), this.transactionService.insertOne({
currency: transactionInput.currency, ...transactionData,
client_id: transactionInput.client_id, commission,
commission, }),
base_currency: Currency.EUR, )
base_amount: .catch((error) => console.log(error));
parseInt(transactionInput.amount) *
exchangeRateResponse[transactionInput.currency],
}),
)
.catch((error) => console.log(error)),
error: (error) => {
console.log(error);
},
});
} catch (error) {
console.log(error);
}
return commissionAmount; return commissionAmount;
} }
async getAmountWithoutExchange(transactionInput: TransactionInput) { async getAmountWithoutExchange(transactionInput: TransactionInput) {
const transactionData = {
date: transactionInput.date,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
base_currency: Currency.EUR,
base_amount: parseInt(transactionInput.amount),
};
const commissionAmount = await this.applyRules( const commissionAmount = await this.applyRules(
[this.turnoverRule, this.discountRule], [this.turnoverRule, this.discountRule],
transactionInput, transactionData,
); );
try { try {
this.transactionService this.transactionService
.insertOne({ .insertOne({
date: transactionInput.date, ...transactionData,
amount: parseInt(transactionInput.amount),
currency: transactionInput.currency,
client_id: transactionInput.client_id,
commission: commissionAmount, commission: commissionAmount,
base_currency: Currency.EUR,
base_amount: parseInt(transactionInput.amount),
}) })
.catch((error) => console.log(error)); .catch((error) => console.log(error));
} catch (error) { } catch (error) {

Loading…
Cancel
Save