Browse Source

Add groupBy=year to portfolio performance endpoint

pull/8094/head
Thomas Kaul 6 days ago
parent
commit
2698b13a21
  1. 32
      apps/api/src/app/portfolio/portfolio.service.spec.ts
  2. 23
      apps/api/src/app/portfolio/portfolio.service.ts

32
apps/api/src/app/portfolio/portfolio.service.spec.ts

@ -1158,38 +1158,6 @@ describe('PortfolioService', () => {
{ date: '2025-01-01', investmentValueWithCurrencyEffect: 200 } { date: '2025-01-01', investmentValueWithCurrencyEffect: 200 }
]); ]);
}); });
it('should skip a calendar year without a chart when grouped by year', async () => {
getPerformanceOfCalculator
.mockResolvedValueOnce({
chart: [
{ date: '2023-12-31', netPerformance: 0 },
{ date: '2024-12-31', netPerformance: 200 },
{ date: '2025-06-15', netPerformance: 300 }
]
})
.mockResolvedValueOnce({ chart: [] })
.mockResolvedValueOnce({
chart: [
{ date: '2024-12-31', netPerformance: 0 },
{ date: '2025-06-15', netPerformance: 100 }
]
});
const { chart } = await portfolioService.getPerformance({
dateRange: 'max',
groupBy: 'year',
userId: userDummyData.id
});
expect(chart).toEqual([
{
date: '2025-01-01',
investmentValueWithCurrencyEffect: 0,
netPerformance: 100
}
]);
});
}); });
describe('getSummary', () => { describe('getSummary', () => {

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

@ -2065,15 +2065,14 @@ export class PortfolioService {
} }
/** /**
* Returns one chart item per calendar year of the date range, like the * Returns one chart item per calendar year of the date range, dated on
* dividends and the investments grouped by year, dated on 1 January. Each * 1 January. Each year stands alone and is not accumulated, so its values
* year stands alone and is not accumulated, so its values are the same as * are the same as for the date range of the year (e.g. '2024'), clipped to
* for the date range of the year (e.g. '2024'), clipped to the requested * the requested date range. The investment is the sum of the year, the other
* date range. The investment is the sum of the year, the other values are * values are the ones at the end of the year. The first chart item of the
* the ones at the end of the year. The first chart item of the date range * date range carries the values at its start date and belongs to the
* carries the values at its start date and belongs to the previous period, * previous period, like 31 December for a calendar year, so the years before
* like 31 December for a calendar year, so the years before the first * the first activity have no chart item.
* activity have no chart item.
*/ */
private async getPerformanceByYear({ private async getPerformanceByYear({
chartOfDateRange, chartOfDateRange,
@ -2089,7 +2088,7 @@ export class PortfolioService {
const chart: HistoricalDataItem[] = []; const chart: HistoricalDataItem[] = [];
const years = uniq( const years = uniq(
chartOfDateRange?.slice(1).map(({ date }) => { chartOfDateRange.slice(1).map(({ date }) => {
return date.substring(0, 4); return date.substring(0, 4);
}) })
); );
@ -2103,10 +2102,6 @@ export class PortfolioService {
start: index === 0 ? startDate : startDateOfYear start: index === 0 ? startDate : startDateOfYear
}); });
if (!chartOfYear?.length) {
continue;
}
let investmentValueWithCurrencyEffect = new Big(0); let investmentValueWithCurrencyEffect = new Big(0);
for (const historicalDataItem of chartOfYear.slice(1)) { for (const historicalDataItem of chartOfYear.slice(1)) {

Loading…
Cancel
Save