From 1d0ba5fe4b01a36b99c36bf53fff2ee4f0215cd7 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Tue, 30 May 2023 21:05:53 +0200 Subject: [PATCH] Bugfix/fix indirect calculation in exchange rate service for specific date (#2026) * Fix indirect calculation * Update changelog --- CHANGELOG.md | 4 ++ .../exchange-rate-data.service.ts | 54 ++++++++++++------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8abca7f5..7e456c265 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improved the language localization for German (`de`) +### Fixed + +- Fixed the exchange rate service for a specific date (indirect calculation via base currency) used in activities with a manual currency + ## 1.274.0 - 2023-05-29 ### Added diff --git a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts index e39e926a9..448933b42 100644 --- a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts +++ b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts @@ -186,28 +186,42 @@ export class ExchangeRateDataService { factor = marketData?.marketPrice; } else { // Calculate indirectly via base currency + + let marketPriceBaseCurrencyFromCurrency: number; + let marketPriceBaseCurrencyToCurrency: number; + try { - const [ - { marketPrice: marketPriceBaseCurrencyFromCurrency }, - { marketPrice: marketPriceBaseCurrencyToCurrency } - ] = await Promise.all([ - this.marketDataService.get({ - dataSource, - date: aDate, - symbol: `${this.baseCurrency}${aFromCurrency}` - }), - this.marketDataService.get({ - dataSource, - date: aDate, - symbol: `${this.baseCurrency}${aToCurrency}` - }) - ]); - - // Calculate the opposite direction - factor = - (1 / marketPriceBaseCurrencyFromCurrency) * - marketPriceBaseCurrencyToCurrency; + if (this.baseCurrency === aFromCurrency) { + marketPriceBaseCurrencyFromCurrency = 1; + } else { + marketPriceBaseCurrencyFromCurrency = ( + await this.marketDataService.get({ + dataSource, + date: aDate, + symbol: `${this.baseCurrency}${aFromCurrency}` + }) + )?.marketPrice; + } } catch {} + + try { + if (this.baseCurrency === aToCurrency) { + marketPriceBaseCurrencyToCurrency = 1; + } else { + marketPriceBaseCurrencyToCurrency = ( + await this.marketDataService.get({ + dataSource, + date: aDate, + symbol: `${this.baseCurrency}${aToCurrency}` + }) + )?.marketPrice; + } + } catch {} + + // Calculate the opposite direction + factor = + (1 / marketPriceBaseCurrencyFromCurrency) * + marketPriceBaseCurrencyToCurrency; } }