Browse Source

Add logic to detect when converting between a derived currency and its root currency, and apply the fixed mathematical factor directly (0.01 for GBp→GBP, 100 for GBP→GBp) instead of trying to fetch exchange rates.

pull/5961/head
Sven Günther 1 month ago
parent
commit
5e7dd96510
  1. 58
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

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

@ -270,6 +270,14 @@ export class ExchangeRateDataService {
if (aFromCurrency === aToCurrency) { if (aFromCurrency === aToCurrency) {
factor = 1; factor = 1;
} else {
const derivedCurrencyFactor = this.getDerivedCurrencyFactor(
aFromCurrency,
aToCurrency
);
if (derivedCurrencyFactor !== null) {
factor = derivedCurrencyFactor;
} else { } else {
const dataSource = const dataSource =
this.dataProviderService.getDataSourceForExchangeRates(); this.dataProviderService.getDataSourceForExchangeRates();
@ -323,6 +331,7 @@ export class ExchangeRateDataService {
marketPriceBaseCurrencyToCurrency; marketPriceBaseCurrencyToCurrency;
} }
} }
}
if (isNumber(factor) && !isNaN(factor)) { if (isNumber(factor) && !isNaN(factor)) {
return factor * aValue; return factor * aValue;
@ -357,7 +366,25 @@ export class ExchangeRateDataService {
for (const date of dates) { for (const date of dates) {
factors[format(date, DATE_FORMAT)] = 1; factors[format(date, DATE_FORMAT)] = 1;
} }
} else { return factors;
}
// Check if this is a conversion between a derived currency and its root currency
const derivedCurrencyFactor = this.getDerivedCurrencyFactor(
currencyFrom,
currencyTo
);
if (derivedCurrencyFactor !== null) {
// Use the fixed mathematical factor for derived currencies
for (const date of dates) {
factors[format(date, DATE_FORMAT)] = derivedCurrencyFactor;
}
return factors;
}
// Continue with standard exchange rate logic
{
const dataSource = const dataSource =
this.dataProviderService.getDataSourceForExchangeRates(); this.dataProviderService.getDataSourceForExchangeRates();
const symbol = `${currencyFrom}${currencyTo}`; const symbol = `${currencyFrom}${currencyTo}`;
@ -469,6 +496,35 @@ export class ExchangeRateDataService {
return factors; return factors;
} }
private getDerivedCurrencyFactor(
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
);
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[]> {
let currencies: string[] = [DEFAULT_CURRENCY]; let currencies: string[] = [DEFAULT_CURRENCY];

Loading…
Cancel
Save