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 { 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;
}

Loading…
Cancel
Save