Browse Source

use map for derivedCurrencies to improve efficiency

pull/5961/head
Sven Günther 1 month ago
parent
commit
21ff7c20ed
  1. 6
      CHANGELOG.md
  2. 33
      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/), 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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Fixed
- Fixed exchange rate conversion errors when converting between derived currencies (e.g., GBp) and their root currencies (e.g., GBP) by using fixed mathematical factors instead of fetching from external data sources
## 2.218.0 - 2025-11-20 ## 2.218.0 - 2025-11-20
### Added ### Added

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

@ -30,6 +30,7 @@ import ms from 'ms';
export class ExchangeRateDataService { export class ExchangeRateDataService {
private currencies: string[] = []; private currencies: string[] = [];
private currencyPairs: DataGatheringItem[] = []; private currencyPairs: DataGatheringItem[] = [];
private derivedCurrencyFactors = new Map<string, number>();
private exchangeRates: { [currencyPair: string]: number } = {}; private exchangeRates: { [currencyPair: string]: number } = {};
public constructor( public constructor(
@ -136,6 +137,13 @@ export class ExchangeRateDataService {
this.currencies = await this.prepareCurrencies(); this.currencies = await this.prepareCurrencies();
this.currencyPairs = []; this.currencyPairs = [];
this.exchangeRates = {}; this.exchangeRates = {};
this.derivedCurrencyFactors = new Map();
// Initialize derived currencies factor map in both directions
for (const { currency, factor, rootCurrency } of DERIVED_CURRENCIES) {
this.derivedCurrencyFactors.set(`${currency}${rootCurrency}`, 1 / factor);
this.derivedCurrencyFactors.set(`${rootCurrency}${currency}`, factor);
}
for (const { for (const {
currency1, currency1,
@ -500,29 +508,10 @@ export class ExchangeRateDataService {
currencyFrom: string, currencyFrom: string,
currencyTo: string currencyTo: string
): number | null { ): number | null {
// Check if currencyFrom is a derived currency of currencyTo const factor = this.derivedCurrencyFactors.get(
const derivedFrom = DERIVED_CURRENCIES.find( `${currencyFrom}${currencyTo}`
({ currency, rootCurrency }) =>
currency === currencyFrom && rootCurrency === currencyTo
); );
return factor ?? null;
if (derivedFrom) {
// e.g., GBp → GBP: factor = 1/100 = 0.01
return 1 / derivedFrom.factor;
}
// Check if currencyTo is a derived currency of currencyFrom
const derivedTo = DERIVED_CURRENCIES.find(
({ currency, rootCurrency }) =>
currency === currencyTo && rootCurrency === currencyFrom
);
if (derivedTo) {
// e.g., GBP → GBp: factor = 100
return derivedTo.factor;
}
return null;
} }
private async prepareCurrencies(): Promise<string[]> { private async prepareCurrencies(): Promise<string[]> {

Loading…
Cancel
Save