From a0036bae8559edd073c8aa701c1f3a50f5e04c7f Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 6 Oct 2024 09:44:27 +0200 Subject: [PATCH] Reuse markets calculation in public portfolio endpoint --- .../app/endpoints/public/public.controller.ts | 7 ++- .../app/pages/public/public-page.component.ts | 46 +------------------ .../src/app/pages/public/public-page.html | 6 +-- .../public-portfolio-response.interface.ts | 9 +++- 4 files changed, 19 insertions(+), 49 deletions(-) diff --git a/apps/api/src/app/endpoints/public/public.controller.ts b/apps/api/src/app/endpoints/public/public.controller.ts index b0f7bb2c3..9399f97bf 100644 --- a/apps/api/src/app/endpoints/public/public.controller.ts +++ b/apps/api/src/app/endpoints/public/public.controller.ts @@ -57,7 +57,7 @@ export class PublicController { } const [ - { holdings }, + { holdings, markets }, { performance: performance1d }, { performance: performanceMax }, { performance: performanceYtd } @@ -76,8 +76,13 @@ export class PublicController { }) ]); + Object.values(markets).forEach((market) => { + delete market.valueInBaseCurrency; + }); + const publicPortfolioResponse: PublicPortfolioResponse = { hasDetails, + markets, alias: access.alias, holdings: {}, performance: { diff --git a/apps/client/src/app/pages/public/public-page.component.ts b/apps/client/src/app/pages/public/public-page.component.ts index 56d6ebdb7..3dbce23ec 100644 --- a/apps/client/src/app/pages/public/public-page.component.ts +++ b/apps/client/src/app/pages/public/public-page.component.ts @@ -32,7 +32,7 @@ export class PublicPageComponent implements OnInit { public deviceType: string; public holdings: PublicPortfolioResponse['holdings'][string][]; public markets: { - [key in Market]: { name: string; value: number }; + [key in Market]: { id: Market; valueInPercentage: number }; }; public positions: { [symbol: string]: Pick & { @@ -102,24 +102,7 @@ export class PublicPageComponent implements OnInit { } }; this.holdings = []; - this.markets = { - [UNKNOWN_KEY]: { - name: UNKNOWN_KEY, - value: 0 - }, - developedMarkets: { - name: 'developedMarkets', - value: 0 - }, - emergingMarkets: { - name: 'emergingMarkets', - value: 0 - }, - otherMarkets: { - name: 'otherMarkets', - value: 0 - } - }; + this.markets = this.publicPortfolioDetails.markets; this.positions = {}; this.sectors = { [UNKNOWN_KEY]: { @@ -150,13 +133,6 @@ export class PublicPageComponent implements OnInit { // Prepare analysis data by continents, countries, holdings and sectors except for liquidity if (position.countries.length > 0) { - this.markets.developedMarkets.value += - position.markets.developedMarkets * position.valueInBaseCurrency; - this.markets.emergingMarkets.value += - position.markets.emergingMarkets * position.valueInBaseCurrency; - this.markets.otherMarkets.value += - position.markets.otherMarkets * position.valueInBaseCurrency; - for (const country of position.countries) { const { code, continent, name, weight } = country; @@ -192,9 +168,6 @@ export class PublicPageComponent implements OnInit { this.countries[UNKNOWN_KEY].value += this.publicPortfolioDetails.holdings[symbol].valueInBaseCurrency; - - this.markets[UNKNOWN_KEY].value += - this.publicPortfolioDetails.holdings[symbol].valueInBaseCurrency; } if (position.sectors.length > 0) { @@ -227,21 +200,6 @@ export class PublicPageComponent implements OnInit { : position.valueInPercentage }; } - - const marketsTotal = - this.markets.developedMarkets.value + - this.markets.emergingMarkets.value + - this.markets.otherMarkets.value + - this.markets[UNKNOWN_KEY].value; - - this.markets.developedMarkets.value = - this.markets.developedMarkets.value / marketsTotal; - this.markets.emergingMarkets.value = - this.markets.emergingMarkets.value / marketsTotal; - this.markets.otherMarkets.value = - this.markets.otherMarkets.value / marketsTotal; - this.markets[UNKNOWN_KEY].value = - this.markets[UNKNOWN_KEY].value / marketsTotal; } public ngOnDestroy() { diff --git a/apps/client/src/app/pages/public/public-page.html b/apps/client/src/app/pages/public/public-page.html index 369ea50f5..09a7c17ab 100644 --- a/apps/client/src/app/pages/public/public-page.html +++ b/apps/client/src/app/pages/public/public-page.html @@ -156,7 +156,7 @@ i18n size="large" [isPercent]="true" - [value]="markets?.developedMarkets?.value" + [value]="markets?.developedMarkets?.valueInPercentage" >Developed Markets @@ -165,7 +165,7 @@ i18n size="large" [isPercent]="true" - [value]="markets?.emergingMarkets?.value" + [value]="markets?.emergingMarkets?.valueInPercentage" >Emerging Markets @@ -174,7 +174,7 @@ i18n size="large" [isPercent]="true" - [value]="markets?.otherMarkets?.value" + [value]="markets?.otherMarkets?.valueInPercentage" >Other Markets diff --git a/libs/common/src/lib/interfaces/responses/public-portfolio-response.interface.ts b/libs/common/src/lib/interfaces/responses/public-portfolio-response.interface.ts index ce623a058..dc6e57587 100644 --- a/libs/common/src/lib/interfaces/responses/public-portfolio-response.interface.ts +++ b/libs/common/src/lib/interfaces/responses/public-portfolio-response.interface.ts @@ -1,4 +1,5 @@ -import { PortfolioPosition } from '../portfolio-position.interface'; +import { PortfolioDetails, PortfolioPosition } from '..'; +import { Market } from '../../types'; export interface PublicPortfolioResponse extends PublicPortfolioResponseV1 { alias?: string; @@ -22,6 +23,12 @@ export interface PublicPortfolioResponse extends PublicPortfolioResponseV1 { | 'valueInPercentage' >; }; + markets: { + [key in Market]: Pick< + PortfolioDetails['markets'][key], + 'id' | 'valueInPercentage' + >; + }; } interface PublicPortfolioResponseV1 {