Browse Source

Refactoring

pull/2875/head
Thomas Kaul 2 years ago
parent
commit
90f52bc027
  1. 11
      apps/api/src/app/benchmark/benchmark.service.ts
  2. 12
      apps/api/src/app/portfolio/portfolio-calculator.ts
  3. 20
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

11
apps/api/src/app/benchmark/benchmark.service.ts

@ -243,7 +243,7 @@ export class BenchmarkService {
}); });
const exchangeRateAtStartDate = const exchangeRateAtStartDate =
exchangeRates[currentSymbolItem.currency]?.[ exchangeRates[`${currentSymbolItem.currency}${userCurrency}`]?.[
format(startDate, DATE_FORMAT) format(startDate, DATE_FORMAT)
]; ];
@ -275,7 +275,9 @@ export class BenchmarkService {
} }
const exchangeRate = const exchangeRate =
exchangeRates[format(marketDataItem.date, DATE_FORMAT)]; exchangeRates[`${currentSymbolItem.currency}${userCurrency}`]?.[
format(marketDataItem.date, DATE_FORMAT)
];
const exchangeRateFactor = const exchangeRateFactor =
isNumber(exchangeRateAtStartDate) && isNumber(exchangeRate) isNumber(exchangeRateAtStartDate) && isNumber(exchangeRate)
@ -300,7 +302,10 @@ export class BenchmarkService {
); );
if (currentSymbolItem?.marketPrice && !includesToday) { if (currentSymbolItem?.marketPrice && !includesToday) {
const exchangeRate = exchangeRates[format(new Date(), DATE_FORMAT)]; const exchangeRate =
exchangeRates[`${currentSymbolItem.currency}${userCurrency}`]?.[
format(new Date(), DATE_FORMAT)
];
const exchangeRateFactor = const exchangeRateFactor =
isNumber(exchangeRateAtStartDate) && isNumber(exchangeRate) isNumber(exchangeRateAtStartDate) && isNumber(exchangeRate)

12
apps/api/src/app/portfolio/portfolio-calculator.ts

@ -298,7 +298,8 @@ export class PortfolioCalculator {
start, start,
step, step,
symbol, symbol,
exchangeRates: exchangeRatesByCurrency[currencies[symbol]], exchangeRates:
exchangeRatesByCurrency[`${currencies[symbol]}${this.currency}`],
isChartMode: true isChartMode: true
}); });
@ -565,7 +566,11 @@ export class PortfolioCalculator {
for (const item of lastTransactionPoint.items) { for (const item of lastTransactionPoint.items) {
const marketPriceInBaseCurrency = marketSymbolMap[endDateString]?.[ const marketPriceInBaseCurrency = marketSymbolMap[endDateString]?.[
item.symbol item.symbol
]?.mul(exchangeRatesByCurrency[item.currency]?.[endDateString]); ]?.mul(
exchangeRatesByCurrency[`${item.currency}${this.currency}`]?.[
endDateString
]
);
const { const {
grossPerformance, grossPerformance,
@ -585,7 +590,8 @@ export class PortfolioCalculator {
end, end,
marketSymbolMap, marketSymbolMap,
start, start,
exchangeRates: exchangeRatesByCurrency[item.currency], exchangeRates:
exchangeRatesByCurrency[`${item.currency}${this.currency}`],
symbol: item.symbol symbol: item.symbol
}); });

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

@ -64,7 +64,8 @@ export class ExchangeRateDataService {
} = {}; } = {};
for (let currency of currencies) { for (let currency of currencies) {
exchangeRatesByCurrency[currency] = await this.getExchangeRates({ exchangeRatesByCurrency[`${currency}${targetCurrency}`] =
await this.getExchangeRates({
startDate, startDate,
currencyFrom: currency, currencyFrom: currency,
currencyTo: targetCurrency currencyTo: targetCurrency
@ -82,17 +83,25 @@ export class ExchangeRateDataService {
let dateString = format(date, DATE_FORMAT); let dateString = format(date, DATE_FORMAT);
// Check if the exchange rate for the current date is missing // Check if the exchange rate for the current date is missing
if (isNaN(exchangeRatesByCurrency[currency][dateString])) { if (
isNaN(
exchangeRatesByCurrency[`${currency}${targetCurrency}`][dateString]
)
) {
// If missing, fill with the previous exchange rate // If missing, fill with the previous exchange rate
exchangeRatesByCurrency[currency][dateString] = previousExchangeRate; exchangeRatesByCurrency[`${currency}${targetCurrency}`][dateString] =
previousExchangeRate;
if (currency === DEFAULT_CURRENCY) {
Logger.error( Logger.error(
`No exchange rate has been found for ${DEFAULT_CURRENCY}${currency} at ${dateString}`, `No exchange rate has been found for ${currency}${targetCurrency} at ${dateString}`,
'ExchangeRateDataService' 'ExchangeRateDataService'
); );
}
} else { } else {
// If available, update the previous exchange rate // If available, update the previous exchange rate
previousExchangeRate = exchangeRatesByCurrency[currency][dateString]; previousExchangeRate =
exchangeRatesByCurrency[`${currency}${targetCurrency}`][dateString];
} }
} }
} }
@ -249,6 +258,7 @@ export class ExchangeRateDataService {
`No exchange rate has been found for ${aFromCurrency}${aToCurrency}`, `No exchange rate has been found for ${aFromCurrency}${aToCurrency}`,
'ExchangeRateDataService' 'ExchangeRateDataService'
); );
return aValue; return aValue;
} }

Loading…
Cancel
Save