From e91ee1a31fd0956ddd92587e9dc9bc7f9bcaead7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20G=C3=B3mez?= Date: Tue, 13 Aug 2024 16:18:34 +0200 Subject: [PATCH] Round sumWeights (#547) --- src/portfolios/domain/portfolio-states.service.spec.ts | 7 ++++--- src/portfolios/domain/portfolio-states.service.ts | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/portfolios/domain/portfolio-states.service.spec.ts b/src/portfolios/domain/portfolio-states.service.spec.ts index f2f3bbf8..2a0a2df8 100644 --- a/src/portfolios/domain/portfolio-states.service.spec.ts +++ b/src/portfolios/domain/portfolio-states.service.spec.ts @@ -4,6 +4,7 @@ import { TimeRange } from './entities/time-range.enum'; import { portfolioFactory } from './entities/__tests__/portfolio.factory'; import { positionFactory } from './entities/__tests__/position.factory'; import { PortfolioStatesService } from './portfolio-states.service'; +import { round } from 'lodash'; describe('PortfolioStatesService', () => { const portfolioStatesRepository = jest.mocked({ @@ -29,9 +30,9 @@ describe('PortfolioStatesService', () => { (sum, pos) => sum + pos.value, portfolio.cash, ); - const sumWeights = positions.reduce( - (acc, pos) => acc + pos.targetWeight, - 0, + const sumWeights = round( + positions.reduce((acc, pos) => acc + pos.targetWeight, 0), + 2, ); await service.createPortfolioState(portfolio, positions); diff --git a/src/portfolios/domain/portfolio-states.service.ts b/src/portfolios/domain/portfolio-states.service.ts index ff524c98..7ee9e4de 100644 --- a/src/portfolios/domain/portfolio-states.service.ts +++ b/src/portfolios/domain/portfolio-states.service.ts @@ -7,13 +7,17 @@ import { PortfolioState } from './entities/portfolio-state.entity'; import { Portfolio } from './entities/portfolio.entity'; import { Position } from './entities/position.entity'; import { TimeRange } from './entities/time-range.enum'; +import { round } from 'lodash'; @Injectable() export class PortfolioStatesService { constructor(private readonly repository: PortfolioStatesRepository) {} async createPortfolioState(portfolio: Portfolio, positions: Position[]) { - const sumWeights = positions.reduce((acc, p) => acc + p.targetWeight, 0); + const sumWeights = round( + positions.reduce((acc, p) => acc + p.targetWeight, 0), + 2, + ); const cash = portfolio.cash ?? 0; const totalValueEUR = positions.reduce((sum, pos) => sum + pos.value, cash); const contributionsAmount = portfolio.contributions