[ACS-5093] Optimise Categories facet names load (#8498)

* [ACS-5093] removed redundant API call

* [ACS-5093] added error handling
This commit is contained in:
Nikita Maliarchuk
2023-04-21 09:07:35 +02:00
committed by GitHub
parent a563dc2f54
commit 9b2d433f2b
5 changed files with 21 additions and 33 deletions

View File

@@ -22,6 +22,9 @@ Manages categories in Content Services.
- **getCategory**(categoryId: `string`): [`Observable`](http://reactivex.io/documentation/observable.html)`<`[`CategoryEntry`](https://github.com/Alfresco/alfresco-js-api/blob/master/src/api/content-rest-api/docs/CategoryEntry.md)`>`<br/>
Gets a specific category by categoryId.
- _categoryId:_ `string` - The identifier of a category
- _opts:_ `any` - Optional parameters
- _opts.fields_ `string[]` - A list of field names
- _opts.include_ `string[]` - Returns additional information about the category. The following optional fields can be requested: count, path
- **Returns** [`Observable`](http://reactivex.io/documentation/observable.html)`<`[`CategoryEntry`](https://github.com/Alfresco/alfresco-js-api/blob/master/src/api/content-rest-api/docs/CategoryEntry.md)`>` - CategoryEntry object (defined in JS-API) containing information about the category.
- **createSubcategories**(parentCategoryId: `string`, payload: [`CategoryBody[]`](https://github.com/Alfresco/alfresco-js-api/blob/master/src/api/content-rest-api/docs/CategoryBody.md)): [`Observable`](http://reactivex.io/documentation/observable.html)`<`[`CategoryPaging`](https://github.com/Alfresco/alfresco-js-api/blob/master/src/api/content-rest-api/docs/CategoryPaging.md) | [`CategoryEntry`](https://github.com/Alfresco/alfresco-js-api/blob/master/src/api/content-rest-api/docs/CategoryEntry.md)`>`<br/>
Creates subcategories under category with provided categoryId.

View File

@@ -67,8 +67,8 @@ describe('CategoryService', () => {
it('should fetch the category with the provided categoryId', fakeAsync(() => {
const getSpy = spyOn(categoryService.categoriesApi, 'getCategory').and.returnValue(Promise.resolve(fakeCategoryEntry));
categoryService.getCategory(fakeParentCategoryId).subscribe(() => {
expect(getSpy).toHaveBeenCalledOnceWith(fakeParentCategoryId);
categoryService.getCategory(fakeParentCategoryId, {include: ['path']}).subscribe(() => {
expect(getSpy).toHaveBeenCalledOnceWith(fakeParentCategoryId, {include: ['path']});
});
}));

View File

@@ -62,10 +62,15 @@ export class CategoryService {
* Get a category by ID
*
* @param categoryId The identifier of a category.
* @param opts Optional parameters.
* @param opts.fields A list of field names.
* @param opts.include Returns additional information about the category. The following optional fields can be requested:
* count
* path
* @return Observable<CategoryEntry>
*/
getCategory(categoryId: string): Observable<CategoryEntry> {
return from(this.categoriesApi.getCategory(categoryId));
getCategory(categoryId: string, opts?: any): Observable<CategoryEntry> {
return from(this.categoriesApi.getCategory(categoryId, opts));
}
/**

View File

@@ -34,8 +34,7 @@ describe('SearchFacetFiltersService', () => {
providers: [{
provide: CategoryService,
useValue: {
getCategory: () => EMPTY,
searchCategories: () => EMPTY
getCategory: () => EMPTY
}
}]
});
@@ -472,22 +471,9 @@ describe('SearchFacetFiltersService', () => {
});
it('should load category names for cm:categories facet', () => {
const entry = {id: 'test-id-test', name: 'name'};
const entry = {id: 'test-id-test', name: 'name', path: '/categories/General/Test Category/Subcategory'};
searchFacetFiltersService.responseFacets = null;
spyOn(categoryService, 'getCategory').and.returnValue(of({entry}));
spyOn(categoryService, 'searchCategories').and.returnValue(of({
list: {
entries: [{
entry: {
...entry,
nodeType: 'node-type',
path: { name: '/categories/General/Test Category/Subcategory'},
isFolder: false,
isFile: false
}
}]
}
}));
queryBuilder.config = {
categories: [],
@@ -521,8 +507,7 @@ describe('SearchFacetFiltersService', () => {
searchFacetFiltersService.onDataLoaded(data);
expect(categoryService.getCategory).toHaveBeenCalledWith(entry.id);
expect(categoryService.searchCategories).toHaveBeenCalledWith(entry.name);
expect(categoryService.getCategory).toHaveBeenCalledWith(entry.id, { include: [ 'path' ]});
expect(searchFacetFiltersService.responseFacets[1].buckets.items[0].display).toBe(`Test Category/Subcategory/${entry.name}`);
expect(searchFacetFiltersService.responseFacets[1].buckets.length).toEqual(1);
expect(searchFacetFiltersService.responseFacets.length).toEqual(2);

View File

@@ -22,7 +22,7 @@ import { SEARCH_QUERY_SERVICE_TOKEN } from '../search-query-service.token';
import { SearchQueryBuilderService } from './search-query-builder.service';
import { TranslationService } from '@alfresco/adf-core';
import { SearchService } from './search.service';
import { catchError, concatMap, takeUntil } from 'rxjs/operators';
import { catchError, takeUntil } from 'rxjs/operators';
import { GenericBucket, GenericFacetResponse, ResultSetContext, ResultSetPaging } from '@alfresco/js-api';
import { SearchFilterList } from '../models/search-filter-list.model';
import { FacetFieldBucket } from '../models/facet-field-bucket.interface';
@@ -345,19 +345,14 @@ export class SearchFacetFiltersService implements OnDestroy {
private loadCategoryNames(bucketList: FacetFieldBucket[]) {
bucketList.forEach((item) => {
const categoryId = item.label.split('/').pop();
this.categoryService.getCategory(categoryId)
.pipe(
concatMap((categoryEntry) => this.categoryService.searchCategories(categoryEntry.entry.name)),
catchError(error => throwError(error))
)
this.categoryService.getCategory(categoryId, {include: ['path']})
.pipe(catchError(error => throwError(error)))
.subscribe(
result => {
category => {
const nextAfterGeneralPathPartIndex = 3;
const pathSeparator = '/';
const currentCat = result.list.entries.filter(entry => entry.entry.id === categoryId)[0];
const path = currentCat.entry.path.name.split(pathSeparator).slice(nextAfterGeneralPathPartIndex).join('/');
item.display = path ? `${path}/${currentCat.entry.name}` : currentCat.entry.name;
const path = category.entry.path.split(pathSeparator).slice(nextAfterGeneralPathPartIndex).join('/');
item.display = path ? `${path}/${category.entry.name}` : category.entry.name;
}
);
});