Browse Source

Fix getExchangeRates()

pull/2834/head
Thomas Kaul 2 years ago
parent
commit
2ea4564b03
  1. 6
      apps/api/src/app/benchmark/benchmark.service.ts
  2. 28
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

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

@ -236,11 +236,9 @@ export class BenchmarkService {
]);
const exchangeRates = await this.exchangeRateDataService.getExchangeRates({
startDate,
currencyFrom: currentSymbolItem.currency,
currencyTo: userCurrency,
dates: marketDataItems.map(({ date }) => {
return date;
})
currencyTo: userCurrency
});
const exchangeRateAtStartDate =

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

@ -13,7 +13,7 @@ import {
resetHours
} from '@ghostfolio/common/helper';
import { Injectable, Logger } from '@nestjs/common';
import { addDays, format, isAfter, isToday } from 'date-fns';
import { addDays, eachDayOfInterval, format, isAfter, isToday } from 'date-fns';
import { isNumber, uniq } from 'lodash';
import ms from 'ms';
@ -53,21 +53,13 @@ export class ExchangeRateDataService {
return {};
}
let currentDate = resetHours(startDate);
let dates: Date[] = [];
let exchangeRatesByCurrency: {
[currency: string]: { [dateString: string]: number };
} = {};
while (isAfter(endDate, currentDate)) {
dates.push(currentDate);
currentDate = addDays(currentDate, 1);
}
for (let currency of uniq(Object.values(currencies))) {
for (let currency of uniq(currencies)) {
exchangeRatesByCurrency[currency] = await this.getExchangeRates({
dates,
startDate,
currencyFrom: currency,
currencyTo: targetCurrency
});
@ -94,12 +86,15 @@ export class ExchangeRateDataService {
public async getExchangeRates({
currencyFrom,
currencyTo,
dates
endDate = new Date(),
startDate
}: {
currencyFrom: string;
currencyTo: string;
dates: Date[];
endDate?: Date;
startDate: Date;
}) {
const dates = eachDayOfInterval({ end: endDate, start: startDate });
let factors: { [dateString: string]: number } = {};
if (currencyFrom === currencyTo) {
@ -112,7 +107,7 @@ export class ExchangeRateDataService {
const symbol = `${currencyFrom}${currencyTo}`;
const marketData = await this.marketDataService.getRange({
dateQuery: { in: dates },
dateQuery: { gte: startDate, lt: endDate },
uniqueAssets: [
{
dataSource,
@ -143,7 +138,7 @@ export class ExchangeRateDataService {
}
} else {
const marketData = await this.marketDataService.getRange({
dateQuery: { in: dates },
dateQuery: { gte: startDate, lt: endDate },
uniqueAssets: [
{
dataSource,
@ -167,7 +162,8 @@ export class ExchangeRateDataService {
} else {
const marketData = await this.marketDataService.getRange({
dateQuery: {
in: dates
gte: startDate,
lt: endDate
},
uniqueAssets: [
{

Loading…
Cancel
Save