Browse Source

Refactoring

pull/5961/head
Thomas Kaul 1 month ago
parent
commit
17483ca5e4
  1. 55
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

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

@ -30,7 +30,7 @@ import ms from 'ms';
export class ExchangeRateDataService {
private currencies: string[] = [];
private currencyPairs: DataGatheringItem[] = [];
private derivedCurrencyFactors = new Map<string, number>();
private derivedCurrencyFactors: { [currencyPair: string]: number } = {};
private exchangeRates: { [currencyPair: string]: number } = {};
public constructor(
@ -136,13 +136,12 @@ export class ExchangeRateDataService {
public async initialize() {
this.currencies = await this.prepareCurrencies();
this.currencyPairs = [];
this.derivedCurrencyFactors = {};
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);
this.derivedCurrencyFactors[`${currency}${rootCurrency}`] = 1 / factor;
this.derivedCurrencyFactors[`${rootCurrency}${currency}`] = factor;
}
for (const {
@ -274,17 +273,13 @@ export class ExchangeRateDataService {
return this.toCurrency(aValue, aFromCurrency, aToCurrency);
}
const derivedCurrencyFactor =
this.derivedCurrencyFactors[`${aFromCurrency}${aToCurrency}`];
let factor: number;
if (aFromCurrency === aToCurrency) {
factor = 1;
} else {
const derivedCurrencyFactor = this.getDerivedCurrencyFactor(
aFromCurrency,
aToCurrency
);
if (derivedCurrencyFactor !== null) {
} else if (derivedCurrencyFactor) {
factor = derivedCurrencyFactor;
} else {
const dataSource =
@ -339,7 +334,6 @@ export class ExchangeRateDataService {
marketPriceBaseCurrencyToCurrency;
}
}
}
if (isNumber(factor) && !isNaN(factor)) {
return factor * aValue;
@ -374,27 +368,22 @@ export class ExchangeRateDataService {
for (const date of dates) {
factors[format(date, DATE_FORMAT)] = 1;
}
return factors;
}
// Check if this is a conversion between a derived currency and its root currency
const derivedCurrencyFactor = this.getDerivedCurrencyFactor(
currencyFrom,
currencyTo
);
const derivedCurrencyFactor =
this.derivedCurrencyFactors[`${currencyFrom}${currencyTo}`];
if (derivedCurrencyFactor !== null) {
// Use the fixed mathematical factor for derived currencies
if (derivedCurrencyFactor) {
for (const date of dates) {
factors[format(date, DATE_FORMAT)] = derivedCurrencyFactor;
}
return factors;
}
// Continue with standard exchange rate logic
{
const dataSource =
this.dataProviderService.getDataSourceForExchangeRates();
const dataSource = this.dataProviderService.getDataSourceForExchangeRates();
const symbol = `${currencyFrom}${currencyTo}`;
const marketData = await this.marketDataService.getRange({
@ -424,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({
@ -475,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)) {
@ -499,21 +485,10 @@ export class ExchangeRateDataService {
}
}
}
}
return factors;
}
private getDerivedCurrencyFactor(
currencyFrom: string,
currencyTo: string
): number | null {
const factor = this.derivedCurrencyFactors.get(
`${currencyFrom}${currencyTo}`
);
return factor ?? null;
}
private async prepareCurrencies(): Promise<string[]> {
let currencies: string[] = [DEFAULT_CURRENCY];

Loading…
Cancel
Save