Browse Source

Add groupBy=year to portfolio performance endpoint

pull/8094/head
Thomas Kaul 5 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 }
]);
});
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', () => {

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

Loading…
Cancel
Save