Browse Source

Refactoring

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

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

@ -383,105 +383,111 @@ export class ExchangeRateDataService {
return factors; return factors;
} }
const dataSource = this.dataProviderService.getDataSourceForExchangeRates(); {
const symbol = `${currencyFrom}${currencyTo}`; const dataSource =
this.dataProviderService.getDataSourceForExchangeRates();
const marketData = await this.marketDataService.getRange({ const symbol = `${currencyFrom}${currencyTo}`;
assetProfileIdentifiers: [
{
dataSource,
symbol
}
],
dateQuery: { gte: startDate, lt: endDate }
});
if (marketData?.length > 0) { const marketData = await this.marketDataService.getRange({
for (const { date, marketPrice } of marketData) { assetProfileIdentifiers: [
factors[format(date, DATE_FORMAT)] = marketPrice; {
} dataSource,
} else { symbol
// Calculate indirectly via base currency
const marketPriceBaseCurrencyFromCurrency: {
[dateString: string]: number;
} = {};
const marketPriceBaseCurrencyToCurrency: {
[dateString: string]: number;
} = {};
try {
if (currencyFrom === DEFAULT_CURRENCY) {
for (const date of dates) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] = 1;
} }
} else { ],
const marketData = await this.marketDataService.getRange({ dateQuery: { gte: startDate, lt: endDate }
assetProfileIdentifiers: [ });
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyFrom}`
}
],
dateQuery: { gte: startDate, lt: endDate }
});
for (const { date, marketPrice } of marketData) { if (marketData?.length > 0) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] = for (const { date, marketPrice } of marketData) {
marketPrice; factors[format(date, DATE_FORMAT)] = marketPrice;
}
} }
} catch {} } else {
// Calculate indirectly via base currency
try { const marketPriceBaseCurrencyFromCurrency: {
if (currencyTo === DEFAULT_CURRENCY) { [dateString: string]: number;
for (const date of dates) { } = {};
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = 1; const marketPriceBaseCurrencyToCurrency: {
} [dateString: string]: number;
} else { } = {};
const marketData = await this.marketDataService.getRange({
assetProfileIdentifiers: [
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyTo}`
}
],
dateQuery: {
gte: startDate,
lt: endDate
}
});
for (const { date, marketPrice } of marketData) { try {
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = if (currencyFrom === DEFAULT_CURRENCY) {
marketPrice; for (const date of dates) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
1;
}
} else {
const marketData = await this.marketDataService.getRange({
assetProfileIdentifiers: [
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyFrom}`
}
],
dateQuery: { gte: startDate, lt: endDate }
});
for (const { date, marketPrice } of marketData) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
marketPrice;
}
} }
} } catch {}
} catch {}
for (const date of dates) {
try { try {
const factor = if (currencyTo === DEFAULT_CURRENCY) {
(1 / for (const date of dates) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)]) * marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = 1;
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)]; }
if (isNaN(factor)) {
throw new Error('Exchange rate is not a number');
} else { } else {
factors[format(date, DATE_FORMAT)] = factor; const marketData = await this.marketDataService.getRange({
} assetProfileIdentifiers: [
} catch { {
let errorMessage = `No exchange rate has been found for ${currencyFrom}${currencyTo} at ${format( dataSource,
date, symbol: `${DEFAULT_CURRENCY}${currencyTo}`
DATE_FORMAT }
)}. Please complement market data for ${DEFAULT_CURRENCY}${currencyFrom}`; ],
dateQuery: {
if (DEFAULT_CURRENCY !== currencyTo) { gte: startDate,
errorMessage = `${errorMessage} and ${DEFAULT_CURRENCY}${currencyTo}`; lt: endDate
}
});
for (const { date, marketPrice } of marketData) {
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] =
marketPrice;
}
} }
} catch {}
Logger.error(`${errorMessage}.`, 'ExchangeRateDataService'); for (const date of dates) {
try {
const factor =
(1 /
marketPriceBaseCurrencyFromCurrency[
format(date, DATE_FORMAT)
]) *
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)];
if (isNaN(factor)) {
throw new Error('Exchange rate is not a number');
} else {
factors[format(date, DATE_FORMAT)] = factor;
}
} catch {
let errorMessage = `No exchange rate has been found for ${currencyFrom}${currencyTo} at ${format(
date,
DATE_FORMAT
)}. Please complement market data for ${DEFAULT_CURRENCY}${currencyFrom}`;
if (DEFAULT_CURRENCY !== currencyTo) {
errorMessage = `${errorMessage} and ${DEFAULT_CURRENCY}${currencyTo}`;
}
Logger.error(`${errorMessage}.`, 'ExchangeRateDataService');
}
} }
} }
} }

Loading…
Cancel
Save