refactor: implemented async getExchangeRate method

pull/8/head
Inga 🏳‍🌈 10 months ago
parent a228668915
commit 279e5230e5
  1. 32
      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,11 +129,9 @@ export class TransactionController {
date: transactionInput.date,
};
try {
this.exchangeRateService.convertCurrency(exhangeRateInput).subscribe({
next: (exchangeRateResponse) =>
commissionAmount
.then((commission) =>
this.getExchangeRate(exhangeRateInput)
.then((exchangeRateResponse) =>
commissionAmount.then((commission) =>
this.transactionService.insertOne({
date: transactionInput.date,
amount: parseInt(transactionInput.amount),
@ -131,15 +143,9 @@ export class TransactionController {
parseInt(transactionInput.amount) *
exchangeRateResponse[transactionInput.currency],
}),
),
)
.catch((error) => console.log(error)),
error: (error) => {
console.log(error);
},
});
} catch (error) {
console.log(error);
}
.catch((error) => console.log(error));
return commissionAmount;
}

Loading…
Cancel
Save