From 296fdb49457f55ebb0596fd5bf221d3e15e950d5 Mon Sep 17 00:00:00 2001 From: Inga Date: Mon, 15 Jan 2024 20:51:37 +0000 Subject: [PATCH] fix: use base_amount to calculate commission --- .../transaction.controller.spec.ts | 2 +- src/transaction/transaction.controller.ts | 48 +++++++++---------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/transaction/transaction.controller.spec.ts b/src/transaction/transaction.controller.spec.ts index 521fa29..a8bf324 100644 --- a/src/transaction/transaction.controller.spec.ts +++ b/src/transaction/transaction.controller.spec.ts @@ -72,7 +72,7 @@ describe('TransactionController Unit Tests', () => { it('calling applyRules method minimum result of applied rules should be got', async () => { const mockTransactionInput = { date: '2021-01-05', - amount: 1000.0, + base_amount: 1000.0, currency: 'EUR', client_id: 1, }; diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index 3fd1bc1..5cdd52a 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -14,7 +14,7 @@ import { } from './transaction.dto'; import { Transaction } from './transaction.entity'; -type TransactionRuleData = Pick; +type TransactionRuleData = Pick; @Controller('transaction') export class TransactionController { @@ -83,7 +83,8 @@ export class TransactionController { defaultRule(transactionInput: TransactionRuleData) { const commissionAmount = - (transactionInput.amount * DefaultCommissionPercentage.percentage) / 100; + (transactionInput.base_amount * DefaultCommissionPercentage.percentage) / + 100; return commissionAmount < DefaultCommissionAmount.amount ? DefaultCommissionAmount.amount : commissionAmount; @@ -123,12 +124,20 @@ export class TransactionController { }); } - getAmountWithExchange(transactionInput: TransactionInput) { + async getAmountWithExchange(transactionInput: TransactionInput) { + const exchangeRateResponse = await this.getExchangeRate({ + date: transactionInput.date, + }); + + const transactionAmount = parseInt(transactionInput.amount); const transactionData = { date: transactionInput.date, - amount: parseInt(transactionInput.amount), + amount: transactionAmount, currency: transactionInput.currency, client_id: transactionInput.client_id, + base_currency: Currency.EUR, + base_amount: + transactionAmount * exchangeRateResponse[transactionInput.currency], }; const commissionAmount = this.applyRules( @@ -136,22 +145,12 @@ export class TransactionController { transactionData, ); - const exhangeRateInput = { - date: transactionInput.date, - }; - - this.getExchangeRate(exhangeRateInput) - .then((exchangeRateResponse) => - commissionAmount.then((commission) => - this.transactionService.insertOne({ - ...transactionData, - commission, - base_currency: Currency.EUR, - base_amount: - parseInt(transactionInput.amount) * - exchangeRateResponse[transactionInput.currency], - }), - ), + commissionAmount + .then((commission) => + this.transactionService.insertOne({ + ...transactionData, + commission, + }), ) .catch((error) => console.log(error)); @@ -164,6 +163,8 @@ export class TransactionController { 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( @@ -173,13 +174,8 @@ export class TransactionController { try { this.transactionService .insertOne({ - date: transactionInput.date, - amount: parseInt(transactionInput.amount), - currency: transactionInput.currency, - client_id: transactionInput.client_id, + ...transactionData, commission: commissionAmount, - base_currency: Currency.EUR, - base_amount: parseInt(transactionInput.amount), }) .catch((error) => console.log(error)); } catch (error) {