From b45c102e589e820a06753099bd777b0c263a618f Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 27 Jun 2026 11:40:58 +0200 Subject: [PATCH] Refactoring --- ...reate-or-update-access-dialog.component.ts | 74 ++++++------------- 1 file changed, 21 insertions(+), 53 deletions(-) diff --git a/apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts b/apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts index 77699a304..df083a5dc 100644 --- a/apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts +++ b/apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts @@ -1,5 +1,6 @@ import { UserService } from '@ghostfolio/client/services/user/user.service'; import { CreateAccessDto, UpdateAccessDto } from '@ghostfolio/common/dtos'; +import { getAssetProfileIdentifier } from '@ghostfolio/common/helper'; import { Filter, PortfolioPosition } from '@ghostfolio/common/interfaces'; import { AccountWithPlatform } from '@ghostfolio/common/types'; import { validateObjectForForm } from '@ghostfolio/common/utils'; @@ -38,7 +39,7 @@ import { import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select'; -import { AccessPermission, AssetClass } from '@prisma/client'; +import { AccessPermission, AssetClass, DataSource } from '@prisma/client'; import { StatusCodes } from 'http-status-codes'; import { EMPTY, catchError } from 'rxjs'; @@ -146,8 +147,6 @@ export class GfCreateOrUpdateAccessDialogComponent implements OnInit { return a.label.localeCompare(b.label); }) ?? []; - this.updateFiltersFormControl(this.data.access?.settings?.filters); - this.changeDetectorRef.markForCheck(); }); @@ -344,61 +343,30 @@ export class GfCreateOrUpdateAccessDialogComponent implements OnInit { } } - private updateFiltersFormControl(existingFilters: Filter[] | undefined) { - if (!existingFilters?.length) { + private updateFiltersFormControl(filters: Filter[] | undefined) { + if (!filters?.length) { return; } - const filterValue: Partial = {}; - - const accountFilter = existingFilters.find(({ type }) => { - return type === 'ACCOUNT'; - }); - - if (accountFilter && this.accounts.length > 0) { - filterValue.account = accountFilter.id; - } - - const assetClassFilter = existingFilters.find(({ type }) => { - return type === 'ASSET_CLASS'; - }); - - if (assetClassFilter && this.assetClasses.length > 0) { - filterValue.assetClass = assetClassFilter.id; - } - - const dataSourceFilter = existingFilters.find(({ type }) => { - return type === 'DATA_SOURCE'; - }); - - const symbolFilter = existingFilters.find(({ type }) => { - return type === 'SYMBOL'; - }); - - if (dataSourceFilter && symbolFilter && this.holdings.length > 0) { - const holding = this.holdings.find(({ assetProfile }) => { - return ( - assetProfile.dataSource === dataSourceFilter.id && - assetProfile.symbol === symbolFilter.id - ); - }); - - if (holding) { - filterValue.holding = holding; - } - } + const getFilterId = (type: Filter['type']) => { + return filters.find((filter) => filter.type === type)?.id ?? null; + }; - const tagFilter = existingFilters.find(({ type }) => { - return type === 'TAG'; + const dataSource = getFilterId('DATA_SOURCE') as DataSource; + const symbol = getFilterId('SYMBOL'); + const holding = this.holdings.find(({ assetProfile }) => { + return ( + !!(dataSource && symbol) && + getAssetProfileIdentifier(assetProfile) === + getAssetProfileIdentifier({ dataSource, symbol }) + ); }); - if (tagFilter && this.tags.length > 0) { - filterValue.tag = tagFilter.id; - } - - if (Object.keys(filterValue).length > 0) { - this.accessForm.get('filters')?.setValue(filterValue); - this.changeDetectorRef.markForCheck(); - } + this.accessForm.get('filters')?.setValue({ + account: getFilterId('ACCOUNT'), + assetClass: getFilterId('ASSET_CLASS'), + holding: holding ?? null, + tag: getFilterId('TAG') + } satisfies PortfolioFilterFormValue); } }