From a8eb7a6643fedf63dee4c83e9449694d30f470c4 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Thu, 30 Jan 2025 21:01:55 +0100 Subject: [PATCH] Refactoring --- .../src/app/portfolio/portfolio.service.ts | 36 +++++++++---------- apps/api/src/app/user/user.service.ts | 13 +++---- .../north-america.ts | 17 ++++----- .../x-ray-rules-settings.interface.ts | 2 +- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index ba911f628..a14f97d26 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -1176,14 +1176,14 @@ export class PortfolioService { withSummary: true }); - const marketsTotalInBaseCurrency = getSum( - Object.values(markets).map(({ valueInBaseCurrency }) => { + const marketsAdvancedTotalInBaseCurrency = getSum( + Object.values(marketsAdvanced).map(({ valueInBaseCurrency }) => { return new Big(valueInBaseCurrency); }) ).toNumber(); - const marketsAdvancedTotalInBaseCurrency = getSum( - Object.values(marketsAdvanced).map(({ valueInBaseCurrency }) => { + const marketsTotalInBaseCurrency = getSum( + Object.values(markets).map(({ valueInBaseCurrency }) => { return new Big(valueInBaseCurrency); }) ).toNumber(); @@ -1255,19 +1255,6 @@ export class PortfolioService { userSettings ) : undefined, - regionalMarketClusterRisk: - summary.ordersCount > 0 - ? await this.rulesService.evaluate( - [ - new RegionalMarketClusterRiskNorthAmerica( - this.exchangeRateDataService, - marketsAdvancedTotalInBaseCurrency, - marketsAdvanced.northAmerica.valueInBaseCurrency - ) - ], - userSettings - ) - : undefined, emergencyFund: await this.rulesService.evaluate( [ new EmergencyFundSetup( @@ -1286,7 +1273,20 @@ export class PortfolioService { ) ], userSettings - ) + ), + regionalMarketClusterRisk: + summary.ordersCount > 0 + ? await this.rulesService.evaluate( + [ + new RegionalMarketClusterRiskNorthAmerica( + this.exchangeRateDataService, + marketsAdvancedTotalInBaseCurrency, + marketsAdvanced.northAmerica.valueInBaseCurrency + ) + ], + userSettings + ) + : undefined }; return { rules, statistics: this.getReportStatistics(rules) }; diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts index 3609ec0c5..415cbc99d 100644 --- a/apps/api/src/app/user/user.service.ts +++ b/apps/api/src/app/user/user.service.ts @@ -261,11 +261,6 @@ export class UserService { undefined, undefined ).getSettings(user.Settings.settings), - RegionalMarketClusterRisk: new RegionalMarketClusterRiskNorthAmerica( - undefined, - undefined, - undefined - ).getSettings(user.Settings.settings), EmergencyFundSetup: new EmergencyFundSetup( undefined, undefined @@ -274,7 +269,13 @@ export class UserService { undefined, undefined, undefined - ).getSettings(user.Settings.settings) + ).getSettings(user.Settings.settings), + RegionalMarketClusterRiskNorthAmerica: + new RegionalMarketClusterRiskNorthAmerica( + undefined, + undefined, + undefined + ).getSettings(user.Settings.settings) }; let currentPermissions = getPermissions(user.role); diff --git a/apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts b/apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts index 00064ee1a..a137f7abf 100644 --- a/apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts +++ b/apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts @@ -5,7 +5,7 @@ import { UserSettings } from '@ghostfolio/common/interfaces'; export class RegionalMarketClusterRiskNorthAmerica extends Rule { private currentValueInBaseCurrency: number; - private regionalMarketNorthAmericaValueInBaseCurrency: number; + private northAmericaValueInBaseCurrency: number; public constructor( protected exchangeRateDataService: ExchangeRateDataService, @@ -14,27 +14,28 @@ export class RegionalMarketClusterRiskNorthAmerica extends Rule { ) { super(exchangeRateDataService, { key: RegionalMarketClusterRiskNorthAmerica.name, - name: 'Regional Markets' + name: 'North America' }); + this.currentValueInBaseCurrency = currentValueInBaseCurrency; - this.regionalMarketNorthAmericaValueInBaseCurrency = valueInBaseCurrency; + this.northAmericaValueInBaseCurrency = valueInBaseCurrency; } public evaluate(ruleSettings: Settings) { const northAmericaMarketValueRatio = this.currentValueInBaseCurrency - ? this.regionalMarketNorthAmericaValueInBaseCurrency / - this.currentValueInBaseCurrency + ? this.northAmericaValueInBaseCurrency / this.currentValueInBaseCurrency : 0; + if (northAmericaMarketValueRatio > ruleSettings.thresholdMax) { return { - evaluation: `The north america market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) exceeds ${( + evaluation: `The North America market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) exceeds ${( ruleSettings.thresholdMax * 100 ).toPrecision(3)}%`, value: false }; } else if (northAmericaMarketValueRatio < ruleSettings.thresholdMin) { return { - evaluation: `The north america market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) is below ${( + evaluation: `The North America market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) is below ${( ruleSettings.thresholdMin * 100 ).toPrecision(3)}%`, value: false @@ -42,7 +43,7 @@ export class RegionalMarketClusterRiskNorthAmerica extends Rule { } return { - evaluation: `The north america market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) is within the range of ${( + evaluation: `The North America market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) is within the range of ${( ruleSettings.thresholdMin * 100 ).toPrecision( 3 diff --git a/libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts b/libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts index da2859855..61f79d65f 100644 --- a/libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts +++ b/libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts @@ -7,9 +7,9 @@ export interface XRayRulesSettings { CurrencyClusterRiskCurrentInvestment?: RuleSettings; EconomicMarketClusterRiskDevelopedMarkets?: RuleSettings; EconomicMarketClusterRiskEmergingMarkets?: RuleSettings; - RegionalMarketClusterRisk?: RuleSettings; EmergencyFundSetup?: RuleSettings; FeeRatioInitialInvestment?: RuleSettings; + RegionalMarketClusterRiskNorthAmerica?: RuleSettings; } interface RuleSettings {