|
@ -307,74 +307,18 @@ export class PortfolioService { |
|
|
let investments: InvestmentItem[]; |
|
|
let investments: InvestmentItem[]; |
|
|
|
|
|
|
|
|
if (groupBy) { |
|
|
if (groupBy) { |
|
|
investments = portfolioCalculator |
|
|
investments = this.groupHistoricalDataItems(chartData.items, groupBy); |
|
|
.getInvestmentsByGroup(groupBy) |
|
|
|
|
|
.map((item) => { |
|
|
|
|
|
return { |
|
|
|
|
|
date: item.date, |
|
|
|
|
|
investment: item.investment.toNumber() |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Add investment of current group
|
|
|
|
|
|
const dateOfCurrentGroup = format( |
|
|
|
|
|
set(new Date(), { |
|
|
|
|
|
date: 1, |
|
|
|
|
|
month: groupBy === 'year' ? 0 : new Date().getMonth() |
|
|
|
|
|
}), |
|
|
|
|
|
DATE_FORMAT |
|
|
|
|
|
); |
|
|
|
|
|
const investmentOfCurrentGroup = investments.filter(({ date }) => { |
|
|
|
|
|
return date === dateOfCurrentGroup; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (investmentOfCurrentGroup.length <= 0) { |
|
|
|
|
|
investments.push({ |
|
|
|
|
|
date: dateOfCurrentGroup, |
|
|
|
|
|
investment: 0 |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
investments = portfolioCalculator |
|
|
investments = chartData.items.map( |
|
|
.getInvestments() |
|
|
({ date, investmentValueWithCurrencyEffect }) => { |
|
|
.map(({ date, investment }) => { |
|
|
|
|
|
return { |
|
|
return { |
|
|
date, |
|
|
date, |
|
|
investment: investment.toNumber() |
|
|
investment: investmentValueWithCurrencyEffect |
|
|
}; |
|
|
}; |
|
|
}); |
|
|
} |
|
|
|
|
|
); |
|
|
// Add investment of today
|
|
|
|
|
|
const investmentOfToday = investments.filter(({ date }) => { |
|
|
|
|
|
return date === format(new Date(), DATE_FORMAT); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (investmentOfToday.length <= 0) { |
|
|
|
|
|
const pastInvestments = investments.filter(({ date }) => { |
|
|
|
|
|
return isBefore(parseDate(date), new Date()); |
|
|
|
|
|
}); |
|
|
|
|
|
const lastInvestment = pastInvestments[pastInvestments.length - 1]; |
|
|
|
|
|
|
|
|
|
|
|
investments.push({ |
|
|
|
|
|
date: format(new Date(), DATE_FORMAT), |
|
|
|
|
|
investment: lastInvestment?.investment ?? 0 |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
investments = sortBy(investments, ({ date }) => { |
|
|
|
|
|
return date; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const startDate = this.getStartDate( |
|
|
|
|
|
dateRange, |
|
|
|
|
|
parseDate(investments[0]?.date) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
investments = investments.filter(({ date }) => { |
|
|
|
|
|
return !isBefore(parseDate(date), startDate); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
let streaks: PortfolioInvestments['streaks']; |
|
|
let streaks: PortfolioInvestments['streaks']; |
|
|
|
|
|
|
|
|
if (savingsRate) { |
|
|
if (savingsRate) { |
|
@ -2142,6 +2086,25 @@ export class PortfolioService { |
|
|
return { accounts, platforms }; |
|
|
return { accounts, platforms }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private groupHistoricalDataItems( |
|
|
|
|
|
data: HistoricalDataItem[], |
|
|
|
|
|
groupBy: GroupBy |
|
|
|
|
|
): InvestmentItem[] { |
|
|
|
|
|
const groupedData: { [dateGroup: string]: number } = {}; |
|
|
|
|
|
|
|
|
|
|
|
for (const { date, investmentValueWithCurrencyEffect } of data) { |
|
|
|
|
|
const unit = |
|
|
|
|
|
groupBy === 'month' ? date.substring(0, 7) : date.substring(0, 4); |
|
|
|
|
|
groupedData[unit] = |
|
|
|
|
|
(groupedData[unit] ?? 0) + investmentValueWithCurrencyEffect; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Object.keys(groupedData).map((unit) => ({ |
|
|
|
|
|
date: groupBy === 'month' ? `${unit}-01` : `${unit}-01-01`, |
|
|
|
|
|
investment: groupedData[unit] |
|
|
|
|
|
})); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private mergeHistoricalDataItems( |
|
|
private mergeHistoricalDataItems( |
|
|
accountBalanceItems: HistoricalDataItem[], |
|
|
accountBalanceItems: HistoricalDataItem[], |
|
|
performanceChartItems: HistoricalDataItem[] |
|
|
performanceChartItems: HistoricalDataItem[] |
|
|