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. 34
      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 =
exchangeRates[currentSymbolItem.currency]?.[
exchangeRates[`${currentSymbolItem.currency}${userCurrency}`]?.[
format(startDate, DATE_FORMAT)
];
@ -275,7 +275,9 @@ export class BenchmarkService {
}
const exchangeRate =
exchangeRates[format(marketDataItem.date, DATE_FORMAT)];
exchangeRates[`${currentSymbolItem.currency}${userCurrency}`]?.[
format(marketDataItem.date, DATE_FORMAT)
];
const exchangeRateFactor =
isNumber(exchangeRateAtStartDate) && isNumber(exchangeRate)
@ -300,7 +302,10 @@ export class BenchmarkService {
);
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 =
isNumber(exchangeRateAtStartDate) && isNumber(exchangeRate)

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

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

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

@ -64,11 +64,12 @@ export class ExchangeRateDataService {
} = {};
for (let currency of currencies) {
exchangeRatesByCurrency[currency] = await this.getExchangeRates({
startDate,
currencyFrom: currency,
currencyTo: targetCurrency
});
exchangeRatesByCurrency[`${currency}${targetCurrency}`] =
await this.getExchangeRates({
startDate,
currencyFrom: currency,
currencyTo: targetCurrency
});
let previousExchangeRate = 1;
@ -82,17 +83,25 @@ export class ExchangeRateDataService {
let dateString = format(date, DATE_FORMAT);
// 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
exchangeRatesByCurrency[currency][dateString] = previousExchangeRate;
exchangeRatesByCurrency[`${currency}${targetCurrency}`][dateString] =
previousExchangeRate;
Logger.error(
`No exchange rate has been found for ${DEFAULT_CURRENCY}${currency} at ${dateString}`,
'ExchangeRateDataService'
);
if (currency === DEFAULT_CURRENCY) {
Logger.error(
`No exchange rate has been found for ${currency}${targetCurrency} at ${dateString}`,
'ExchangeRateDataService'
);
}
} else {
// 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}`,
'ExchangeRateDataService'
);
return aValue;
}

Loading…
Cancel
Save