Browse Source

Fix Asset Allocation Overview

pull/5027/head
Dan 1 year ago
parent
commit
ea39ab15ea
  1. 88
      apps/api/src/app/portfolio/portfolio.service.ts

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

@ -490,22 +490,21 @@ export class PortfolioService {
}); });
} }
var netWorth =
summary?.netWorth ??
(await this.getNetWorth(impersonationId, userId, userCurrency));
return { return {
accounts, accounts,
holdings, holdings,
platforms, platforms,
summary, summary,
filteredValueInBaseCurrency: filteredValueInBaseCurrency.toNumber(), filteredValueInBaseCurrency: filteredValueInBaseCurrency.toNumber(),
filteredValueInPercentage: summary?.netWorth filteredValueInPercentage: netWorth
? filteredValueInBaseCurrency.div(summary.netWorth).toNumber() ? filteredValueInBaseCurrency.div(netWorth).toNumber()
: 0, : 0,
hasErrors: currentPositions.hasErrors, hasErrors: currentPositions.hasErrors,
totalValueInBaseCurrency: totalValueInBaseCurrency: netWorth
summary?.netWorth ??
Object.keys(holdings).reduce(
(s, k) => s + holdings[k].valueInBaseCurrency ?? 0,
0
)
}; };
} }
@ -1867,6 +1866,42 @@ export class PortfolioService {
return { currentStreak, longestStreak }; return { currentStreak, longestStreak };
} }
@LogPerformance
private async getNetWorth(
impersonationId: string,
userId: string,
userCurrency: string
) {
userId = await this.getUserId(impersonationId, userId);
const { orders, portfolioOrders, transactionPoints } =
await this.getTransactionPoints({
userId,
withExcludedAccounts: true
});
const portfolioCalculator = new PortfolioCalculator({
currency: userCurrency,
currentRateService: this.currentRateService,
exchangeRateDataService: this.exchangeRateDataService,
orders: portfolioOrders
});
const portfolioStart = parseDate(
transactionPoints[0]?.date ?? format(new Date(), DATE_FORMAT)
);
portfolioCalculator.setTransactionPoints(transactionPoints);
const { currentValue } = await portfolioCalculator.getCurrentPositions(
portfolioStart,
new Date(Date.now()),
false
);
return currentValue;
}
@LogPerformance @LogPerformance
private async getSummary({ private async getSummary({
balanceInBaseCurrency, balanceInBaseCurrency,
@ -1883,11 +1918,26 @@ export class PortfolioService {
}): Promise<PortfolioSummary> { }): Promise<PortfolioSummary> {
userId = await this.getUserId(impersonationId, userId); userId = await this.getUserId(impersonationId, userId);
const user = await this.userService.user({ id: userId }); const user = await this.userService.user({ id: userId });
let performanceInformation: PortfolioPerformanceResponse = {
const performanceInformation = await this.getPerformance({ chart: [],
impersonationId, firstOrderDate: undefined,
userId performance: {
}); annualizedPerformancePercent: 0,
currentGrossPerformance: 0,
currentGrossPerformancePercent: 0,
currentGrossPerformancePercentWithCurrencyEffect: 0,
currentGrossPerformanceWithCurrencyEffect: 0,
currentNetPerformance: 0,
currentNetPerformancePercent: 0,
currentNetPerformancePercentWithCurrencyEffect: 0,
currentNetPerformanceWithCurrencyEffect: 0,
currentNetWorth: 0,
currentValue: 0,
totalInvestment: 0
},
errors: [],
hasErrors: false
};
const { activities } = await this.orderService.getOrders({ const { activities } = await this.orderService.getOrders({
userCurrency, userCurrency,
@ -1906,6 +1956,13 @@ export class PortfolioService {
let totalSell = 0; let totalSell = 0;
let activitiesUsed: Activity[] = []; let activitiesUsed: Activity[] = [];
let ordersCount = 0; let ordersCount = 0;
let excludedAccountsAndActivities = 0;
const firstOrderDate = activities[0]?.date;
performanceInformation = await this.getPerformance({
impersonationId,
userId
});
for (let order of activities) { for (let order of activities) {
if (order.Account?.isExcluded ?? false) { if (order.Account?.isExcluded ?? false) {
excludedActivities.push(order); excludedActivities.push(order);
@ -1952,8 +2009,6 @@ export class PortfolioService {
) )
); );
const firstOrderDate = activitiesUsed[0]?.date;
const cash = new Big(balanceInBaseCurrency) const cash = new Big(balanceInBaseCurrency)
.minus(emergencyFund) .minus(emergencyFund)
.plus(emergencyFundPositionsValueInBaseCurrency) .plus(emergencyFundPositionsValueInBaseCurrency)
@ -1977,12 +2032,11 @@ export class PortfolioService {
currency: userCurrency, currency: userCurrency,
withExcludedAccounts: true withExcludedAccounts: true
}); });
const excludedBalanceInBaseCurrency = new Big( const excludedBalanceInBaseCurrency = new Big(
cashDetailsWithExcludedAccounts.balanceInBaseCurrency cashDetailsWithExcludedAccounts.balanceInBaseCurrency
).minus(balanceInBaseCurrency); ).minus(balanceInBaseCurrency);
const excludedAccountsAndActivities = excludedBalanceInBaseCurrency excludedAccountsAndActivities = excludedBalanceInBaseCurrency
.plus(totalOfExcludedActivities) .plus(totalOfExcludedActivities)
.toNumber(); .toNumber();

Loading…
Cancel
Save