Browse Source

Calculate net worth

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

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

@ -346,15 +346,32 @@ 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)
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: new Big(value)
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'
]

19
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;
if (isAccountBalanceItem && performanceChartItems.length > 0) {
latestAccountBalance = item.value;
} else {
historicalDataItemsMap[item.date] = {
...item,
totalAccountBalance,
netWorth: (isAccountBalanceItem ? 0 : item.value) + totalAccountBalance
netWorth:
(isAccountBalanceItem ? 0 : item.value) + totalAccountBalance
};
if (isAccountBalanceItem) {
latestAccountBalance = item.value;
}
}

1
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;
}

Loading…
Cancel
Save