|
|
@ -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,11 +129,9 @@ 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 |
|
|
|
|
|
|
|
.then((commission) => |
|
|
|
|
|
|
|
this.transactionService.insertOne({ |
|
|
|
this.transactionService.insertOne({ |
|
|
|
date: transactionInput.date, |
|
|
|
date: transactionInput.date, |
|
|
|
amount: parseInt(transactionInput.amount), |
|
|
|
amount: parseInt(transactionInput.amount), |
|
|
@ -131,15 +143,9 @@ export class TransactionController { |
|
|
|
parseInt(transactionInput.amount) * |
|
|
|
parseInt(transactionInput.amount) * |
|
|
|
exchangeRateResponse[transactionInput.currency], |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|