From 01156cf1844db9bf1806bb21f268b4f0601fbac2 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sun, 19 Nov 2023 10:13:58 +0100 Subject: [PATCH] Calculate net worth --- .../src/app/portfolio/portfolio.controller.ts | 34 ++++++++++++++----- .../src/app/portfolio/portfolio.service.ts | 25 +++++++++----- .../portfolio-performance.interface.ts | 1 + 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio.controller.ts b/apps/api/src/app/portfolio/portfolio.controller.ts index 5f02dca02..dd013989f 100644 --- a/apps/api/src/app/portfolio/portfolio.controller.ts +++ b/apps/api/src/app/portfolio/portfolio.controller.ts @@ -346,17 +346,34 @@ export class PortfolioController { this.userService.isRestrictedView(this.request.user) ) { performanceInformation.chart = performanceInformation.chart.map( - ({ date, netPerformanceInPercentage, totalInvestment, value }) => { + ({ + date, + netPerformanceInPercentage, + netWorth, + totalInvestment, + value + }) => { return { date, netPerformanceInPercentage, - netWorthInPercentage: 0, // TODO - totalInvestment: new Big(totalInvestment) - .div(performanceInformation.performance.totalInvestment) - .toNumber(), - valueInPercentage: new Big(value) - .div(performanceInformation.performance.currentValue) - .toNumber() + netWorthInPercentage: + performanceInformation.performance.currentNetWorth === 0 + ? 0 + : new Big(netWorth) + .div(performanceInformation.performance.currentNetWorth) + .toNumber(), + totalInvestment: + performanceInformation.performance.totalInvestment === 0 + ? 0 + : new Big(totalInvestment) + .div(performanceInformation.performance.totalInvestment) + .toNumber(), + valueInPercentage: + performanceInformation.performance.currentValue === 0 + ? 0 + : new Big(value) + .div(performanceInformation.performance.currentValue) + .toNumber() }; } ); @@ -366,6 +383,7 @@ export class PortfolioController { [ 'currentGrossPerformance', 'currentNetPerformance', + 'currentNetWorth', 'currentValue', 'totalInvestment' ] diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 99c9360f0..27b64f211 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -77,7 +77,7 @@ import { subDays, subYears } from 'date-fns'; -import { isEmpty, sortBy, uniq, uniqBy } from 'lodash'; +import { isEmpty, last, sortBy, uniq, uniqBy } from 'lodash'; import { HistoricalDataContainer, @@ -1183,6 +1183,7 @@ export class PortfolioService { currentGrossPerformancePercent: 0, currentNetPerformance: 0, currentNetPerformancePercent: 0, + currentNetWorth: 0, currentValue: 0, totalInvestment: 0 } @@ -1246,18 +1247,22 @@ export class PortfolioService { items ); + const currentHistoricalDataItem = last(mergedHistoricalDataItems); + const currentNetWorth = currentHistoricalDataItem?.netWorth ?? 0; + return { errors, hasErrors, chart: mergedHistoricalDataItems, firstOrderDate: parseDate(items[0]?.date), performance: { - currentValue: currentValue.toNumber(), + currentNetWorth, currentGrossPerformance: currentGrossPerformance.toNumber(), currentGrossPerformancePercent: currentGrossPerformancePercent.toNumber(), currentNetPerformance: currentNetPerformance.toNumber(), currentNetPerformancePercent: currentNetPerformancePercent.toNumber(), + currentValue: currentValue.toNumber(), totalInvestment: totalInvestment.toNumber() } }; @@ -2045,18 +2050,20 @@ export class PortfolioService { for (const item of accountBalanceItems.concat(performanceChartItems)) { const isAccountBalanceItem = accountBalanceItems.includes(item); + // TODO: Still zero if no account balance item has been tracked yet const totalAccountBalance = isAccountBalanceItem ? item.value : latestAccountBalance; - historicalDataItemsMap[item.date] = { - ...item, - totalAccountBalance, - netWorth: (isAccountBalanceItem ? 0 : item.value) + totalAccountBalance - }; - - if (isAccountBalanceItem) { + if (isAccountBalanceItem && performanceChartItems.length > 0) { latestAccountBalance = item.value; + } else { + historicalDataItemsMap[item.date] = { + ...item, + totalAccountBalance, + netWorth: + (isAccountBalanceItem ? 0 : item.value) + totalAccountBalance + }; } } diff --git a/libs/common/src/lib/interfaces/portfolio-performance.interface.ts b/libs/common/src/lib/interfaces/portfolio-performance.interface.ts index 5a4f91023..0343ef338 100644 --- a/libs/common/src/lib/interfaces/portfolio-performance.interface.ts +++ b/libs/common/src/lib/interfaces/portfolio-performance.interface.ts @@ -4,6 +4,7 @@ export interface PortfolioPerformance { currentGrossPerformancePercent: number; currentNetPerformance: number; currentNetPerformancePercent: number; + currentNetWorth: number; currentValue: number; totalInvestment: number; }