Browse Source

Add option to group dividends yearly

pull/1568/head
yksolanki9 3 years ago
parent
commit
42bae5b719
  1. 44
      apps/api/src/app/portfolio/portfolio.service.ts
  2. 2
      apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts

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

@ -235,8 +235,8 @@ export class PortfolioService {
}; };
}); });
if (groupBy === 'month') { if (groupBy) {
dividends = this.getDividendsByMonth(dividends); dividends = this.getDividendsByGroup(dividends, groupBy);
} }
const startDate = this.getStartDate( 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) { if (aDividends.length === 0) {
return []; return [];
} }
const dividends = []; const dividends = [];
let currentDate: Date; let currentDate: Date;
let investmentByMonth = new Big(0); let investmentByGroup = new Big(0);
for (const [index, dividend] of aDividends.entries()) { for (const [index, dividend] of aDividends.entries()) {
if ( 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 { } else {
// New month: Store previous month and reset // New group: Store previous group and reset
if (currentDate) { if (currentDate) {
dividends.push({ dividends.push({
date: format(set(currentDate, { date: 1 }), DATE_FORMAT), date: format(
investment: investmentByMonth set(
currentDate,
groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 }
),
DATE_FORMAT
),
investment: investmentByGroup
}); });
} }
currentDate = parseDate(dividend.date); currentDate = parseDate(dividend.date);
investmentByMonth = new Big(dividend.investment); investmentByGroup = new Big(dividend.investment);
} }
if (index === aDividends.length - 1) { if (index === aDividends.length - 1) {
// Store current month (latest order) // Store current month (latest order)
dividends.push({ dividends.push({
date: format(set(currentDate, { date: 1 }), DATE_FORMAT), date: format(
investment: investmentByMonth set(
currentDate,
groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 }
),
DATE_FORMAT
),
investment: investmentByGroup
}); });
} }
} }

2
apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts

@ -296,7 +296,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
this.dataService this.dataService
.fetchDividends({ .fetchDividends({
filters: this.activeFilters, filters: this.activeFilters,
groupBy: 'month', groupBy: this.mode,
range: this.user?.settings?.dateRange range: this.user?.settings?.dateRange
}) })
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))

Loading…
Cancel
Save