Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Task/remove fallbacks for cached portfolio snapshots (#7991)

Thomas Kaul67ee4b0a26e4
diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
index 0b2207b1fb..4ef6a95841 100644
--- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
+++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
@@ -1863,10 +1863,8 @@ export abstract class PortfolioCalculator {
// Take the values at the start date from the first day of the date
// range
if (historicalDataItemsOfDateRange.length === 0) {
- // TODO: Remove the fallback to 0 with the next release, when each
- // cached portfolio snapshot contains the dividend
dividendInBaseCurrencyAtStartDate =
- historicalDataItem.dividendInBaseCurrency ?? 0;
+ historicalDataItem.dividendInBaseCurrency;
netPerformanceAtStartDate = historicalDataItem.netPerformance;
@@ -1876,10 +1874,8 @@ export abstract class PortfolioCalculator {
historicalDataItemsOfDateRange.push({
...historicalDataItem,
- // TODO: Remove the fallback to 0 with the next release, when each
- // cached portfolio snapshot contains the dividend
dividendInBaseCurrency:
- (historicalDataItem.dividendInBaseCurrency ?? 0) -
+ historicalDataItem.dividendInBaseCurrency -
dividendInBaseCurrencyAtStartDate,
netPerformance:
historicalDataItem.netPerformance - netPerformanceAtStartDate,
diff --git a/apps/api/src/app/portfolio/portfolio.service.spec.ts b/apps/api/src/app/portfolio/portfolio.service.spec.ts
index a043a10309..12d2b3147e 100644
--- a/apps/api/src/app/portfolio/portfolio.service.spec.ts
+++ b/apps/api/src/app/portfolio/portfolio.service.spec.ts
@@ -1241,6 +1241,8 @@ describe('PortfolioService', () => {
getLiabilitiesInBaseCurrency: jest.fn().mockResolvedValue(new Big(0)),
getSnapshot: jest.fn().mockResolvedValue({
currentValueInBaseCurrency: new Big(3000),
+ dividendYieldPercent: new Big(0),
+ dividendYieldPercentWithCurrencyEffect: new Big(0),
totalCashInBaseCurrency: new Big(1000),
totalInvestment: new Big(2000),
totalInvestmentWithCurrencyEffect: new Big(2000)
diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts
index ebd05805f5..a8a3fc6740 100644
--- a/apps/api/src/app/portfolio/portfolio.service.ts
+++ b/apps/api/src/app/portfolio/portfolio.service.ts
@@ -996,15 +996,12 @@ export class PortfolioService {
const {
activitiesCount,
- averageInvestment,
- averageInvestmentWithCurrencyEffect,
averagePrice,
currency,
dateOfFirstActivity,
dividendInBaseCurrency,
- dividendYieldPercent: dividendYieldPercentOfSnapshot,
- dividendYieldPercentWithCurrencyEffect:
- dividendYieldPercentWithCurrencyEffectOfSnapshot,
+ dividendYieldPercent,
+ dividendYieldPercentWithCurrencyEffect,
feeInBaseCurrency,
grossPerformance,
grossPerformancePercentage,
@@ -1020,34 +1017,6 @@ export class PortfolioService {
tags
} = holding;
- // TODO: Remove the block below with the next release, when each cached
- // portfolio snapshot contains the dividend yield. Then take
- // dividendYieldPercent and dividendYieldPercentWithCurrencyEffect
- // directly from the holding and remove averageInvestment and
- // averageInvestmentWithCurrencyEffect from the properties above
- const daysInMarket = differenceInDays(
- new Date(),
- parseDate(dateOfFirstActivity)
- );
-
- const dividendYieldPercent =
- dividendYieldPercentOfSnapshot ??
- getAnnualizedPerformancePercent({
- daysInMarket,
- netPerformancePercentage: averageInvestment.eq(0)
- ? new Big(0)
- : dividendInBaseCurrency.div(averageInvestment)
- });
-
- const dividendYieldPercentWithCurrencyEffect =
- dividendYieldPercentWithCurrencyEffectOfSnapshot ??
- getAnnualizedPerformancePercent({
- daysInMarket,
- netPerformancePercentage: averageInvestmentWithCurrencyEffect.eq(0)
- ? new Big(0)
- : dividendInBaseCurrency.div(averageInvestmentWithCurrencyEffect)
- });
-
const activitiesOfHolding = activities.filter((activity) => {
return (
activity.assetProfile.dataSource === dataSource &&
@@ -2323,11 +2292,9 @@ export class PortfolioService {
activityCount: activities.filter(({ type }) => {
return ['BUY', 'SELL'].includes(type);
}).length,
- // TODO: Remove the fallback to 0 with the next release, when each
- // cached portfolio snapshot contains the dividend yield
- dividendYieldPercent: dividendYieldPercent?.toNumber() ?? 0,
+ dividendYieldPercent: dividendYieldPercent.toNumber(),
dividendYieldPercentWithCurrencyEffect:
- dividendYieldPercentWithCurrencyEffect?.toNumber() ?? 0,
+ dividendYieldPercentWithCurrencyEffect.toNumber(),
emergencyFund: {
assets: emergencyFundHoldingsValueInBaseCurrency,
cash: totalEmergencyFund
diff --git a/libs/common/src/lib/models/portfolio-snapshot-holding.ts b/libs/common/src/lib/models/portfolio-snapshot-holding.ts
index 9b440b9782..e248cd997d 100644
--- a/libs/common/src/lib/models/portfolio-snapshot-holding.ts
+++ b/libs/common/src/lib/models/portfolio-snapshot-holding.ts
@@ -7,41 +7,16 @@ import type { Tag } from '@ghostfolio/prisma/browser';
import { DataSource } from '@ghostfolio/prisma/enums';
import { Big } from 'big.js';
-import { Expose, Transform, Type } from 'class-transformer';
-
-/**
- * @deprecated Backward compatibility to read the portfolio snapshots which
- * have been cached with the former name of the property
- *
- * TODO: Remove the fallback with the next release, together with the
- * `@Expose()` decorators of `averageInvestment` and
- * `averageInvestmentWithCurrencyEffect`
- */
-function transformToBigWithFallback(formerKey: string) {
- return ({ obj, value }: { obj: Record<string, string>; value: string }) => {
- const valueOrFallback = value ?? obj[formerKey];
-
- return valueOrFallback === undefined
- ? undefined
- : transformToBig({ value: valueOrFallback });
- };
-}
+import { Transform, Type } from 'class-transformer';
export class PortfolioSnapshotHolding {
activitiesCount: number;
- @Expose()
- @Transform(transformToBigWithFallback('timeWeightedInvestment'), {
- toClassOnly: true
- })
+ @Transform(transformToBig, { toClassOnly: true })
@Type(() => Big)
averageInvestment: Big;
- @Expose()
- @Transform(
- transformToBigWithFallback('timeWeightedInvestmentWithCurrencyEffect'),
- { toClassOnly: true }
- )
+ @Transform(transformToBig, { toClassOnly: true })
@Type(() => Big)
averageInvestmentWithCurrencyEffect: Big;