Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
fix(converter): fix diff calculating
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Mar 3, 2019
1 parent 7c5fd2c commit 9a68a41
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions back/src/money/application/CurrencyConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class CurrencyConverter {

const rate = await this.getExchangeRate(from, to, normalizedDate)
.catch(tryTo(this.exchangeRateRepo.findClosest(from, to, normalizedDate)))
.catch(() => this.fetchExchangeRate(from, to, new Date()))
.catch(() => this.getExchangeRate(from, to, new Date()))
.catch(tryTo(this.exchangeRateRepo.findLast(from, to)))

return Math.round(amount * rate)
Expand Down Expand Up @@ -79,7 +79,8 @@ export class CurrencyConverter {
const MIN_DAY_FOR_HISTORY_TRANSACTION = 2

const rateIsOld =
differenceInDays(when, new Date()) > MIN_DAY_FOR_HISTORY_TRANSACTION
Math.abs(differenceInDays(when, new Date())) >
MIN_DAY_FOR_HISTORY_TRANSACTION

if (rateIsOld) {
return this.exchangeRateApi.getHistoryExchangeRate(from, to, when)
Expand Down
1 change: 1 addition & 0 deletions back/src/money/domain/ExchangeRateRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ExchangeRateRepo {
const rateToDiff = (rate: Option<ExchangeRate>) =>
rate
.map(r => differenceInMilliseconds(r.collectAt, forWhen))
.map(Math.abs)
.getOrElse(Infinity)

const beforeDiff = rateToDiff(before)
Expand Down

0 comments on commit 9a68a41

Please sign in to comment.