From ac7aabb7c4252822e11be5f55728272ec8c40ab1 Mon Sep 17 00:00:00 2001 From: Inga Date: Mon, 15 Jan 2024 20:41:52 +0000 Subject: [PATCH] refactor: rules now accept partial transaction object instead of TransactionInput --- .../transaction.controller.spec.ts | 4 +- src/transaction/transaction.controller.ts | 43 +++++++++++++------ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/transaction/transaction.controller.spec.ts b/src/transaction/transaction.controller.spec.ts index 4c0c389..521fa29 100644 --- a/src/transaction/transaction.controller.spec.ts +++ b/src/transaction/transaction.controller.spec.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, }; diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index b478188..065ae35 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -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, + ) => { try { const deposit = await this.transactionService.findByClientIdWithinActualMonth( @@ -61,7 +64,7 @@ export class TransactionController { return 0; }; - turnoverRule = async (transactionInput: TransactionInput) => { + turnoverRule = async (transactionInput: Pick) => { try { const clientDeposit = await this.getClientDeposit(transactionInput); if (clientDeposit) { @@ -72,24 +75,25 @@ export class TransactionController { } }; - discountRule(transactionInput: TransactionInput) { + discountRule(transactionInput: Pick) { return transactionInput.client_id === 42 ? DiscountRuleForClientById.client_42 : false; } - defaultRule(transactionInput: TransactionInput) { + defaultRule(transactionInput: Pick) { 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, + ) => any)[], + transactionInput: Pick, ) { 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