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. 97
      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,
}; });
try { const transactionAmount = parseInt(transactionInput.amount);
this.exchangeRateService.convertCurrency(exhangeRateInput).subscribe({ const transactionData = {
next: (exchangeRateResponse) =>
commissionAmount
.then((commission) =>
this.transactionService.insertOne({
date: transactionInput.date, date: transactionInput.date,
amount: parseInt(transactionInput.amount), amount: transactionAmount,
currency: transactionInput.currency, currency: transactionInput.currency,
client_id: transactionInput.client_id, client_id: transactionInput.client_id,
commission,
base_currency: Currency.EUR, base_currency: Currency.EUR,
base_amount: base_amount:
parseInt(transactionInput.amount) * transactionAmount / exchangeRateResponse[transactionInput.currency],
exchangeRateResponse[transactionInput.currency], };
const commissionAmount = this.applyRules(
[this.turnoverRule, this.discountRule],
transactionData,
);
commissionAmount
.then((commission) =>
this.transactionService.insertOne({
...transactionData,
commission,
}), }),
) )
.catch((error) => console.log(error)), .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