Feature/optimize summary calculation (#3088)
* Optimize calculation * Update changelog
This commit is contained in:
parent
bcdd873222
commit
b932bac9aa
@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Optimized the calculation of the portfolio summary
|
||||||
|
|
||||||
## 2.60.0 - 2024-03-02
|
## 2.60.0 - 2024-03-02
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@ -62,6 +62,7 @@ import {
|
|||||||
Tag
|
Tag
|
||||||
} from '@prisma/client';
|
} from '@prisma/client';
|
||||||
import Big from 'big.js';
|
import Big from 'big.js';
|
||||||
|
import { isUUID } from 'class-validator';
|
||||||
import {
|
import {
|
||||||
differenceInDays,
|
differenceInDays,
|
||||||
format,
|
format,
|
||||||
@ -617,6 +618,7 @@ export class PortfolioService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const summary = await this.getSummary({
|
const summary = await this.getSummary({
|
||||||
|
holdings,
|
||||||
impersonationId,
|
impersonationId,
|
||||||
userCurrency,
|
userCurrency,
|
||||||
userId,
|
userId,
|
||||||
@ -1708,12 +1710,14 @@ export class PortfolioService {
|
|||||||
private async getSummary({
|
private async getSummary({
|
||||||
balanceInBaseCurrency,
|
balanceInBaseCurrency,
|
||||||
emergencyFundPositionsValueInBaseCurrency,
|
emergencyFundPositionsValueInBaseCurrency,
|
||||||
|
holdings,
|
||||||
impersonationId,
|
impersonationId,
|
||||||
userCurrency,
|
userCurrency,
|
||||||
userId
|
userId
|
||||||
}: {
|
}: {
|
||||||
balanceInBaseCurrency: number;
|
balanceInBaseCurrency: number;
|
||||||
emergencyFundPositionsValueInBaseCurrency: number;
|
emergencyFundPositionsValueInBaseCurrency: number;
|
||||||
|
holdings: PortfolioDetails['holdings'];
|
||||||
impersonationId: string;
|
impersonationId: string;
|
||||||
userCurrency: string;
|
userCurrency: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
@ -1727,43 +1731,57 @@ export class PortfolioService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const { activities } = await this.orderService.getOrders({
|
const { activities } = await this.orderService.getOrders({
|
||||||
userCurrency,
|
|
||||||
userId
|
|
||||||
});
|
|
||||||
|
|
||||||
let { activities: excludedActivities } = await this.orderService.getOrders({
|
|
||||||
userCurrency,
|
userCurrency,
|
||||||
userId,
|
userId,
|
||||||
withExcludedAccounts: true
|
withExcludedAccounts: true
|
||||||
});
|
});
|
||||||
|
|
||||||
excludedActivities = excludedActivities.filter(({ Account: account }) => {
|
const excludedActivities: Activity[] = [];
|
||||||
return account?.isExcluded ?? false;
|
const nonExcludedActivities: Activity[] = [];
|
||||||
});
|
|
||||||
|
for (const activity of activities) {
|
||||||
|
if (activity.Account?.isExcluded) {
|
||||||
|
excludedActivities.push(activity);
|
||||||
|
} else {
|
||||||
|
nonExcludedActivities.push(activity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const dividend = this.getSumOfActivityType({
|
const dividend = this.getSumOfActivityType({
|
||||||
activities,
|
activities,
|
||||||
userCurrency,
|
userCurrency,
|
||||||
activityType: 'DIVIDEND'
|
activityType: 'DIVIDEND'
|
||||||
}).toNumber();
|
}).toNumber();
|
||||||
|
|
||||||
const emergencyFund = new Big(
|
const emergencyFund = new Big(
|
||||||
Math.max(
|
Math.max(
|
||||||
emergencyFundPositionsValueInBaseCurrency,
|
emergencyFundPositionsValueInBaseCurrency,
|
||||||
(user.Settings?.settings as UserSettings)?.emergencyFund ?? 0
|
(user.Settings?.settings as UserSettings)?.emergencyFund ?? 0
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
const fees = this.getFees({ activities, userCurrency }).toNumber();
|
const fees = this.getFees({ activities, userCurrency }).toNumber();
|
||||||
const firstOrderDate = activities[0]?.date;
|
const firstOrderDate = activities[0]?.date;
|
||||||
|
|
||||||
const interest = this.getSumOfActivityType({
|
const interest = this.getSumOfActivityType({
|
||||||
activities,
|
activities,
|
||||||
userCurrency,
|
userCurrency,
|
||||||
activityType: 'INTEREST'
|
activityType: 'INTEREST'
|
||||||
}).toNumber();
|
}).toNumber();
|
||||||
const items = this.getSumOfActivityType({
|
|
||||||
activities,
|
const items = Object.keys(holdings)
|
||||||
userCurrency,
|
.filter((symbol) => {
|
||||||
activityType: 'ITEM'
|
return isUUID(symbol) && holdings[symbol].dataSource === 'MANUAL';
|
||||||
}).toNumber();
|
})
|
||||||
|
.map((symbol) => {
|
||||||
|
return holdings[symbol].valueInBaseCurrency;
|
||||||
|
})
|
||||||
|
.reduce(
|
||||||
|
(previous, current) => new Big(previous).plus(current),
|
||||||
|
new Big(0)
|
||||||
|
)
|
||||||
|
.toNumber();
|
||||||
|
|
||||||
const liabilities = this.getSumOfActivityType({
|
const liabilities = this.getSumOfActivityType({
|
||||||
activities,
|
activities,
|
||||||
userCurrency,
|
userCurrency,
|
||||||
@ -1771,13 +1789,14 @@ export class PortfolioService {
|
|||||||
}).toNumber();
|
}).toNumber();
|
||||||
|
|
||||||
const totalBuy = this.getSumOfActivityType({
|
const totalBuy = this.getSumOfActivityType({
|
||||||
activities,
|
|
||||||
userCurrency,
|
userCurrency,
|
||||||
|
activities: nonExcludedActivities,
|
||||||
activityType: 'BUY'
|
activityType: 'BUY'
|
||||||
}).toNumber();
|
}).toNumber();
|
||||||
|
|
||||||
const totalSell = this.getSumOfActivityType({
|
const totalSell = this.getSumOfActivityType({
|
||||||
activities,
|
|
||||||
userCurrency,
|
userCurrency,
|
||||||
|
activities: nonExcludedActivities,
|
||||||
activityType: 'SELL'
|
activityType: 'SELL'
|
||||||
}).toNumber();
|
}).toNumber();
|
||||||
|
|
||||||
@ -1785,7 +1804,9 @@ export class PortfolioService {
|
|||||||
.minus(emergencyFund)
|
.minus(emergencyFund)
|
||||||
.plus(emergencyFundPositionsValueInBaseCurrency)
|
.plus(emergencyFundPositionsValueInBaseCurrency)
|
||||||
.toNumber();
|
.toNumber();
|
||||||
|
|
||||||
const committedFunds = new Big(totalBuy).minus(totalSell);
|
const committedFunds = new Big(totalBuy).minus(totalSell);
|
||||||
|
|
||||||
const totalOfExcludedActivities = this.getSumOfActivityType({
|
const totalOfExcludedActivities = this.getSumOfActivityType({
|
||||||
userCurrency,
|
userCurrency,
|
||||||
activities: excludedActivities,
|
activities: excludedActivities,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user