From 109b2d0fc4e9436460cce19c453f32cd86658c0d Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 12 Oct 2024 20:38:29 +0200 Subject: [PATCH] Add threshold configuration --- apps/api/src/app/portfolio/rules.service.ts | 6 +----- apps/api/src/models/rule.ts | 10 +++++++++- .../rules/account-cluster-risk/current-investment.ts | 11 +++++++++++ .../rules/account-cluster-risk/single-account.ts | 4 ++++ .../base-currency-current-investment.ts | 4 ++++ .../rules/currency-cluster-risk/current-investment.ts | 11 +++++++++++ .../rules/emergency-fund/emergency-fund-setup.ts | 4 ++++ .../models/rules/fees/fee-ratio-initial-investment.ts | 11 +++++++++++ .../lib/interfaces/portfolio-report-rule.interface.ts | 9 +++++++-- 9 files changed, 62 insertions(+), 8 deletions(-) diff --git a/apps/api/src/app/portfolio/rules.service.ts b/apps/api/src/app/portfolio/rules.service.ts index 1d7acbd9d..5f0aa64d5 100644 --- a/apps/api/src/app/portfolio/rules.service.ts +++ b/apps/api/src/app/portfolio/rules.service.ts @@ -6,7 +6,6 @@ import { } from '@ghostfolio/common/interfaces'; import { Injectable } from '@nestjs/common'; -import { isNumber } from 'lodash'; @Injectable() export class RulesService { @@ -25,10 +24,7 @@ export class RulesService { return { evaluation, value, - configuration: { - thresholdMax: isNumber(settings['thresholdMax']) ? true : false, - thresholdMin: isNumber(settings['thresholdMin']) ? true : false - } as PortfolioReportRule['configuration'], + configuration: rule.getConfiguration(), isActive: true, key: rule.getKey(), name: rule.getName() diff --git a/apps/api/src/models/rule.ts b/apps/api/src/models/rule.ts index a1e0d9bee..187527fbb 100644 --- a/apps/api/src/models/rule.ts +++ b/apps/api/src/models/rule.ts @@ -1,7 +1,11 @@ import { RuleSettings } from '@ghostfolio/api/models/interfaces/rule-settings.interface'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { groupBy } from '@ghostfolio/common/helper'; -import { PortfolioPosition, UserSettings } from '@ghostfolio/common/interfaces'; +import { + PortfolioPosition, + PortfolioReportRule, + UserSettings +} from '@ghostfolio/common/interfaces'; import { Big } from 'big.js'; @@ -65,5 +69,9 @@ export abstract class Rule implements RuleInterface { public abstract evaluate(aRuleSettings: T): EvaluationResult; + public abstract getConfiguration(): Partial< + PortfolioReportRule['configuration'] + >; + public abstract getSettings(aUserSettings: UserSettings): T; } diff --git a/apps/api/src/models/rules/account-cluster-risk/current-investment.ts b/apps/api/src/models/rules/account-cluster-risk/current-investment.ts index 82044a975..95a8022ed 100644 --- a/apps/api/src/models/rules/account-cluster-risk/current-investment.ts +++ b/apps/api/src/models/rules/account-cluster-risk/current-investment.ts @@ -76,6 +76,17 @@ export class AccountClusterRiskCurrentInvestment extends Rule { }; } + public getConfiguration() { + return { + threshold: { + max: 1, + min: 0, + step: 0.01 + }, + thresholdMax: true + }; + } + public getSettings({ baseCurrency, xRayRules }: UserSettings): Settings { return { baseCurrency, diff --git a/apps/api/src/models/rules/account-cluster-risk/single-account.ts b/apps/api/src/models/rules/account-cluster-risk/single-account.ts index 85aec28c9..22e789041 100644 --- a/apps/api/src/models/rules/account-cluster-risk/single-account.ts +++ b/apps/api/src/models/rules/account-cluster-risk/single-account.ts @@ -34,6 +34,10 @@ export class AccountClusterRiskSingleAccount extends Rule { }; } + public getConfiguration() { + return {}; + } + public getSettings({ xRayRules }: UserSettings): RuleSettings { return { isActive: xRayRules?.[this.getKey()].isActive ?? true diff --git a/apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts b/apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts index fb0b3a533..d5b586cdc 100644 --- a/apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts +++ b/apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts @@ -61,6 +61,10 @@ export class CurrencyClusterRiskBaseCurrencyCurrentInvestment extends Rule { }; } + public getConfiguration() { + return { + threshold: { + max: 1, + min: 0, + step: 0.01 + }, + thresholdMax: true + }; + } + public getSettings({ baseCurrency, xRayRules }: UserSettings): Settings { return { baseCurrency, diff --git a/apps/api/src/models/rules/emergency-fund/emergency-fund-setup.ts b/apps/api/src/models/rules/emergency-fund/emergency-fund-setup.ts index 88d2c2738..3f5d65def 100644 --- a/apps/api/src/models/rules/emergency-fund/emergency-fund-setup.ts +++ b/apps/api/src/models/rules/emergency-fund/emergency-fund-setup.ts @@ -32,6 +32,10 @@ export class EmergencyFundSetup extends Rule { }; } + public getConfiguration() { + return {}; + } + public getSettings({ baseCurrency, xRayRules }: UserSettings): Settings { return { baseCurrency, diff --git a/apps/api/src/models/rules/fees/fee-ratio-initial-investment.ts b/apps/api/src/models/rules/fees/fee-ratio-initial-investment.ts index dda8f66d5..a3ea8d059 100644 --- a/apps/api/src/models/rules/fees/fee-ratio-initial-investment.ts +++ b/apps/api/src/models/rules/fees/fee-ratio-initial-investment.ts @@ -43,6 +43,17 @@ export class FeeRatioInitialInvestment extends Rule { }; } + public getConfiguration() { + return { + threshold: { + max: 0.1, + min: 0, + step: 0.005 + }, + thresholdMax: true + }; + } + public getSettings({ baseCurrency, xRayRules }: UserSettings): Settings { return { baseCurrency, diff --git a/libs/common/src/lib/interfaces/portfolio-report-rule.interface.ts b/libs/common/src/lib/interfaces/portfolio-report-rule.interface.ts index ace2583a8..f69c097fc 100644 --- a/libs/common/src/lib/interfaces/portfolio-report-rule.interface.ts +++ b/libs/common/src/lib/interfaces/portfolio-report-rule.interface.ts @@ -1,7 +1,12 @@ export interface PortfolioReportRule { configuration?: { - thresholdMax: boolean; - thresholdMin: boolean; + threshold?: { + max: number; + min: number; + step: number; + }; + thresholdMax?: boolean; + thresholdMin?: boolean; }; evaluation?: string; isActive: boolean;