From 4bf3a778dd7328a067b721b63b251953973cdcb1 Mon Sep 17 00:00:00 2001 From: Reto Kaul Date: Sat, 17 Aug 2024 21:00:58 +0200 Subject: [PATCH 1/3] Use snapshot instead of getChart() --- .../calculator/portfolio-calculator.ts | 364 ------------------ .../src/app/portfolio/portfolio.service.ts | 10 +- 2 files changed, 7 insertions(+), 367 deletions(-) diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index 812b14d6e..e9c8a28bf 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -598,370 +598,6 @@ export abstract class PortfolioCalculator { }; } - public async getChart({ - dateRange = 'max', - withDataDecimation = true - }: { - dateRange?: DateRange; - withDataDecimation?: boolean; - }): Promise { - if (this.getTransactionPoints().length === 0) { - return []; - } - - const { endDate, startDate } = getIntervalFromDateRange( - dateRange, - this.getStartDate() - ); - - const daysInMarket = differenceInDays(endDate, startDate) + 1; - const step = withDataDecimation - ? Math.round(daysInMarket / Math.min(daysInMarket, MAX_CHART_ITEMS)) - : 1; - - const chartData = await this.getChartData({ - step, - end: endDate, - start: startDate - }); - - return chartData; - } - - public async getChartData({ - end = new Date(), - start, - step = 1 - }: { - end?: Date; - start: Date; - step?: number; - }): Promise { - const symbols: { [symbol: string]: boolean } = {}; - - const transactionPointsBeforeEndDate = - this.transactionPoints?.filter((transactionPoint) => { - return isBefore(parseDate(transactionPoint.date), end); - }) ?? []; - - const currencies: { [symbol: string]: string } = {}; - const dataGatheringItems: IDataGatheringItem[] = []; - const firstIndex = transactionPointsBeforeEndDate.length; - - if (transactionPointsBeforeEndDate.length > 0) { - for (const { - currency, - dataSource, - symbol - } of transactionPointsBeforeEndDate[firstIndex - 1].items) { - dataGatheringItems.push({ - dataSource, - symbol - }); - currencies[symbol] = currency; - symbols[symbol] = true; - } - } - - const { dataProviderInfos, values: marketSymbols } = - await this.currentRateService.getValues({ - dataGatheringItems, - dateQuery: { - gte: start, - lt: end - } - }); - - this.dataProviderInfos = dataProviderInfos; - - const marketSymbolMap: { - [date: string]: { [symbol: string]: Big }; - } = {}; - - let exchangeRatesByCurrency = - await this.exchangeRateDataService.getExchangeRatesByCurrency({ - currencies: uniq(Object.values(currencies)), - endDate: endOfDay(end), - startDate: this.getStartDate(), - targetCurrency: this.currency - }); - - for (const marketSymbol of marketSymbols) { - const dateString = format(marketSymbol.date, DATE_FORMAT); - if (!marketSymbolMap[dateString]) { - marketSymbolMap[dateString] = {}; - } - if (marketSymbol.marketPrice) { - marketSymbolMap[dateString][marketSymbol.symbol] = new Big( - marketSymbol.marketPrice - ); - } - } - - const accumulatedValuesByDate: { - [date: string]: { - investmentValueWithCurrencyEffect: Big; - totalCurrentValue: Big; - totalCurrentValueWithCurrencyEffect: Big; - totalAccountBalanceWithCurrencyEffect: Big; - totalInvestmentValue: Big; - totalInvestmentValueWithCurrencyEffect: Big; - totalNetPerformanceValue: Big; - totalNetPerformanceValueWithCurrencyEffect: Big; - totalTimeWeightedInvestmentValue: Big; - totalTimeWeightedInvestmentValueWithCurrencyEffect: Big; - }; - } = {}; - - const valuesBySymbol: { - [symbol: string]: { - currentValues: { [date: string]: Big }; - currentValuesWithCurrencyEffect: { [date: string]: Big }; - investmentValuesAccumulated: { [date: string]: Big }; - investmentValuesAccumulatedWithCurrencyEffect: { [date: string]: Big }; - investmentValuesWithCurrencyEffect: { [date: string]: Big }; - netPerformanceValues: { [date: string]: Big }; - netPerformanceValuesWithCurrencyEffect: { [date: string]: Big }; - timeWeightedInvestmentValues: { [date: string]: Big }; - timeWeightedInvestmentValuesWithCurrencyEffect: { [date: string]: Big }; - }; - } = {}; - - let chartDateMap = this.getChartDateMap({ - step, - endDate: end, - startDate: start - }); - - const chartDates = sortBy(Object.keys(chartDateMap), (chartDate) => { - return chartDate; - }); - - for (const symbol of Object.keys(symbols)) { - const { - currentValues, - currentValuesWithCurrencyEffect, - investmentValuesAccumulated, - investmentValuesAccumulatedWithCurrencyEffect, - investmentValuesWithCurrencyEffect, - netPerformanceValues, - netPerformanceValuesWithCurrencyEffect, - timeWeightedInvestmentValues, - timeWeightedInvestmentValuesWithCurrencyEffect - } = this.getSymbolMetrics({ - chartDateMap, - end, - marketSymbolMap, - start, - symbol, - dataSource: null, - exchangeRates: - exchangeRatesByCurrency[`${currencies[symbol]}${this.currency}`], - isChartMode: true - }); - - valuesBySymbol[symbol] = { - currentValues, - currentValuesWithCurrencyEffect, - investmentValuesAccumulated, - investmentValuesAccumulatedWithCurrencyEffect, - investmentValuesWithCurrencyEffect, - netPerformanceValues, - netPerformanceValuesWithCurrencyEffect, - timeWeightedInvestmentValues, - timeWeightedInvestmentValuesWithCurrencyEffect - }; - } - - let lastDate = format(this.startDate, DATE_FORMAT); - - for (const dateString of chartDates) { - accumulatedValuesByDate[dateString] = { - investmentValueWithCurrencyEffect: new Big(0), - totalAccountBalanceWithCurrencyEffect: new Big(0), - totalCurrentValue: new Big(0), - totalCurrentValueWithCurrencyEffect: new Big(0), - totalInvestmentValue: new Big(0), - totalInvestmentValueWithCurrencyEffect: new Big(0), - totalNetPerformanceValue: new Big(0), - totalNetPerformanceValueWithCurrencyEffect: new Big(0), - totalTimeWeightedInvestmentValue: new Big(0), - totalTimeWeightedInvestmentValueWithCurrencyEffect: new Big(0) - }; - - for (const symbol of Object.keys(valuesBySymbol)) { - const symbolValues = valuesBySymbol[symbol]; - - const currentValue = - symbolValues.currentValues?.[dateString] ?? new Big(0); - - const currentValueWithCurrencyEffect = - symbolValues.currentValuesWithCurrencyEffect?.[dateString] ?? - new Big(0); - - const investmentValueAccumulated = - symbolValues.investmentValuesAccumulated?.[dateString] ?? new Big(0); - - const investmentValueAccumulatedWithCurrencyEffect = - symbolValues.investmentValuesAccumulatedWithCurrencyEffect?.[ - dateString - ] ?? new Big(0); - - const investmentValueWithCurrencyEffect = - symbolValues.investmentValuesWithCurrencyEffect?.[dateString] ?? - new Big(0); - - const netPerformanceValue = - symbolValues.netPerformanceValues?.[dateString] ?? new Big(0); - - const netPerformanceValueWithCurrencyEffect = - symbolValues.netPerformanceValuesWithCurrencyEffect?.[dateString] ?? - new Big(0); - - const timeWeightedInvestmentValue = - symbolValues.timeWeightedInvestmentValues?.[dateString] ?? new Big(0); - - const timeWeightedInvestmentValueWithCurrencyEffect = - symbolValues.timeWeightedInvestmentValuesWithCurrencyEffect?.[ - dateString - ] ?? new Big(0); - - accumulatedValuesByDate[dateString].investmentValueWithCurrencyEffect = - accumulatedValuesByDate[ - dateString - ].investmentValueWithCurrencyEffect.add( - investmentValueWithCurrencyEffect - ); - - accumulatedValuesByDate[dateString].totalCurrentValue = - accumulatedValuesByDate[dateString].totalCurrentValue.add( - currentValue - ); - - accumulatedValuesByDate[ - dateString - ].totalCurrentValueWithCurrencyEffect = accumulatedValuesByDate[ - dateString - ].totalCurrentValueWithCurrencyEffect.add( - currentValueWithCurrencyEffect - ); - - accumulatedValuesByDate[dateString].totalInvestmentValue = - accumulatedValuesByDate[dateString].totalInvestmentValue.add( - investmentValueAccumulated - ); - - accumulatedValuesByDate[ - dateString - ].totalInvestmentValueWithCurrencyEffect = accumulatedValuesByDate[ - dateString - ].totalInvestmentValueWithCurrencyEffect.add( - investmentValueAccumulatedWithCurrencyEffect - ); - - accumulatedValuesByDate[dateString].totalNetPerformanceValue = - accumulatedValuesByDate[dateString].totalNetPerformanceValue.add( - netPerformanceValue - ); - - accumulatedValuesByDate[ - dateString - ].totalNetPerformanceValueWithCurrencyEffect = accumulatedValuesByDate[ - dateString - ].totalNetPerformanceValueWithCurrencyEffect.add( - netPerformanceValueWithCurrencyEffect - ); - - accumulatedValuesByDate[dateString].totalTimeWeightedInvestmentValue = - accumulatedValuesByDate[ - dateString - ].totalTimeWeightedInvestmentValue.add(timeWeightedInvestmentValue); - - accumulatedValuesByDate[ - dateString - ].totalTimeWeightedInvestmentValueWithCurrencyEffect = - accumulatedValuesByDate[ - dateString - ].totalTimeWeightedInvestmentValueWithCurrencyEffect.add( - timeWeightedInvestmentValueWithCurrencyEffect - ); - } - - if ( - this.accountBalanceItems.some(({ date }) => { - return date === dateString; - }) - ) { - accumulatedValuesByDate[ - dateString - ].totalAccountBalanceWithCurrencyEffect = new Big( - this.accountBalanceItems.find(({ date }) => { - return date === dateString; - }).value - ); - } else { - accumulatedValuesByDate[ - dateString - ].totalAccountBalanceWithCurrencyEffect = - accumulatedValuesByDate[lastDate] - ?.totalAccountBalanceWithCurrencyEffect ?? new Big(0); - } - - lastDate = dateString; - } - - return Object.entries(accumulatedValuesByDate).map(([date, values]) => { - const { - investmentValueWithCurrencyEffect, - totalAccountBalanceWithCurrencyEffect, - totalCurrentValue, - totalCurrentValueWithCurrencyEffect, - totalInvestmentValue, - totalInvestmentValueWithCurrencyEffect, - totalNetPerformanceValue, - totalNetPerformanceValueWithCurrencyEffect, - totalTimeWeightedInvestmentValue, - totalTimeWeightedInvestmentValueWithCurrencyEffect - } = values; - - const netPerformanceInPercentage = totalTimeWeightedInvestmentValue.eq(0) - ? 0 - : totalNetPerformanceValue - .div(totalTimeWeightedInvestmentValue) - .mul(100) - .toNumber(); - - const netPerformanceInPercentageWithCurrencyEffect = - totalTimeWeightedInvestmentValueWithCurrencyEffect.eq(0) - ? 0 - : totalNetPerformanceValueWithCurrencyEffect - .div(totalTimeWeightedInvestmentValueWithCurrencyEffect) - .mul(100) - .toNumber(); - - return { - date, - netPerformanceInPercentage, - netPerformanceInPercentageWithCurrencyEffect, - investmentValueWithCurrencyEffect: - investmentValueWithCurrencyEffect.toNumber(), - netPerformance: totalNetPerformanceValue.toNumber(), - netPerformanceWithCurrencyEffect: - totalNetPerformanceValueWithCurrencyEffect.toNumber(), - // TODO: Add valuables - netWorth: totalCurrentValueWithCurrencyEffect - .plus(totalAccountBalanceWithCurrencyEffect) - .toNumber(), - totalAccountBalance: totalAccountBalanceWithCurrencyEffect.toNumber(), - totalInvestment: totalInvestmentValue.toNumber(), - totalInvestmentValueWithCurrencyEffect: - totalInvestmentValueWithCurrencyEffect.toNumber(), - value: totalCurrentValue.toNumber(), - valueWithCurrencyEffect: totalCurrentValueWithCurrencyEffect.toNumber() - }; - }); - } - public getDataProviderInfos() { return this.dataProviderInfos; } diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 789b3a979..d00b25be3 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -67,6 +67,7 @@ import { differenceInDays, format, isAfter, + isBefore, isSameMonth, isSameYear, parseISO, @@ -244,6 +245,8 @@ export class PortfolioService { }): Promise { const userId = await this.getUserId(impersonationId, this.request.user.id); + const { endDate, startDate } = getIntervalFromDateRange(dateRange); + const { activities } = await this.orderService.getOrders({ filters, userId, @@ -269,9 +272,10 @@ export class PortfolioService { this.request.user.Settings.settings.isExperimentalFeatures }); - const items = await portfolioCalculator.getChart({ - dateRange, - withDataDecimation: false + const { historicalData } = await portfolioCalculator.getSnapshot(); + + const items = historicalData.filter(({ date }) => { + return !isBefore(date, startDate) && !isAfter(date, endDate); }); let investments: InvestmentItem[]; From f9a4bb7c58d73c159b2942ac0bd30230473f2530 Mon Sep 17 00:00:00 2001 From: Reto Kaul Date: Sat, 17 Aug 2024 21:28:33 +0200 Subject: [PATCH 2/3] Fix tests --- .../calculator/portfolio-calculator.ts | 24 ++++++++++++++++++- ...aln-buy-and-sell-in-two-activities.spec.ts | 6 +---- ...folio-calculator-baln-buy-and-sell.spec.ts | 6 +---- .../twr/portfolio-calculator-baln-buy.spec.ts | 6 +---- ...ator-btcusd-buy-and-sell-partially.spec.ts | 6 +---- .../portfolio-calculator-googl-buy.spec.ts | 6 +---- .../portfolio-calculator-no-orders.spec.ts | 6 +---- ...ulator-novn-buy-and-sell-partially.spec.ts | 6 +---- ...folio-calculator-novn-buy-and-sell.spec.ts | 14 +++++------ 9 files changed, 37 insertions(+), 43 deletions(-) diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index e9c8a28bf..b14134c99 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -288,6 +288,7 @@ export abstract class PortfolioCalculator { const accumulatedValuesByDate: { [date: string]: { investmentValueWithCurrencyEffect: Big; + totalAccountBalanceWithCurrencyEffect: Big; totalCurrentValue: Big; totalCurrentValueWithCurrencyEffect: Big; totalInvestmentValue: Big; @@ -458,6 +459,8 @@ export abstract class PortfolioCalculator { } } + let lastDate = chartDates[0]; + for (const dateString of chartDates) { for (const symbol of Object.keys(valuesBySymbol)) { const symbolValues = valuesBySymbol[symbol]; @@ -501,6 +504,18 @@ export abstract class PortfolioCalculator { accumulatedValuesByDate[dateString] ?.investmentValueWithCurrencyEffect ?? new Big(0) ).add(investmentValueWithCurrencyEffect), + totalAccountBalanceWithCurrencyEffect: this.accountBalanceItems.some( + ({ date }) => { + return date === dateString; + } + ) + ? new Big( + this.accountBalanceItems.find(({ date }) => { + return date === dateString; + }).value + ) + : (accumulatedValuesByDate[lastDate] + ?.totalAccountBalanceWithCurrencyEffect ?? new Big(0)), totalCurrentValue: ( accumulatedValuesByDate[dateString]?.totalCurrentValue ?? new Big(0) ).add(currentValue), @@ -534,6 +549,8 @@ export abstract class PortfolioCalculator { ).add(timeWeightedInvestmentValueWithCurrencyEffect) }; } + + lastDate = dateString; } const historicalData: HistoricalDataItem[] = Object.entries( @@ -541,6 +558,7 @@ export abstract class PortfolioCalculator { ).map(([date, values]) => { const { investmentValueWithCurrencyEffect, + totalAccountBalanceWithCurrencyEffect, totalCurrentValue, totalCurrentValueWithCurrencyEffect, totalInvestmentValue, @@ -575,7 +593,11 @@ export abstract class PortfolioCalculator { netPerformance: totalNetPerformanceValue.toNumber(), netPerformanceWithCurrencyEffect: totalNetPerformanceValueWithCurrencyEffect.toNumber(), - netWorth: 0, // TODO + // TODO: Add valuables + netWorth: totalCurrentValueWithCurrencyEffect + .plus(totalAccountBalanceWithCurrencyEffect) + .toNumber(), + totalAccountBalance: totalAccountBalanceWithCurrencyEffect.toNumber(), totalInvestment: totalInvestmentValue.toNumber(), totalInvestmentValueWithCurrencyEffect: totalInvestmentValueWithCurrencyEffect.toNumber(), diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts index 5c63cb58d..e143e1ae6 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts @@ -125,16 +125,12 @@ describe('PortfolioCalculator', () => { userId: userDummyData.id }); - const chartData = await portfolioCalculator.getChartData({ - start: parseDate('2021-11-22') - }); - const portfolioSnapshot = await portfolioCalculator.getSnapshot(); const investments = portfolioCalculator.getInvestments(); const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ - data: chartData, + data: portfolioSnapshot.historicalData, groupBy: 'month' }); diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts index 5ce1dd80b..912776311 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts @@ -110,16 +110,12 @@ describe('PortfolioCalculator', () => { userId: userDummyData.id }); - const chartData = await portfolioCalculator.getChartData({ - start: parseDate('2021-11-22') - }); - const portfolioSnapshot = await portfolioCalculator.getSnapshot(); const investments = portfolioCalculator.getInvestments(); const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ - data: chartData, + data: portfolioSnapshot.historicalData, groupBy: 'month' }); diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts index a71342c5a..a584d6809 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts @@ -95,16 +95,12 @@ describe('PortfolioCalculator', () => { userId: userDummyData.id }); - const chartData = await portfolioCalculator.getChartData({ - start: parseDate('2021-11-30') - }); - const portfolioSnapshot = await portfolioCalculator.getSnapshot(); const investments = portfolioCalculator.getInvestments(); const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ - data: chartData, + data: portfolioSnapshot.historicalData, groupBy: 'month' }); diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts index 0ce9d95a4..4a16cc86d 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts @@ -124,16 +124,12 @@ describe.skip('PortfolioCalculator', () => { userId: userDummyData.id }); - const chartData = await portfolioCalculator.getChartData({ - start: parseDate('2015-01-01') - }); - const portfolioSnapshot = await portfolioCalculator.getSnapshot(); const investments = portfolioCalculator.getInvestments(); const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ - data: chartData, + data: portfolioSnapshot.historicalData, groupBy: 'month' }); diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts index 74d91f9b7..1177fb461 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts @@ -108,16 +108,12 @@ describe('PortfolioCalculator', () => { userId: userDummyData.id }); - const chartData = await portfolioCalculator.getChartData({ - start: parseDate('2023-01-03') - }); - const portfolioSnapshot = await portfolioCalculator.getSnapshot(); const investments = portfolioCalculator.getInvestments(); const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ - data: chartData, + data: portfolioSnapshot.historicalData, groupBy: 'month' }); diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts index 564527cbd..19994f4ee 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts @@ -73,16 +73,12 @@ describe('PortfolioCalculator', () => { userId: userDummyData.id }); - const start = subDays(new Date(), 10); - - const chartData = await portfolioCalculator.getChartData({ start }); - const portfolioSnapshot = await portfolioCalculator.getSnapshot(); const investments = portfolioCalculator.getInvestments(); const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ - data: chartData, + data: portfolioSnapshot.historicalData, groupBy: 'month' }); diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts index 3157c9171..4770e2ce7 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts @@ -110,16 +110,12 @@ describe('PortfolioCalculator', () => { userId: userDummyData.id }); - const chartData = await portfolioCalculator.getChartData({ - start: parseDate('2022-03-07') - }); - const portfolioSnapshot = await portfolioCalculator.getSnapshot(); const investments = portfolioCalculator.getInvestments(); const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ - data: chartData, + data: portfolioSnapshot.historicalData, groupBy: 'month' }); diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts index 828cbc55e..992489bf9 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts @@ -110,20 +110,16 @@ describe('PortfolioCalculator', () => { userId: userDummyData.id }); - const chartData = await portfolioCalculator.getChartData({ - start: parseDate('2022-03-07') - }); - const portfolioSnapshot = await portfolioCalculator.getSnapshot(); const investments = portfolioCalculator.getInvestments(); const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ - data: chartData, + data: portfolioSnapshot.historicalData, groupBy: 'month' }); - expect(chartData[0]).toEqual({ + expect(portfolioSnapshot.historicalData[0]).toEqual({ date: '2022-03-07', investmentValueWithCurrencyEffect: 151.6, netPerformance: 0, @@ -138,7 +134,11 @@ describe('PortfolioCalculator', () => { valueWithCurrencyEffect: 151.6 }); - expect(chartData[chartData.length - 1]).toEqual({ + expect( + portfolioSnapshot.historicalData[ + portfolioSnapshot.historicalData.length - 1 + ] + ).toEqual({ date: '2022-04-11', investmentValueWithCurrencyEffect: 0, netPerformance: 19.86, From d4cd7a1af69e72df08a3a8e507d290f4a41c39d1 Mon Sep 17 00:00:00 2001 From: Reto Kaul Date: Sun, 18 Aug 2024 08:49:57 +0200 Subject: [PATCH 3/3] Fix test --- .../calculator/twr/portfolio-calculator-no-orders.spec.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts index 19994f4ee..67f69d1d2 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts @@ -104,12 +104,7 @@ describe('PortfolioCalculator', () => { expect(investments).toEqual([]); - expect(investmentsByMonth).toEqual([ - { - date: '2021-12-01', - investment: 0 - } - ]); + expect(investmentsByMonth).toEqual([]); }); }); });