From 42bae5b7191c59eadf32a4da8c15268daac75ee4 Mon Sep 17 00:00:00 2001 From: yksolanki9 Date: Mon, 2 Jan 2023 19:57:14 +0530 Subject: [PATCH] Add option to group dividends yearly --- .../src/app/portfolio/portfolio.service.ts | 44 +++++++++++++------ .../analysis/analysis-page.component.ts | 2 +- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 74a366702..e00de76e4 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -235,8 +235,8 @@ export class PortfolioService { }; }); - if (groupBy === 'month') { - dividends = this.getDividendsByMonth(dividends); + if (groupBy) { + dividends = this.getDividendsByGroup(dividends, groupBy); } const startDate = this.getStartDate( @@ -1269,42 +1269,58 @@ export class PortfolioService { ); } - private getDividendsByMonth(aDividends: InvestmentItem[]): InvestmentItem[] { + private getDividendsByGroup( + aDividends: InvestmentItem[], + groupBy: GroupBy + ): InvestmentItem[] { if (aDividends.length === 0) { return []; } const dividends = []; let currentDate: Date; - let investmentByMonth = new Big(0); + let investmentByGroup = new Big(0); for (const [index, dividend] of aDividends.entries()) { if ( - isSameMonth(parseDate(dividend.date), currentDate) && - isSameYear(parseDate(dividend.date), currentDate) + isSameYear(parseDate(dividend.date), currentDate) && + (groupBy === 'year' || + isSameMonth(parseDate(dividend.date), currentDate)) ) { - // Same month: Add up divididends + // Same group: Add up dividends - investmentByMonth = investmentByMonth.plus(dividend.investment); + investmentByGroup = investmentByGroup.plus(dividend.investment); } else { - // New month: Store previous month and reset + // New group: Store previous group and reset if (currentDate) { dividends.push({ - date: format(set(currentDate, { date: 1 }), DATE_FORMAT), - investment: investmentByMonth + date: format( + set( + currentDate, + groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 } + ), + DATE_FORMAT + ), + investment: investmentByGroup }); } currentDate = parseDate(dividend.date); - investmentByMonth = new Big(dividend.investment); + investmentByGroup = new Big(dividend.investment); } if (index === aDividends.length - 1) { // Store current month (latest order) dividends.push({ - date: format(set(currentDate, { date: 1 }), DATE_FORMAT), - investment: investmentByMonth + date: format( + set( + currentDate, + groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 } + ), + DATE_FORMAT + ), + investment: investmentByGroup }); } } diff --git a/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts b/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts index 130a6244f..3b089024c 100644 --- a/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts +++ b/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts @@ -296,7 +296,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit { this.dataService .fetchDividends({ filters: this.activeFilters, - groupBy: 'month', + groupBy: this.mode, range: this.user?.settings?.dateRange }) .pipe(takeUntil(this.unsubscribeSubject))