From fe975945d1a2d24a4611369c229fcb9fafa8d196 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Tue, 24 Aug 2021 21:09:02 +0200 Subject: [PATCH] Feature/add fallback for loading currencies (#315) * Add fallback for loading currencies * Update changelog --- CHANGELOG.md | 1 + .../services/exchange-rate-data.service.ts | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b28db8ee1..4d53b537b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Extended the data management of symbol profile data by countries (automated for stocks) +- Added a fallback for initially loading currencies if historical data is not yet available ## 1.42.0 - 22.08.2021 diff --git a/apps/api/src/services/exchange-rate-data.service.ts b/apps/api/src/services/exchange-rate-data.service.ts index afb6ea122..a2617647f 100644 --- a/apps/api/src/services/exchange-rate-data.service.ts +++ b/apps/api/src/services/exchange-rate-data.service.ts @@ -3,7 +3,7 @@ import { DATE_FORMAT, getYesterday } from '@ghostfolio/common/helper'; import { Injectable } from '@nestjs/common'; import { Currency } from '@prisma/client'; import { format } from 'date-fns'; -import { isNumber } from 'lodash'; +import { isEmpty, isNumber } from 'lodash'; import { DataProviderService } from './data-provider/data-provider.service'; @@ -35,6 +35,24 @@ export class ExchangeRateDataService { getYesterday() ); + if (isEmpty(result)) { + // Load currencies directly from data provider as a fallback + // if historical data is not yet available + const historicalData = await this.dataProviderService.get( + this.currencyPairs.map((currencyPair) => { + return currencyPair; + }) + ); + + Object.keys(historicalData).forEach((key) => { + result[key] = { + [format(getYesterday(), DATE_FORMAT)]: { + marketPrice: historicalData[key].marketPrice + } + }; + }); + } + const resultExtended = result; Object.keys(result).forEach((pair) => {