diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index 0c50631..b478188 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -1,4 +1,5 @@ import { Controller, Post, UsePipes, Body } from '@nestjs/common'; +import { ExchangeRateInput } from 'src/exchange-rate/exchange-rate.dto'; import { ExchangeRateService } from 'src/exchange-rate/exchange-rate.service'; import { TransactionService } from './transaction.service'; import { transactionBodySchema } from './transaction.validation'; @@ -105,6 +106,19 @@ export class TransactionController { : this.defaultRule(transactionInput); } + getExchangeRate(exchangeRateInput: ExchangeRateInput) { + return new Promise((resolve, reject) => { + try { + this.exchangeRateService.convertCurrency(exchangeRateInput).subscribe({ + next: resolve, + error: reject, + }); + } catch (error) { + reject(error); + } + }); + } + getAmountWithExchange(transactionInput: TransactionInput) { const commissionAmount = this.applyRules( [this.turnoverRule, this.discountRule], @@ -115,31 +129,23 @@ export class TransactionController { date: transactionInput.date, }; - try { - this.exchangeRateService.convertCurrency(exhangeRateInput).subscribe({ - next: (exchangeRateResponse) => - commissionAmount - .then((commission) => - this.transactionService.insertOne({ - date: transactionInput.date, - amount: parseInt(transactionInput.amount), - currency: transactionInput.currency, - client_id: transactionInput.client_id, - commission, - base_currency: Currency.EUR, - base_amount: - parseInt(transactionInput.amount) * - exchangeRateResponse[transactionInput.currency], - }), - ) - .catch((error) => console.log(error)), - error: (error) => { - console.log(error); - }, - }); - } catch (error) { - console.log(error); - } + this.getExchangeRate(exhangeRateInput) + .then((exchangeRateResponse) => + commissionAmount.then((commission) => + this.transactionService.insertOne({ + date: transactionInput.date, + amount: parseInt(transactionInput.amount), + currency: transactionInput.currency, + client_id: transactionInput.client_id, + commission, + base_currency: Currency.EUR, + base_amount: + parseInt(transactionInput.amount) * + exchangeRateResponse[transactionInput.currency], + }), + ), + ) + .catch((error) => console.log(error)); return commissionAmount; }