refactor: implemented async getExchangeRate method

pull/8/head
Inga 🏳‍🌈 10 months ago
parent a228668915
commit 279e5230e5
  1. 56
      src/transaction/transaction.controller.ts

@ -1,4 +1,5 @@
import { Controller, Post, UsePipes, Body } from '@nestjs/common'; 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 { 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';
@ -105,6 +106,19 @@ export class TransactionController {
: this.defaultRule(transactionInput); : 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) { getAmountWithExchange(transactionInput: TransactionInput) {
const commissionAmount = this.applyRules( const commissionAmount = this.applyRules(
[this.turnoverRule, this.discountRule], [this.turnoverRule, this.discountRule],
@ -115,31 +129,23 @@ export class TransactionController {
date: transactionInput.date, date: transactionInput.date,
}; };
try { this.getExchangeRate(exhangeRateInput)
this.exchangeRateService.convertCurrency(exhangeRateInput).subscribe({ .then((exchangeRateResponse) =>
next: (exchangeRateResponse) => commissionAmount.then((commission) =>
commissionAmount this.transactionService.insertOne({
.then((commission) => date: transactionInput.date,
this.transactionService.insertOne({ amount: parseInt(transactionInput.amount),
date: transactionInput.date, currency: transactionInput.currency,
amount: parseInt(transactionInput.amount), client_id: transactionInput.client_id,
currency: transactionInput.currency, commission,
client_id: transactionInput.client_id, base_currency: Currency.EUR,
commission, base_amount:
base_currency: Currency.EUR, parseInt(transactionInput.amount) *
base_amount: exchangeRateResponse[transactionInput.currency],
parseInt(transactionInput.amount) * }),
exchangeRateResponse[transactionInput.currency], ),
}), )
) .catch((error) => console.log(error));
.catch((error) => console.log(error)),
error: (error) => {
console.log(error);
},
});
} catch (error) {
console.log(error);
}
return commissionAmount; return commissionAmount;
} }

Loading…
Cancel
Save