110889: Prevent vocabulary undefined from being retrieved in hierarchy filter when none is configured

(cherry picked from commit 9e7a59ddd0)
This commit is contained in:
Alexandre Vryghem
2024-02-18 11:58:04 +01:00
parent ce3eb76190
commit 0aadcdfdd9
2 changed files with 29 additions and 20 deletions

View File

@@ -27,6 +27,8 @@ import { SearchConfigurationServiceStub } from '../../../../testing/search-confi
import { VocabularyEntryDetail } from '../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model'; import { VocabularyEntryDetail } from '../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
import { FacetValue} from '../../../models/facet-value.model'; import { FacetValue} from '../../../models/facet-value.model';
import { SearchFilterConfig } from '../../../models/search-filter-config.model'; import { SearchFilterConfig } from '../../../models/search-filter-config.model';
import { APP_CONFIG } from '../../../../../../config/app-config.interface';
import { environment } from '../../../../../../environments/environment.test';
describe('SearchHierarchyFilterComponent', () => { describe('SearchHierarchyFilterComponent', () => {
@@ -34,7 +36,7 @@ describe('SearchHierarchyFilterComponent', () => {
let showVocabularyTreeLink: DebugElement; let showVocabularyTreeLink: DebugElement;
const testSearchLink = 'test-search'; const testSearchLink = 'test-search';
const testSearchFilter = 'test-search-filter'; const testSearchFilter = 'subject';
const VocabularyTreeViewComponent = { const VocabularyTreeViewComponent = {
select: new EventEmitter<VocabularyEntryDetail>(), select: new EventEmitter<VocabularyEntryDetail>(),
}; };
@@ -73,6 +75,7 @@ describe('SearchHierarchyFilterComponent', () => {
{ provide: Router, useValue: router }, { provide: Router, useValue: router },
{ provide: NgbModal, useValue: ngbModal }, { provide: NgbModal, useValue: ngbModal },
{ provide: VocabularyService, useValue: vocabularyService }, { provide: VocabularyService, useValue: vocabularyService },
{ provide: APP_CONFIG, useValue: environment },
{ provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() }, { provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() },
{ provide: IN_PLACE_SEARCH, useValue: false }, { provide: IN_PLACE_SEARCH, useValue: false },
{ provide: FILTER_CONFIG, useValue: Object.assign(new SearchFilterConfig(), { name: testSearchFilter }) }, { provide: FILTER_CONFIG, useValue: Object.assign(new SearchFilterConfig(), { name: testSearchFilter }) },
@@ -86,7 +89,7 @@ describe('SearchHierarchyFilterComponent', () => {
function init() { function init() {
fixture = TestBed.createComponent(SearchHierarchyFilterComponent); fixture = TestBed.createComponent(SearchHierarchyFilterComponent);
fixture.detectChanges(); fixture.detectChanges();
showVocabularyTreeLink = fixture.debugElement.query(By.css('a#show-test-search-filter-tree')); showVocabularyTreeLink = fixture.debugElement.query(By.css(`a#show-${testSearchFilter}-tree`));
} }
describe('if the vocabulary doesn\'t exist', () => { describe('if the vocabulary doesn\'t exist', () => {

View File

@@ -24,9 +24,11 @@ import { filter, map, take } from 'rxjs/operators';
import { VocabularyService } from '../../../../../core/submission/vocabularies/vocabulary.service'; import { VocabularyService } from '../../../../../core/submission/vocabularies/vocabulary.service';
import { Observable, BehaviorSubject } from 'rxjs'; import { Observable, BehaviorSubject } from 'rxjs';
import { PageInfo } from '../../../../../core/shared/page-info.model'; import { PageInfo } from '../../../../../core/shared/page-info.model';
import { environment } from '../../../../../../environments/environment';
import { addOperatorToFilterValue } from '../../../search.utils'; import { addOperatorToFilterValue } from '../../../search.utils';
import { VocabularyTreeviewModalComponent } from '../../../../form/vocabulary-treeview-modal/vocabulary-treeview-modal.component'; import { VocabularyTreeviewModalComponent } from '../../../../form/vocabulary-treeview-modal/vocabulary-treeview-modal.component';
import { hasValue } from '../../../../empty.util';
import { APP_CONFIG, AppConfig } from '../../../../../../config/app-config.interface';
import { FilterVocabularyConfig } from '../../../../../../config/filter-vocabulary-config';
@Component({ @Component({
selector: 'ds-search-hierarchy-filter', selector: 'ds-search-hierarchy-filter',
@@ -47,6 +49,7 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
protected router: Router, protected router: Router,
protected modalService: NgbModal, protected modalService: NgbModal,
protected vocabularyService: VocabularyService, protected vocabularyService: VocabularyService,
@Inject(APP_CONFIG) protected appConfig: AppConfig,
@Inject(SEARCH_CONFIG_SERVICE) public searchConfigService: SearchConfigurationService, @Inject(SEARCH_CONFIG_SERVICE) public searchConfigService: SearchConfigurationService,
@Inject(IN_PLACE_SEARCH) public inPlaceSearch: boolean, @Inject(IN_PLACE_SEARCH) public inPlaceSearch: boolean,
@Inject(FILTER_CONFIG) public filterConfig: SearchFilterConfig, @Inject(FILTER_CONFIG) public filterConfig: SearchFilterConfig,
@@ -67,17 +70,20 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
super.onSubmit(addOperatorToFilterValue(data, 'query')); super.onSubmit(addOperatorToFilterValue(data, 'query'));
} }
ngOnInit() { ngOnInit(): void {
super.ngOnInit(); super.ngOnInit();
this.vocabularyExists$ = this.vocabularyService.searchTopEntries( const vocabularyName: string = this.getVocabularyEntry();
this.getVocabularyEntry(), new PageInfo(), true, false, if (hasValue(vocabularyName)) {
).pipe( this.vocabularyExists$ = this.vocabularyService.searchTopEntries(
filter(rd => rd.hasCompleted), vocabularyName, new PageInfo(), true, false,
take(1), ).pipe(
map(rd => { filter(rd => rd.hasCompleted),
return rd.hasSucceeded; take(1),
}), map(rd => {
); return rd.hasSucceeded;
}),
);
}
} }
/** /**
@@ -93,11 +99,11 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
name: this.getVocabularyEntry(), name: this.getVocabularyEntry(),
closed: true closed: true
}; };
modalRef.result.then((detail: VocabularyEntryDetail) => { void modalRef.result.then((detail: VocabularyEntryDetail) => {
this.selectedValues$ this.subs.push(this.selectedValues$
.pipe(take(1)) .pipe(take(1))
.subscribe((selectedValues) => { .subscribe((selectedValues) => {
this.router.navigate( void this.router.navigate(
[this.searchService.getSearchLink()], [this.searchService.getSearchLink()],
{ {
queryParams: { queryParams: {
@@ -107,16 +113,16 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
queryParamsHandling: 'merge', queryParamsHandling: 'merge',
}, },
); );
}); }));
}).catch(); });
} }
/** /**
* Returns the matching vocabulary entry for the given search filter. * Returns the matching vocabulary entry for the given search filter.
* These are configurable in the config file. * These are configurable in the config file.
*/ */
getVocabularyEntry() { getVocabularyEntry(): string {
const foundVocabularyConfig = environment.vocabularies.filter((v) => v.filter === this.filterConfig.name); const foundVocabularyConfig: FilterVocabularyConfig[] = this.appConfig.vocabularies.filter((v: FilterVocabularyConfig) => v.filter === this.filterConfig.name);
if (foundVocabularyConfig.length > 0 && foundVocabularyConfig[0].enabled === true) { if (foundVocabularyConfig.length > 0 && foundVocabularyConfig[0].enabled === true) {
return foundVocabularyConfig[0].vocabulary; return foundVocabularyConfig[0].vocabulary;
} }