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/),
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
### 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 {
private currencies: string[] = [];
private currencyPairs: DataGatheringItem[] = [];
private derivedCurrencyFactors = new Map<string, number>();
private exchangeRates: { [currencyPair: string]: number } = {};
public constructor(
@ -136,6 +137,13 @@ export class ExchangeRateDataService {
this.currencies = await this.prepareCurrencies();
this.currencyPairs = [];
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 {
currency1,
@ -500,29 +508,10 @@ export class ExchangeRateDataService {
currencyFrom: string,
currencyTo: string
): number | null {
// Check if currencyFrom is a derived currency of currencyTo
const derivedFrom = DERIVED_CURRENCIES.find(
({ currency, rootCurrency }) =>
currency === currencyFrom && rootCurrency === currencyTo
const factor = this.derivedCurrencyFactors.get(
`${currencyFrom}${currencyTo}`
);
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;
return factor ?? null;
}
private async prepareCurrencies(): Promise<string[]> {

Loading…
Cancel
Save