From ce04656c92df378da547cbe710f18493e4f807d5 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sat, 26 Mar 2022 11:53:45 +0100 Subject: [PATCH] Refactoring and integrate in public page --- .../portfolio/portfolio-service.strategy.ts | 3 +- .../src/app/portfolio/portfolio.controller.ts | 5 +- .../app/portfolio/portfolio.service-new.ts | 15 +++--- .../allocations/allocations-page.component.ts | 48 +++++++++---------- .../allocations/allocations-page.html | 19 +++++--- .../app/pages/public/public-page.component.ts | 37 ++++++++++++++ .../src/app/pages/public/public-page.html | 28 ++++++++++- .../app/pages/public/public-page.module.ts | 2 + apps/client/src/styles.scss | 4 -- .../portfolio-position.interface.ts | 3 +- .../portfolio-public-details.interface.ts | 1 + libs/common/src/lib/types/index.ts | 2 + libs/common/src/lib/types/market.type.ts | 1 + libs/ui/src/lib/value/value.component.ts | 2 +- 14 files changed, 122 insertions(+), 48 deletions(-) create mode 100644 libs/common/src/lib/types/market.type.ts diff --git a/apps/api/src/app/portfolio/portfolio-service.strategy.ts b/apps/api/src/app/portfolio/portfolio-service.strategy.ts index 49ec0422f..a85b28852 100644 --- a/apps/api/src/app/portfolio/portfolio-service.strategy.ts +++ b/apps/api/src/app/portfolio/portfolio-service.strategy.ts @@ -13,8 +13,9 @@ export class PortfolioServiceStrategy { @Inject(REQUEST) private readonly request: RequestWithUser ) {} - public get() { + public get(newCalculationEngine?: boolean) { if ( + newCalculationEngine || this.request.user?.Settings?.settings?.['isNewCalculationEngine'] === true ) { return this.portfolioServiceNew; diff --git a/apps/api/src/app/portfolio/portfolio.controller.ts b/apps/api/src/app/portfolio/portfolio.controller.ts index 2d7993859..1bb42a0ed 100644 --- a/apps/api/src/app/portfolio/portfolio.controller.ts +++ b/apps/api/src/app/portfolio/portfolio.controller.ts @@ -120,7 +120,7 @@ export class PortfolioController { const { accounts, holdings, hasErrors } = await this.portfolioServiceStrategy - .get() + .get(true) .getDetails(impersonationId, this.request.user.id, range); if (hasErrors || hasNotDefinedValuesInObject(holdings)) { @@ -277,7 +277,7 @@ export class PortfolioController { } const { holdings } = await this.portfolioServiceStrategy - .get() + .get(true) .getDetails(access.userId, access.userId); const portfolioPublicDetails: PortfolioPublicDetails = { @@ -304,6 +304,7 @@ export class PortfolioController { allocationCurrent: portfolioPosition.allocationCurrent, countries: hasDetails ? portfolioPosition.countries : [], currency: portfolioPosition.currency, + markets: portfolioPosition.markets, name: portfolioPosition.name, sectors: hasDetails ? portfolioPosition.sectors : [], value: portfolioPosition.value / totalValue diff --git a/apps/api/src/app/portfolio/portfolio.service-new.ts b/apps/api/src/app/portfolio/portfolio.service-new.ts index f74cd2719..63b8544a8 100644 --- a/apps/api/src/app/portfolio/portfolio.service-new.ts +++ b/apps/api/src/app/portfolio/portfolio.service-new.ts @@ -40,6 +40,7 @@ import { InvestmentItem } from '@ghostfolio/common/interfaces/investment-item.in import type { AccountWithValue, DateRange, + Market, OrderWithAccount, RequestWithUser } from '@ghostfolio/common/types'; @@ -384,23 +385,23 @@ export class PortfolioServiceNew { const symbolProfile = symbolProfileMap[item.symbol]; const dataProviderResponse = dataProviderResponses[item.symbol]; - const markets = { - DEVELOPED_MARKETS: 0, - EMERGING_MARKETS: 0, - OTHER_MARKETS: 0 + const markets: { [key in Market]: number } = { + developedMarkets: 0, + emergingMarkets: 0, + otherMarkets: 0 }; for (const country of symbolProfile.countries) { if (developedMarkets.includes(country.code)) { - markets.DEVELOPED_MARKETS = new Big(markets.DEVELOPED_MARKETS) + markets.developedMarkets = new Big(markets.developedMarkets) .plus(country.weight) .toNumber(); } else if (emergingMarkets.includes(country.code)) { - markets.EMERGING_MARKETS = new Big(markets.EMERGING_MARKETS) + markets.emergingMarkets = new Big(markets.emergingMarkets) .plus(country.weight) .toNumber(); } else { - markets.OTHER_MARKETS = new Big(markets.OTHER_MARKETS) + markets.otherMarkets = new Big(markets.otherMarkets) .plus(country.weight) .toNumber(); } 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 c9267defd..e0636dc7d 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 @@ -14,7 +14,7 @@ import { User } from '@ghostfolio/common/interfaces'; import { hasPermission, permissions } from '@ghostfolio/common/permissions'; -import { ToggleOption } from '@ghostfolio/common/types'; +import { Market, ToggleOption } from '@ghostfolio/common/types'; import { Account, AssetClass, DataSource } from '@prisma/client'; import { DeviceDetectorService } from 'ngx-device-detector'; import { Subject, Subscription } from 'rxjs'; @@ -43,7 +43,7 @@ export class AllocationsPageComponent implements OnDestroy, OnInit { public hasImpersonationId: boolean; public hasPermissionToCreateOrder: boolean; public markets: { - [market: string]: { name: string; value: number }; + [key in Market]: { name: string; value: number }; }; public period = 'current'; public periodOptions: ToggleOption[] = [ @@ -164,16 +164,16 @@ export class AllocationsPageComponent implements OnDestroy, OnInit { } }; this.markets = { - DEVELOPED_MARKETS: { - name: 'DEVELOPED_MARKETS', + developedMarkets: { + name: 'developedMarkets', value: 0 }, - EMERGING_MARKETS: { - name: 'EMERGING_MARKETS', + emergingMarkets: { + name: 'emergingMarkets', value: 0 }, - OTHER_MARKETS: { - name: 'OTHER_MARKETS', + otherMarkets: { + name: 'otherMarkets', value: 0 } }; @@ -236,14 +236,14 @@ export class AllocationsPageComponent implements OnDestroy, OnInit { // Prepare analysis data by continents, countries and sectors except for cash if (position.countries.length > 0) { - this.markets['DEVELOPED_MARKETS'].value += - position.markets['DEVELOPED_MARKETS'] * + this.markets['developedMarkets'].value += + position.markets['developedMarkets'] * (aPeriod === 'original' ? position.investment : position.value); - this.markets['EMERGING_MARKETS'].value += - position.markets['EMERGING_MARKETS'] * + this.markets['emergingMarkets'].value += + position.markets['emergingMarkets'] * (aPeriod === 'original' ? position.investment : position.value); - this.markets['OTHER_MARKETS'].value += - position.markets['OTHER_MARKETS'] * + this.markets['otherMarkets'].value += + position.markets['otherMarkets'] * (aPeriod === 'original' ? position.investment : position.value); for (const country of position.countries) { @@ -323,16 +323,16 @@ export class AllocationsPageComponent implements OnDestroy, OnInit { } const marketsTotal = - this.markets['DEVELOPED_MARKETS'].value + - this.markets['EMERGING_MARKETS'].value + - this.markets['OTHER_MARKETS'].value; - - this.markets['DEVELOPED_MARKETS'].value = - this.markets['DEVELOPED_MARKETS'].value / marketsTotal; - this.markets['EMERGING_MARKETS'].value = - this.markets['EMERGING_MARKETS'].value / marketsTotal; - this.markets['OTHER_MARKETS'].value = - this.markets['OTHER_MARKETS'].value / marketsTotal; + this.markets.developedMarkets.value + + this.markets.emergingMarkets.value + + this.markets.otherMarkets.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; } public onChangePeriod(aValue: string) { diff --git a/apps/client/src/app/pages/portfolio/allocations/allocations-page.html b/apps/client/src/app/pages/portfolio/allocations/allocations-page.html index 0abb19837..c34eebadd 100644 --- a/apps/client/src/app/pages/portfolio/allocations/allocations-page.html +++ b/apps/client/src/app/pages/portfolio/allocations/allocations-page.html @@ -191,26 +191,31 @@ [isInPercent]="hasImpersonationId || user.settings.isRestrictedView" >