diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index 629c4b33e..9cfbefd08 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -774,7 +774,16 @@ export abstract class PortfolioCalculator { } @LogPerformance - public async getPerformance({ end, start }) { + public async getPerformance({ end, start }): Promise<{ + chart: HistoricalDataItem[]; + netPerformance: number; + netPerformanceInPercentage: number; + netPerformanceWithCurrencyEffect: number; + netPerformanceInPercentageWithCurrencyEffect: number; + netWorth: number; + totalInvestment: number; + valueWithCurrencyEffect: number; + }> { await this.snapshotPromise; const { historicalData } = this.snapshot; @@ -815,6 +824,7 @@ export abstract class PortfolioCalculator { totalInvestmentValuesWithCurrencyEffect.length : 0; + //TODO : Extract in abstractFunction and use timeweighted for ROI + Handle total values separately chart.push({ ...historicalDataItem, netPerformance: @@ -839,7 +849,20 @@ export abstract class PortfolioCalculator { } } - return { chart }; + const last = chart.at(-1); + + return { + chart, + netPerformance: last?.netPerformance ?? 0, + netPerformanceInPercentage: last?.netPerformanceInPercentage ?? 0, + netPerformanceWithCurrencyEffect: + last?.netPerformanceWithCurrencyEffect ?? 0, + netPerformanceInPercentageWithCurrencyEffect: + last?.netPerformanceInPercentageWithCurrencyEffect ?? 0, + netWorth: last?.netWorth ?? 0, + totalInvestment: last?.totalInvestment ?? 0, + valueWithCurrencyEffect: last?.valueWithCurrencyEffect ?? 0 + }; } public getStartDate() { diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 2fab5e5ec..96c89fc63 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -1155,9 +1155,8 @@ export class PortfolioService { const { endDate, startDate } = getIntervalFromDateRange(dateRange); const range = { end: endDate, start: startDate }; - const { chart } = await portfolioCalculator.getPerformance(range); - const { + chart, netPerformance, netPerformanceInPercentage, netPerformanceInPercentageWithCurrencyEffect, @@ -1165,15 +1164,7 @@ export class PortfolioService { netWorth, totalInvestment, valueWithCurrencyEffect - } = chart?.at(-1) ?? { - netPerformance: 0, - netPerformanceInPercentage: 0, - netPerformanceInPercentageWithCurrencyEffect: 0, - netPerformanceWithCurrencyEffect: 0, - netWorth: 0, - totalInvestment: 0, - valueWithCurrencyEffect: 0 - }; + } = await portfolioCalculator.getPerformance(range); return { chart,