From cef7fa79de262a354c4cb07ac643e9481fd22ca3 Mon Sep 17 00:00:00 2001 From: Hugo Persson Date: Fri, 28 Jul 2023 19:42:57 +0200 Subject: [PATCH] Fix total account value calculation for liabilities (#2184) * Fix calculation * Update changelog --------- Co-authored-by: Thomas <4159106+dtslvr@users.noreply.github.com> --- CHANGELOG.md | 6 ++++++ apps/api/src/app/portfolio/portfolio.service.ts | 17 +++++++++-------- .../activities-table.component.ts | 13 +++++++------ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f13cc6b..3c8e7f13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Fixed + +- Considered liabilities in the total account value calculation + ## 1.293.0 - 2023-07-26 ### Added diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 859a2dd1..ba77354b 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -1830,12 +1830,12 @@ export class PortfolioService { userId: string; withExcludedAccounts?: boolean; }) { - const ordersOfTypeItem = await this.orderService.getOrders({ + const ordersOfTypeItemOrLiability = await this.orderService.getOrders({ filters, userCurrency, userId, withExcludedAccounts, - types: ['ITEM'] + types: ['ITEM', 'LIABILITY'] }); const accounts: PortfolioDetails['accounts'] = {}; @@ -1875,13 +1875,14 @@ export class PortfolioService { return accountId === account.id; }); - const ordersOfTypeItemByAccount = ordersOfTypeItem.filter( - ({ accountId }) => { + const ordersOfTypeItemOrLiabilityByAccount = + ordersOfTypeItemOrLiability.filter(({ accountId }) => { return accountId === account.id; - } - ); + }); - ordersByAccount = ordersByAccount.concat(ordersOfTypeItemByAccount); + ordersByAccount = ordersByAccount.concat( + ordersOfTypeItemOrLiabilityByAccount + ); accounts[account.id] = { balance: account.balance, @@ -1921,7 +1922,7 @@ export class PortfolioService { order.unitPrice ?? 0); - if (order.type === 'SELL') { + if (order.type === 'LIABILITY' || order.type === 'SELL') { currentValueOfSymbolInBaseCurrency *= -1; } diff --git a/libs/ui/src/lib/activities-table/activities-table.component.ts b/libs/ui/src/lib/activities-table/activities-table.component.ts index 743747fb..c91404b6 100644 --- a/libs/ui/src/lib/activities-table/activities-table.component.ts +++ b/libs/ui/src/lib/activities-table/activities-table.component.ts @@ -383,13 +383,14 @@ export class ActivitiesTableComponent implements OnChanges, OnDestroy, OnInit { } private getTotalValue() { - let totalValue = new Big(0); const paginatedData = this.getPaginatedData(); - for (const activity of paginatedData) { - if (isNumber(activity.valueInBaseCurrency)) { - if (activity.type === 'BUY' || activity.type === 'ITEM') { - totalValue = totalValue.plus(activity.valueInBaseCurrency); - } else if (activity.type === 'SELL') { + let totalValue = new Big(0); + + for (const { type, valueInBaseCurrency } of paginatedData) { + if (isNumber(valueInBaseCurrency)) { + if (type === 'BUY' || type === 'ITEM') { + totalValue = totalValue.plus(valueInBaseCurrency); + } else if (type === 'LIABILITY' || type === 'SELL') { return null; } } else {