Browse Source

Clean up

pull/5961/head
Thomas Kaul 1 month ago
parent
commit
bbff0bdfc9
  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,111 +383,105 @@ export class ExchangeRateDataService {
return factors; return factors;
} }
{ const dataSource = this.dataProviderService.getDataSourceForExchangeRates();
const dataSource = const symbol = `${currencyFrom}${currencyTo}`;
this.dataProviderService.getDataSourceForExchangeRates();
const symbol = `${currencyFrom}${currencyTo}`; const marketData = await this.marketDataService.getRange({
assetProfileIdentifiers: [
{
dataSource,
symbol
}
],
dateQuery: { gte: startDate, lt: endDate }
});
const marketData = await this.marketDataService.getRange({ if (marketData?.length > 0) {
assetProfileIdentifiers: [ for (const { date, marketPrice } of marketData) {
{ factors[format(date, DATE_FORMAT)] = marketPrice;
dataSource, }
symbol } else {
// 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 {
dateQuery: { gte: startDate, lt: endDate } const marketData = await this.marketDataService.getRange({
}); assetProfileIdentifiers: [
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyFrom}`
}
],
dateQuery: { gte: startDate, lt: endDate }
});
if (marketData?.length > 0) { for (const { date, marketPrice } of marketData) {
for (const { date, marketPrice } of marketData) { marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
factors[format(date, DATE_FORMAT)] = marketPrice; marketPrice;
}
} }
} else { } catch {}
// Calculate indirectly via base currency
const marketPriceBaseCurrencyFromCurrency: {
[dateString: string]: number;
} = {};
const marketPriceBaseCurrencyToCurrency: {
[dateString: string]: number;
} = {};
try { try {
if (currencyFrom === DEFAULT_CURRENCY) { if (currencyTo === DEFAULT_CURRENCY) {
for (const date of dates) { for (const date of dates) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] = marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = 1;
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 {} } else {
const marketData = await this.marketDataService.getRange({
try { assetProfileIdentifiers: [
if (currencyTo === DEFAULT_CURRENCY) { {
for (const date of dates) { dataSource,
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = 1; symbol: `${DEFAULT_CURRENCY}${currencyTo}`
}
} else {
const marketData = await this.marketDataService.getRange({
assetProfileIdentifiers: [
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyTo}`
}
],
dateQuery: {
gte: startDate,
lt: endDate
} }
}); ],
dateQuery: {
for (const { date, marketPrice } of marketData) { gte: startDate,
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = lt: endDate
marketPrice;
} }
});
for (const { date, marketPrice } of marketData) {
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] =
marketPrice;
} }
} catch {} }
} catch {}
for (const date of dates) { for (const date of dates) {
try { try {
const factor = const factor =
(1 / (1 /
marketPriceBaseCurrencyFromCurrency[ marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)]) *
format(date, DATE_FORMAT) marketPriceBaseCurrencyToCurrency[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'); 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