Browse Source

Bugfix/exchange rate calculation when converting derived currencies (#5961)

* Fix exchange rate calculation when converting derived currencies

* Update changelog
pull/5987/head
Sven Günther 6 days ago
committed by GitHub
parent
commit
e1e455da86
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      CHANGELOG.md
  2. 38
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

6
CHANGELOG.md

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Fixed
- Fixed an issue with the exchange rate calculation when converting between derived currencies and their root currencies
## 2.219.0 - 2025-11-23
### Added

38
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -30,6 +30,7 @@ import ms from 'ms';
export class ExchangeRateDataService {
private currencies: string[] = [];
private currencyPairs: DataGatheringItem[] = [];
private derivedCurrencyFactors: { [currencyPair: string]: number } = {};
private exchangeRates: { [currencyPair: string]: number } = {};
public constructor(
@ -135,8 +136,14 @@ export class ExchangeRateDataService {
public async initialize() {
this.currencies = await this.prepareCurrencies();
this.currencyPairs = [];
this.derivedCurrencyFactors = {};
this.exchangeRates = {};
for (const { currency, factor, rootCurrency } of DERIVED_CURRENCIES) {
this.derivedCurrencyFactors[`${currency}${rootCurrency}`] = 1 / factor;
this.derivedCurrencyFactors[`${rootCurrency}${currency}`] = factor;
}
for (const {
currency1,
currency2,
@ -266,10 +273,14 @@ export class ExchangeRateDataService {
return this.toCurrency(aValue, aFromCurrency, aToCurrency);
}
const derivedCurrencyFactor =
this.derivedCurrencyFactors[`${aFromCurrency}${aToCurrency}`];
let factor: number;
if (aFromCurrency === aToCurrency) {
factor = 1;
} else if (derivedCurrencyFactor) {
factor = derivedCurrencyFactor;
} else {
const dataSource =
this.dataProviderService.getDataSourceForExchangeRates();
@ -357,9 +368,22 @@ export class ExchangeRateDataService {
for (const date of dates) {
factors[format(date, DATE_FORMAT)] = 1;
}
} else {
const dataSource =
this.dataProviderService.getDataSourceForExchangeRates();
return factors;
}
const derivedCurrencyFactor =
this.derivedCurrencyFactors[`${currencyFrom}${currencyTo}`];
if (derivedCurrencyFactor) {
for (const date of dates) {
factors[format(date, DATE_FORMAT)] = derivedCurrencyFactor;
}
return factors;
}
const dataSource = this.dataProviderService.getDataSourceForExchangeRates();
const symbol = `${currencyFrom}${currencyTo}`;
const marketData = await this.marketDataService.getRange({
@ -389,8 +413,7 @@ export class ExchangeRateDataService {
try {
if (currencyFrom === DEFAULT_CURRENCY) {
for (const date of dates) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
1;
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] = 1;
}
} else {
const marketData = await this.marketDataService.getRange({
@ -440,9 +463,7 @@ export class ExchangeRateDataService {
try {
const factor =
(1 /
marketPriceBaseCurrencyFromCurrency[
format(date, DATE_FORMAT)
]) *
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)]) *
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)];
if (isNaN(factor)) {
@ -464,7 +485,6 @@ export class ExchangeRateDataService {
}
}
}
}
return factors;
}

Loading…
Cancel
Save