From 5138c12d9c75f8820f0ac5ae11d5c62d89054825 Mon Sep 17 00:00:00 2001 From: Inga Date: Mon, 15 Jan 2024 22:28:59 +0000 Subject: [PATCH] refactor: unified getAmountWithExchange and getAmountWithoutExchange methods (closes #10) --- src/transaction/transaction.controller.ts | 66 ++++++++--------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index e044b34..9bbd985 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -33,10 +33,7 @@ export class TransactionController { @Body() transactionInput: TransactionInput, ): Promise { return JSON.stringify({ - amount: - transactionInput.currency !== Currency.EUR - ? (await this.getAmountWithExchange(transactionInput)).toFixed(2) - : (await this.getAmountWithoutExchange(transactionInput)).toFixed(2), + amount: (await this.getCommissionAmount(transactionInput)).toFixed(2), currency: Currency.EUR, }); } @@ -97,7 +94,7 @@ export class TransactionController { ); } - getExchangeRate(exchangeRateInput: ExchangeRateInput) { + getExchangeRateResponse(exchangeRateInput: ExchangeRateInput) { return new Promise((resolve, reject) => { try { this.exchangeRateService.convertCurrency(exchangeRateInput).subscribe({ @@ -110,11 +107,21 @@ export class TransactionController { }); } - async getAmountWithExchange(transactionInput: TransactionInput) { - const exchangeRateResponse = await this.getExchangeRate({ + async getExchangeRateIfNeeded(transactionInput: TransactionInput) { + if (transactionInput.currency === Currency.EUR) { + return 1; + } + + const exchangeRateResponse = await this.getExchangeRateResponse({ date: transactionInput.date, }); + return exchangeRateResponse[transactionInput.currency]; + } + + async getCommissionAmount(transactionInput: TransactionInput) { + const exchangeRate = await this.getExchangeRateIfNeeded(transactionInput); + const transactionAmount = parseInt(transactionInput.amount); const transactionData = { date: transactionInput.date, @@ -122,47 +129,18 @@ export class TransactionController { currency: transactionInput.currency, client_id: transactionInput.client_id, base_currency: Currency.EUR, - base_amount: - transactionAmount / exchangeRateResponse[transactionInput.currency], + base_amount: transactionAmount / exchangeRate, }; - const commissionAmount = this.applyRules(transactionData); + const commission = await this.applyRules(transactionData); - commissionAmount - .then((commission) => - this.transactionService.insertOne({ - ...transactionData, - commission, - }), - ) + this.transactionService + .insertOne({ + ...transactionData, + commission, + }) .catch((error) => console.log(error)); - return commissionAmount; - } - - 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(transactionData); - try { - this.transactionService - .insertOne({ - ...transactionData, - commission: commissionAmount, - }) - .catch((error) => console.log(error)); - } catch (error) { - console.log(error); - throw error; - } - - return commissionAmount; + return commission; } }