From 35963580bc2fb60cc34e883833d015b50cf1b133 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 3 Sep 2022 08:31:47 +0200 Subject: [PATCH] Bugfix/improve error handling in portfolio calculations (#1215) * Improve error handling * Update changelog --- CHANGELOG.md | 1 + .../src/app/portfolio/portfolio-calculator.ts | 46 +++++++++++-------- .../src/app/portfolio/portfolio.service.ts | 10 ++-- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a039ca7f..66c6e6e55 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 ### Fixed - Made the environment variables `REDIS_HOST` and `REDIS_PORT` mandatory +- Handled errors in the portfolio calculation if there is no internet connection ## 1.185.0 - 30.08.2022 diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index cafc65b8b..b525853de 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -432,30 +432,36 @@ export class PortfolioCalculator { } } + let minNetPerformance = new Big(0); + let maxNetPerformance = new Big(0); + const timelineInfoInterfaces: TimelineInfoInterface[] = await Promise.all( timelinePeriodPromises ); - const minNetPerformance = timelineInfoInterfaces - .map((timelineInfo) => timelineInfo.minNetPerformance) - .filter((performance) => performance !== null) - .reduce((minPerformance, current) => { - if (minPerformance.lt(current)) { - return minPerformance; - } else { - return current; - } - }); - const maxNetPerformance = timelineInfoInterfaces - .map((timelineInfo) => timelineInfo.maxNetPerformance) - .filter((performance) => performance !== null) - .reduce((maxPerformance, current) => { - if (maxPerformance.gt(current)) { - return maxPerformance; - } else { - return current; - } - }); + try { + minNetPerformance = timelineInfoInterfaces + .map((timelineInfo) => timelineInfo.minNetPerformance) + .filter((performance) => performance !== null) + .reduce((minPerformance, current) => { + if (minPerformance.lt(current)) { + return minPerformance; + } else { + return current; + } + }); + + maxNetPerformance = timelineInfoInterfaces + .map((timelineInfo) => timelineInfo.maxNetPerformance) + .filter((performance) => performance !== null) + .reduce((maxPerformance, current) => { + if (maxPerformance.gt(current)) { + return maxPerformance; + } else { + return current; + } + }); + } catch {} const timelinePeriods = timelineInfoInterfaces.map( (timelineInfo) => timelineInfo.timelinePeriods diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 09cc03455..8b925b4e0 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -327,10 +327,10 @@ export class PortfolioService { } let isAllTimeHigh = timelineInfo.maxNetPerformance?.eq( - lastItem?.netPerformance + lastItem?.netPerformance ?? 0 ); let isAllTimeLow = timelineInfo.minNetPerformance?.eq( - lastItem?.netPerformance + lastItem?.netPerformance ?? 0 ); if (isAllTimeHigh && isAllTimeLow) { isAllTimeHigh = false; @@ -466,7 +466,9 @@ export class PortfolioService { holdings[item.symbol] = { markets, - allocationCurrent: value.div(totalValue).toNumber(), + allocationCurrent: totalValue.eq(0) + ? 0 + : value.div(totalValue).toNumber(), allocationInvestment: item.investment.div(totalInvestment).toNumber(), assetClass: symbolProfile.assetClass, assetSubClass: symbolProfile.assetSubClass, @@ -478,7 +480,7 @@ export class PortfolioService { item.grossPerformancePercentage?.toNumber() ?? 0, investment: item.investment.toNumber(), marketPrice: item.marketPrice, - marketState: dataProviderResponse.marketState, + marketState: dataProviderResponse?.marketState ?? 'delayed', name: symbolProfile.name, netPerformance: item.netPerformance?.toNumber() ?? 0, netPerformancePercent: item.netPerformancePercentage?.toNumber() ?? 0,