Skip to content

Commit

Permalink
Drop NaturalHierarchicConfiguration.injectedService
Browse files Browse the repository at this point in the history
This is an implementation detail that should have never been exposed
through our public API
  • Loading branch information
PowerKiKi committed Nov 4, 2024
1 parent ffde802 commit 5fd028b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export type NaturalHierarchicConfiguration<T extends UntypedModelService = Untyp
* Displayed icon for items retrieved for that config
*/
icon?: string;
injectedService?: T;

/**
* Callback function that returns boolean. If true the item is selectable, if false, it's not.
Expand All @@ -63,7 +62,3 @@ export type NaturalHierarchicConfiguration<T extends UntypedModelService = Untyp
*/
displayWith?: (item: any) => string;
};

export type NaturalHierarchicServiceConfiguration<T extends UntypedModelService = UntypedModelService> = {
injectedService: T;
} & NaturalHierarchicConfiguration<T>;
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import {Injectable, Injector, inject} from '@angular/core';
import {inject, Injectable, Injector} from '@angular/core';
import {intersection} from 'lodash-es';
import {BehaviorSubject, first, forkJoin, Observable} from 'rxjs';
import {finalize, map} from 'rxjs/operators';
import {NaturalQueryVariablesManager, QueryVariables} from '../../../classes/query-variable-manager';
import {HierarchicFlatNode} from '../classes/flat-node';
import {
NaturalHierarchicConfiguration,
NaturalHierarchicServiceConfiguration,
} from '../classes/hierarchic-configuration';
import {NaturalHierarchicConfiguration} from '../classes/hierarchic-configuration';
import {
HierarchicFilterConfiguration,
HierarchicFiltersConfiguration,
} from '../classes/hierarchic-filters-configuration';
import {HierarchicModel, HierarchicModelNode} from '../classes/model-node';
import {Literal} from '../../../types/types';
import {Literal, UntypedModelService} from '../../../types/types';
import {FilterGroupCondition} from '../../search/classes/graphql-doctrine.types';

export type OrganizedModelSelection = Record<string, any[]>;

type ContextualizedConfig = {
configuration: NaturalHierarchicServiceConfiguration;
type ContextualizedConfig<T extends UntypedModelService = UntypedModelService> = {
configuration: NaturalHierarchicConfiguration;
injectedService: T;
variablesManager: NaturalQueryVariablesManager;
};

Expand Down Expand Up @@ -51,7 +49,7 @@ export class NaturalHierarchicSelectorService {
searchVariables: QueryVariables | null = null,
): Observable<unknown> {
this.validateConfiguration(config);
this.configuration = this.injectServicesInConfiguration(config);
this.configuration = config;
return this.getList(null, contextFilter, searchVariables).pipe(map(data => this.dataChange.next(data)));
}

Expand Down Expand Up @@ -95,7 +93,7 @@ export class NaturalHierarchicSelectorService {
searchVariables: QueryVariables | null = null,
): Observable<HierarchicModelNode[]> {
const configurations = this.getContextualizedConfigs(node, contextFilters, searchVariables);
const observables = configurations.map(c => c.configuration.injectedService.getAll(c.variablesManager));
const observables = configurations.map(c => c.injectedService.getAll(c.variablesManager));

// Fire queries, and merge results, transforming apollo items into Node Object.
return forkJoin(observables).pipe(
Expand All @@ -117,9 +115,7 @@ export class NaturalHierarchicSelectorService {

public countItems(node: HierarchicFlatNode, contextFilters: HierarchicFiltersConfiguration | null = null): void {
const configurations = this.getContextualizedConfigs(node, contextFilters, null);
const observables = configurations.map(c =>
c.configuration.injectedService.count(c.variablesManager).pipe(first()),
);
const observables = configurations.map(c => c.injectedService.count(c.variablesManager).pipe(first()));

forkJoin(observables).subscribe(results => {
const totalItems = results.reduce((total, length) => total + length, 0);
Expand All @@ -145,11 +141,10 @@ export class NaturalHierarchicSelectorService {
const pagination = {pageIndex: 0, pageSize: 999};

for (const config of configs) {
const item: ContextualizedConfig = {} as ContextualizedConfig;
const contextFilter = this.getFilterByService(config, contextFilters);
const filter = this.getServiceFilter(node, config, contextFilter, !!searchVariables);

if (!filter || !config.injectedService) {
if (!filter) {
continue;
}

Expand All @@ -162,11 +157,12 @@ export class NaturalHierarchicSelectorService {
variablesManager.set('natural-search', searchVariables);
}

// Cast NaturalHierarchicServiceConfiguration because the undefined
// injectedServices are filtered earlier and we can validate value
item.configuration = config as NaturalHierarchicServiceConfiguration;
item.variablesManager = variablesManager;
configsAndServices.push(item);
const injectedService = this.injector.get(config.service);
configsAndServices.push({
configuration: config,
injectedService: injectedService,
variablesManager: variablesManager,
});
}

return configsAndServices;
Expand Down Expand Up @@ -213,18 +209,6 @@ export class NaturalHierarchicSelectorService {
return result;
}

private injectServicesInConfiguration(
configurations: NaturalHierarchicConfiguration[],
): NaturalHierarchicConfiguration[] {
for (const config of configurations) {
if (!config.injectedService) {
config.injectedService = this.injector.get(config.service);
}
}

return configurations;
}

/**
* Checks that each configuration.selectableAtKey attribute is unique
*/
Expand Down

0 comments on commit 5fd028b

Please sign in to comment.