Browse Source

Calculate net worth

pull/2574/head
Thomas 2 years ago
parent
commit
01156cf184
  1. 34
      apps/api/src/app/portfolio/portfolio.controller.ts
  2. 25
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 1
      libs/common/src/lib/interfaces/portfolio-performance.interface.ts

34
apps/api/src/app/portfolio/portfolio.controller.ts

@ -346,17 +346,34 @@ export class PortfolioController {
this.userService.isRestrictedView(this.request.user) this.userService.isRestrictedView(this.request.user)
) { ) {
performanceInformation.chart = performanceInformation.chart.map( performanceInformation.chart = performanceInformation.chart.map(
({ date, netPerformanceInPercentage, totalInvestment, value }) => { ({
date,
netPerformanceInPercentage,
netWorth,
totalInvestment,
value
}) => {
return { return {
date, date,
netPerformanceInPercentage, netPerformanceInPercentage,
netWorthInPercentage: 0, // TODO netWorthInPercentage:
totalInvestment: new Big(totalInvestment) performanceInformation.performance.currentNetWorth === 0
.div(performanceInformation.performance.totalInvestment) ? 0
.toNumber(), : new Big(netWorth)
valueInPercentage: new Big(value) .div(performanceInformation.performance.currentNetWorth)
.div(performanceInformation.performance.currentValue) .toNumber(),
.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', 'currentGrossPerformance',
'currentNetPerformance', 'currentNetPerformance',
'currentNetWorth',
'currentValue', 'currentValue',
'totalInvestment' 'totalInvestment'
] ]

25
apps/api/src/app/portfolio/portfolio.service.ts

@ -77,7 +77,7 @@ import {
subDays, subDays,
subYears subYears
} from 'date-fns'; } from 'date-fns';
import { isEmpty, sortBy, uniq, uniqBy } from 'lodash'; import { isEmpty, last, sortBy, uniq, uniqBy } from 'lodash';
import { import {
HistoricalDataContainer, HistoricalDataContainer,
@ -1183,6 +1183,7 @@ export class PortfolioService {
currentGrossPerformancePercent: 0, currentGrossPerformancePercent: 0,
currentNetPerformance: 0, currentNetPerformance: 0,
currentNetPerformancePercent: 0, currentNetPerformancePercent: 0,
currentNetWorth: 0,
currentValue: 0, currentValue: 0,
totalInvestment: 0 totalInvestment: 0
} }
@ -1246,18 +1247,22 @@ export class PortfolioService {
items items
); );
const currentHistoricalDataItem = last(mergedHistoricalDataItems);
const currentNetWorth = currentHistoricalDataItem?.netWorth ?? 0;
return { return {
errors, errors,
hasErrors, hasErrors,
chart: mergedHistoricalDataItems, chart: mergedHistoricalDataItems,
firstOrderDate: parseDate(items[0]?.date), firstOrderDate: parseDate(items[0]?.date),
performance: { performance: {
currentValue: currentValue.toNumber(), currentNetWorth,
currentGrossPerformance: currentGrossPerformance.toNumber(), currentGrossPerformance: currentGrossPerformance.toNumber(),
currentGrossPerformancePercent: currentGrossPerformancePercent:
currentGrossPerformancePercent.toNumber(), currentGrossPerformancePercent.toNumber(),
currentNetPerformance: currentNetPerformance.toNumber(), currentNetPerformance: currentNetPerformance.toNumber(),
currentNetPerformancePercent: currentNetPerformancePercent.toNumber(), currentNetPerformancePercent: currentNetPerformancePercent.toNumber(),
currentValue: currentValue.toNumber(),
totalInvestment: totalInvestment.toNumber() totalInvestment: totalInvestment.toNumber()
} }
}; };
@ -2045,18 +2050,20 @@ export class PortfolioService {
for (const item of accountBalanceItems.concat(performanceChartItems)) { for (const item of accountBalanceItems.concat(performanceChartItems)) {
const isAccountBalanceItem = accountBalanceItems.includes(item); const isAccountBalanceItem = accountBalanceItems.includes(item);
// TODO: Still zero if no account balance item has been tracked yet
const totalAccountBalance = isAccountBalanceItem const totalAccountBalance = isAccountBalanceItem
? item.value ? item.value
: latestAccountBalance; : latestAccountBalance;
historicalDataItemsMap[item.date] = { if (isAccountBalanceItem && performanceChartItems.length > 0) {
...item,
totalAccountBalance,
netWorth: (isAccountBalanceItem ? 0 : item.value) + totalAccountBalance
};
if (isAccountBalanceItem) {
latestAccountBalance = item.value; latestAccountBalance = item.value;
} else {
historicalDataItemsMap[item.date] = {
...item,
totalAccountBalance,
netWorth:
(isAccountBalanceItem ? 0 : item.value) + totalAccountBalance
};
} }
} }

1
libs/common/src/lib/interfaces/portfolio-performance.interface.ts

@ -4,6 +4,7 @@ export interface PortfolioPerformance {
currentGrossPerformancePercent: number; currentGrossPerformancePercent: number;
currentNetPerformance: number; currentNetPerformance: number;
currentNetPerformancePercent: number; currentNetPerformancePercent: number;
currentNetWorth: number;
currentValue: number; currentValue: number;
totalInvestment: number; totalInvestment: number;
} }

Loading…
Cancel
Save