From 634bdf16d6e4fc93bcee8536f6404026e187012e Mon Sep 17 00:00:00 2001
From: csehatt741 <77381875+csehatt741@users.noreply.github.com>
Date: Mon, 24 Feb 2025 21:54:53 +0100
Subject: [PATCH] Feature/extend portfolio snapshot by activities count (#4352)

* Extend portfolio snapshot by activities count

* Update changelog
---
 CHANGELOG.md                                                   | 1 +
 apps/api/src/app/portfolio/calculator/portfolio-calculator.ts  | 1 +
 .../src/app/portfolio/calculator/twr/portfolio-calculator.ts   | 3 +++
 libs/common/src/lib/models/portfolio-snapshot.ts               | 2 ++
 4 files changed, 7 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 019bf4b6a..ecbcdc820 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 
 - Extended the export functionality by the tags
+- Extended the portfolio snapshot in the portfolio calculator by the activities count
 - Extended the user endpoint `GET api/v1/user` by the activities count
 
 ### Changed
diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
index 2f8a9f0c9..e9c4fdcce 100644
--- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
+++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
@@ -175,6 +175,7 @@ export abstract class PortfolioCalculator {
 
     if (!transactionPoints.length) {
       return {
+        activitiesCount: 0,
         currentValueInBaseCurrency: new Big(0),
         errors: [],
         hasErrors: false,
diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts
index cf808debb..209a0efd5 100644
--- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts
+++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts
@@ -101,6 +101,9 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
       totalInterestWithCurrencyEffect,
       totalInvestment,
       totalInvestmentWithCurrencyEffect,
+      activitiesCount: this.activities.filter(({ type }) => {
+        return ['BUY', 'SELL'].includes(type);
+      }).length,
       errors: [],
       historicalData: [],
       totalLiabilitiesWithCurrencyEffect: new Big(0),
diff --git a/libs/common/src/lib/models/portfolio-snapshot.ts b/libs/common/src/lib/models/portfolio-snapshot.ts
index 46bd5c18f..d0c4c66e0 100644
--- a/libs/common/src/lib/models/portfolio-snapshot.ts
+++ b/libs/common/src/lib/models/portfolio-snapshot.ts
@@ -9,6 +9,8 @@ import { Big } from 'big.js';
 import { Transform, Type } from 'class-transformer';
 
 export class PortfolioSnapshot {
+  activitiesCount: number;
+
   @Transform(transformToBig, { toClassOnly: true })
   @Type(() => Big)
   currentValueInBaseCurrency: Big;