Skip to content

Commit

Permalink
110889: Prevent vocabulary undefined from being retrieved in hierarch…
Browse files Browse the repository at this point in the history
…y filter when none is configured

(cherry picked from commit 9e7a59d)
  • Loading branch information
alexandrevryghem committed May 9, 2024
1 parent ce3eb76 commit 0aadcdf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ import { SearchConfigurationServiceStub } from '../../../../testing/search-confi
import { VocabularyEntryDetail } from '../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
import { FacetValue} from '../../../models/facet-value.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', () => {

let fixture: ComponentFixture<SearchHierarchyFilterComponent>;
let showVocabularyTreeLink: DebugElement;

const testSearchLink = 'test-search';
const testSearchFilter = 'test-search-filter';
const testSearchFilter = 'subject';
const VocabularyTreeViewComponent = {
select: new EventEmitter<VocabularyEntryDetail>(),
};
Expand Down Expand Up @@ -73,6 +75,7 @@ describe('SearchHierarchyFilterComponent', () => {
{ provide: Router, useValue: router },
{ provide: NgbModal, useValue: ngbModal },
{ provide: VocabularyService, useValue: vocabularyService },
{ provide: APP_CONFIG, useValue: environment },
{ provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() },
{ provide: IN_PLACE_SEARCH, useValue: false },
{ provide: FILTER_CONFIG, useValue: Object.assign(new SearchFilterConfig(), { name: testSearchFilter }) },
Expand All @@ -86,7 +89,7 @@ describe('SearchHierarchyFilterComponent', () => {
function init() {
fixture = TestBed.createComponent(SearchHierarchyFilterComponent);
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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import { filter, map, take } from 'rxjs/operators';
import { VocabularyService } from '../../../../../core/submission/vocabularies/vocabulary.service';
import { Observable, BehaviorSubject } from 'rxjs';
import { PageInfo } from '../../../../../core/shared/page-info.model';
import { environment } from '../../../../../../environments/environment';
import { addOperatorToFilterValue } from '../../../search.utils';
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({
selector: 'ds-search-hierarchy-filter',
Expand All @@ -47,6 +49,7 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
protected router: Router,
protected modalService: NgbModal,
protected vocabularyService: VocabularyService,
@Inject(APP_CONFIG) protected appConfig: AppConfig,
@Inject(SEARCH_CONFIG_SERVICE) public searchConfigService: SearchConfigurationService,
@Inject(IN_PLACE_SEARCH) public inPlaceSearch: boolean,
@Inject(FILTER_CONFIG) public filterConfig: SearchFilterConfig,
Expand All @@ -67,17 +70,20 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
super.onSubmit(addOperatorToFilterValue(data, 'query'));
}

ngOnInit() {
ngOnInit(): void {
super.ngOnInit();
this.vocabularyExists$ = this.vocabularyService.searchTopEntries(
this.getVocabularyEntry(), new PageInfo(), true, false,
).pipe(
filter(rd => rd.hasCompleted),
take(1),
map(rd => {
return rd.hasSucceeded;
}),
);
const vocabularyName: string = this.getVocabularyEntry();
if (hasValue(vocabularyName)) {
this.vocabularyExists$ = this.vocabularyService.searchTopEntries(
vocabularyName, new PageInfo(), true, false,
).pipe(
filter(rd => rd.hasCompleted),
take(1),
map(rd => {
return rd.hasSucceeded;
}),
);
}
}

/**
Expand All @@ -93,11 +99,11 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
name: this.getVocabularyEntry(),
closed: true
};
modalRef.result.then((detail: VocabularyEntryDetail) => {
this.selectedValues$
void modalRef.result.then((detail: VocabularyEntryDetail) => {
this.subs.push(this.selectedValues$
.pipe(take(1))
.subscribe((selectedValues) => {
this.router.navigate(
void this.router.navigate(
[this.searchService.getSearchLink()],
{
queryParams: {
Expand All @@ -107,16 +113,16 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
queryParamsHandling: 'merge',
},
);
});
}).catch();
}));
});
}

/**
* Returns the matching vocabulary entry for the given search filter.
* These are configurable in the config file.
*/
getVocabularyEntry() {
const foundVocabularyConfig = environment.vocabularies.filter((v) => v.filter === this.filterConfig.name);
getVocabularyEntry(): string {
const foundVocabularyConfig: FilterVocabularyConfig[] = this.appConfig.vocabularies.filter((v: FilterVocabularyConfig) => v.filter === this.filterConfig.name);
if (foundVocabularyConfig.length > 0 && foundVocabularyConfig[0].enabled === true) {
return foundVocabularyConfig[0].vocabulary;
}
Expand Down

0 comments on commit 0aadcdf

Please sign in to comment.