Browse Source

Refactoring

pull/5012/head
Thomas Kaul 1 week ago
parent
commit
eee81d2122
  1. 30
      apps/api/src/models/rules/account-cluster-risk/current-investment.ts
  2. 8
      apps/client/src/app/pages/i18n/i18n-page.html

30
apps/api/src/models/rules/account-cluster-risk/current-investment.ts

@ -2,11 +2,9 @@ import { RuleSettings } from '@ghostfolio/api/models/interfaces/rule-settings.in
import { Rule } from '@ghostfolio/api/models/rule'; import { Rule } from '@ghostfolio/api/models/rule';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { I18nService } from '@ghostfolio/api/services/i18n/i18n.service'; import { I18nService } from '@ghostfolio/api/services/i18n/i18n.service';
import { import { PortfolioDetails, UserSettings } from '@ghostfolio/common/interfaces';
PortfolioDetails,
PortfolioPosition, import { Account } from '@prisma/client';
UserSettings
} from '@ghostfolio/common/interfaces';
export class AccountClusterRiskCurrentInvestment extends Rule<Settings> { export class AccountClusterRiskCurrentInvestment extends Rule<Settings> {
private accounts: PortfolioDetails['accounts']; private accounts: PortfolioDetails['accounts'];
@ -27,36 +25,36 @@ export class AccountClusterRiskCurrentInvestment extends Rule<Settings> {
public evaluate(ruleSettings: Settings) { public evaluate(ruleSettings: Settings) {
const accounts: { const accounts: {
[symbol: string]: Pick<PortfolioPosition, 'name'> & { [symbol: string]: Pick<Account, 'name'> & {
investment: number; investment: number;
}; };
} = {}; } = {};
for (const [accountId, account] of Object.entries(this.accounts)) { for (const [accountId, account] of Object.entries(this.accounts)) {
accounts[accountId] = { accounts[accountId] = {
name: account.name, investment: account.valueInBaseCurrency,
investment: account.valueInBaseCurrency name: account.name
}; };
} }
let maxItem: (typeof accounts)[0]; let maxAccount: (typeof accounts)[0];
let totalInvestment = 0; let totalInvestment = 0;
for (const account of Object.values(accounts)) { for (const account of Object.values(accounts)) {
if (!maxItem) { if (!maxAccount) {
maxItem = account; maxAccount = account;
} }
// Calculate total investment // Calculate total investment
totalInvestment += account.investment; totalInvestment += account.investment;
// Find maximum // Find maximum
if (account.investment > maxItem?.investment) { if (account.investment > maxAccount?.investment) {
maxItem = account; maxAccount = account;
} }
} }
const maxInvestmentRatio = maxItem?.investment / totalInvestment || 0; const maxInvestmentRatio = maxAccount?.investment / totalInvestment || 0;
if (maxInvestmentRatio > ruleSettings.thresholdMax) { if (maxInvestmentRatio > ruleSettings.thresholdMax) {
return { return {
@ -64,8 +62,8 @@ export class AccountClusterRiskCurrentInvestment extends Rule<Settings> {
id: 'rule.accountClusterRiskCurrentInvestment.false', id: 'rule.accountClusterRiskCurrentInvestment.false',
languageCode: this.getLanguageCode(), languageCode: this.getLanguageCode(),
placeholders: { placeholders: {
maxAccountName: maxAccount.name,
maxInvestmentRatio: (maxInvestmentRatio * 100).toPrecision(3), maxInvestmentRatio: (maxInvestmentRatio * 100).toPrecision(3),
maxItemName: maxItem.name,
thresholdMax: ruleSettings.thresholdMax * 100 thresholdMax: ruleSettings.thresholdMax * 100
} }
}), }),
@ -78,8 +76,8 @@ export class AccountClusterRiskCurrentInvestment extends Rule<Settings> {
id: 'rule.accountClusterRiskCurrentInvestment.true', id: 'rule.accountClusterRiskCurrentInvestment.true',
languageCode: this.getLanguageCode(), languageCode: this.getLanguageCode(),
placeholders: { placeholders: {
maxAccountName: maxAccount.name,
maxInvestmentRatio: (maxInvestmentRatio * 100).toPrecision(3), maxInvestmentRatio: (maxInvestmentRatio * 100).toPrecision(3),
maxItemName: maxItem.name,
thresholdMax: ruleSettings.thresholdMax * 100 thresholdMax: ruleSettings.thresholdMax * 100
} }
}), }),

8
apps/client/src/app/pages/i18n/i18n-page.html

@ -14,12 +14,12 @@
<li i18n="@@rule.accountClusterRiskCurrentInvestment">Investment</li> <li i18n="@@rule.accountClusterRiskCurrentInvestment">Investment</li>
<li i18n="@@rule.accountClusterRiskCurrentInvestment.false"> <li i18n="@@rule.accountClusterRiskCurrentInvestment.false">
Over $&#123;thresholdMax&#125;% of your current investment is at Over $&#123;thresholdMax&#125;% of your current investment is at
$&#123;maxItemName&#125; ($&#123;maxInvestmentRatio&#125;%) $&#123;maxAccountName&#125; ($&#123;maxInvestmentRatio&#125;%)
</li> </li>
<li i18n="@@rule.accountClusterRiskCurrentInvestment.true"> <li i18n="@@rule.accountClusterRiskCurrentInvestment.true">
The major part of your current investment is at $&#123;maxItemName&#125; The major part of your current investment is at
($&#123;maxInvestmentRatio&#125;%) and does not exceed $&#123;maxAccountName&#125; ($&#123;maxInvestmentRatio&#125;%) and does
$&#123;thresholdMax&#125;% not exceed $&#123;thresholdMax&#125;%
</li> </li>
<li i18n="@@rule.accountClusterRiskSingleAccount">Single Account</li> <li i18n="@@rule.accountClusterRiskSingleAccount">Single Account</li>
<li i18n="@@rule.accountClusterRiskSingleAccount.false"> <li i18n="@@rule.accountClusterRiskSingleAccount.false">

Loading…
Cancel
Save