From bce18f7261e29e4834fb9128ca993a7c75e6fd5b Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:05:06 +0200 Subject: [PATCH] Feature/reuse advanced markets calculation in portfolio details endpoint (#3884) --- .../src/app/portfolio/portfolio.controller.ts | 60 ++++++++++++++++--- .../allocations/allocations-page.component.ts | 40 +++---------- .../interfaces/portfolio-details.interface.ts | 2 +- 3 files changed, 60 insertions(+), 42 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio.controller.ts b/apps/api/src/app/portfolio/portfolio.controller.ts index aa3d23644..ff0c31060 100644 --- a/apps/api/src/app/portfolio/portfolio.controller.ts +++ b/apps/api/src/app/portfolio/portfolio.controller.ts @@ -98,15 +98,22 @@ export class PortfolioController { filterByTags }); - const { accounts, hasErrors, holdings, markets, platforms, summary } = - await this.portfolioService.getDetails({ - dateRange, - filters, - impersonationId, - withMarkets, - userId: this.request.user.id, - withSummary: true - }); + const { + accounts, + hasErrors, + holdings, + markets, + marketsAdvanced, + platforms, + summary + } = await this.portfolioService.getDetails({ + dateRange, + filters, + impersonationId, + withMarkets, + userId: this.request.user.id, + withSummary: true + }); if (hasErrors || hasNotDefinedValuesInObject(holdings)) { hasError = true; @@ -168,6 +175,9 @@ export class PortfolioController { Object.values(markets).forEach((market) => { delete market.valueInBaseCurrency; }); + Object.values(marketsAdvanced).forEach((market) => { + delete market.valueInBaseCurrency; + }); portfolioSummary = nullifyValuesInObject(summary, [ 'cash', @@ -241,6 +251,38 @@ export class PortfolioController { valueInPercentage: 0 } }, + marketsAdvanced: hasDetails + ? marketsAdvanced + : { + [UNKNOWN_KEY]: { + id: UNKNOWN_KEY, + valueInPercentage: 0 + }, + asiaPacific: { + id: 'asiaPacific', + valueInPercentage: 0 + }, + emergingMarkets: { + id: 'emergingMarkets', + valueInPercentage: 0 + }, + europe: { + id: 'europe', + valueInPercentage: 0 + }, + japan: { + id: 'japan', + valueInPercentage: 0 + }, + northAmerica: { + id: 'northAmerica', + valueInPercentage: 0 + }, + otherMarkets: { + id: 'otherMarkets', + valueInPercentage: 0 + } + }, summary: portfolioSummary }; } diff --git a/apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts b/apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts index d7502cbfa..9b0384d39 100644 --- a/apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts +++ b/apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts @@ -302,6 +302,14 @@ export class AllocationsPageComponent implements OnDestroy, OnInit { this.markets = this.portfolioDetails.markets; + Object.values(this.portfolioDetails.marketsAdvanced).forEach( + ({ id, valueInBaseCurrency, valueInPercentage }) => { + this.marketsAdvanced[id].value = isNumber(valueInBaseCurrency) + ? valueInBaseCurrency + : valueInPercentage; + } + ); + for (const [symbol, position] of Object.entries( this.portfolioDetails.holdings )) { @@ -332,32 +340,6 @@ export class AllocationsPageComponent implements OnDestroy, OnInit { // Prepare analysis data by continents, countries, holdings and sectors except for liquidity if (position.countries.length > 0) { - this.marketsAdvanced.asiaPacific.value += - position.marketsAdvanced.asiaPacific * - (isNumber(position.valueInBaseCurrency) - ? position.valueInBaseCurrency - : position.valueInPercentage); - this.marketsAdvanced.emergingMarkets.value += - position.marketsAdvanced.emergingMarkets * - (isNumber(position.valueInBaseCurrency) - ? position.valueInBaseCurrency - : position.valueInPercentage); - this.marketsAdvanced.europe.value += - position.marketsAdvanced.europe * - (isNumber(position.valueInBaseCurrency) - ? position.valueInBaseCurrency - : position.valueInPercentage); - this.marketsAdvanced.japan.value += - position.marketsAdvanced.japan * - (isNumber(position.valueInBaseCurrency) - ? position.valueInBaseCurrency - : position.valueInPercentage); - this.marketsAdvanced.northAmerica.value += - position.marketsAdvanced.northAmerica * - (isNumber(position.valueInBaseCurrency) - ? position.valueInBaseCurrency - : position.valueInPercentage); - for (const country of position.countries) { const { code, continent, name, weight } = country; @@ -407,12 +389,6 @@ export class AllocationsPageComponent implements OnDestroy, OnInit { ) ? this.portfolioDetails.holdings[symbol].valueInBaseCurrency : this.portfolioDetails.holdings[symbol].valueInPercentage; - - this.marketsAdvanced[UNKNOWN_KEY].value += isNumber( - position.valueInBaseCurrency - ) - ? this.portfolioDetails.holdings[symbol].valueInBaseCurrency - : this.portfolioDetails.holdings[symbol].valueInPercentage; } if (position.holdings.length > 0) { diff --git a/libs/common/src/lib/interfaces/portfolio-details.interface.ts b/libs/common/src/lib/interfaces/portfolio-details.interface.ts index 711ae424f..e455f73ca 100644 --- a/libs/common/src/lib/interfaces/portfolio-details.interface.ts +++ b/libs/common/src/lib/interfaces/portfolio-details.interface.ts @@ -25,7 +25,7 @@ export interface PortfolioDetails { marketsAdvanced?: { [key in MarketAdvanced]: { id: MarketAdvanced; - valueInBaseCurrency: number; + valueInBaseCurrency?: number; valueInPercentage: number; }; };