diff --git a/lib/content-services/src/lib/common/services/saved-searches.service.spec.ts b/lib/content-services/src/lib/common/services/saved-searches.service.spec.ts index e34e95cb6f..7f46da31fa 100644 --- a/lib/content-services/src/lib/common/services/saved-searches.service.spec.ts +++ b/lib/content-services/src/lib/common/services/saved-searches.service.spec.ts @@ -59,7 +59,6 @@ describe('SavedSearchesService', () => { service = TestBed.inject(SavedSearchesService); authService = TestBed.inject(AuthenticationService); spyOn(service.nodesApi, 'getNode').and.callFake(() => Promise.resolve({ entry: { id: testNodeId } } as NodeEntry)); - spyOn(service.searchApi, 'search').and.callFake(() => Promise.resolve({ list: { entries: [] } })); spyOn(service.nodesApi, 'createNode').and.callFake(() => Promise.resolve({ entry: { id: 'new-node-id' } })); spyOn(service.nodesApi, 'updateNodeContent').and.callFake(() => Promise.resolve({ entry: {} } as NodeEntry)); getNodeContentSpy = spyOn(service.nodesApi, 'getNodeContent').and.callFake(() => createBlob()); @@ -85,14 +84,15 @@ describe('SavedSearchesService', () => { }); it('should create config.json file if it does not exist', (done) => { + const error: Error = { name: 'test', message: '{ "error": { "statusCode": 404 } }' }; spyOn(authService, 'getUsername').and.callFake(() => testUserName); + service.nodesApi.getNode = jasmine.createSpy().and.returnValue(Promise.reject(error)); getNodeContentSpy.and.callFake(() => Promise.resolve(new Blob(['']))); service.innit(); service.getSavedSearches().subscribe((searches) => { - expect(service.nodesApi.getNode).toHaveBeenCalledWith('-my-'); - expect(service.searchApi.search).toHaveBeenCalled(); - expect(service.nodesApi.createNode).toHaveBeenCalledWith(testNodeId, jasmine.objectContaining({ name: 'config.json' })); + expect(service.nodesApi.getNode).toHaveBeenCalledWith('-my-', { relativePath: 'config.json' }); + expect(service.nodesApi.createNode).toHaveBeenCalledWith('-my-', jasmine.objectContaining({ name: 'config.json' })); expect(searches.length).toBe(0); done(); }); diff --git a/lib/content-services/src/lib/common/services/saved-searches.service.ts b/lib/content-services/src/lib/common/services/saved-searches.service.ts index 9f82ec1f37..d1f3b88b8e 100644 --- a/lib/content-services/src/lib/common/services/saved-searches.service.ts +++ b/lib/content-services/src/lib/common/services/saved-searches.service.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { NodesApi, NodeEntry, SearchApi, SEARCH_LANGUAGE, ResultSetPaging } from '@alfresco/js-api'; +import { NodesApi, NodeEntry } from '@alfresco/js-api'; import { Injectable } from '@angular/core'; import { Observable, of, from, ReplaySubject, throwError } from 'rxjs'; import { catchError, concatMap, first, map, switchMap, take, tap } from 'rxjs/operators'; @@ -27,12 +27,6 @@ import { AuthenticationService } from '@alfresco/adf-core'; providedIn: 'root' }) export class SavedSearchesService { - private _searchApi: SearchApi; - get searchApi(): SearchApi { - this._searchApi = this._searchApi ?? new SearchApi(this.apiService.getInstance()); - return this._searchApi; - } - private _nodesApi: NodesApi; get nodesApi(): NodesApi { this._nodesApi = this._nodesApi ?? new NodesApi(this.apiService.getInstance()); @@ -209,43 +203,35 @@ export class SavedSearchesService { this.currentUserLocalStorageKey = localStorageKey; let savedSearchesNodeId = localStorage.getItem(this.currentUserLocalStorageKey) ?? ''; if (savedSearchesNodeId === '') { - return from(this.nodesApi.getNode('-my-')).pipe( + return from(this.nodesApi.getNode('-my-', { relativePath: 'config.json' })).pipe( first(), - map((node) => node.entry.id), - concatMap((parentNodeId) => - from( - this.searchApi.search({ - query: { - language: SEARCH_LANGUAGE.AFTS, - query: `cm:name:"config.json" AND PARENT:"${parentNodeId}"` - } - }) - ).pipe( - first(), - concatMap((searchResult: ResultSetPaging) => { - if (searchResult.list.entries.length > 0) { - savedSearchesNodeId = searchResult.list.entries[0].entry.id; - localStorage.setItem(this.currentUserLocalStorageKey, savedSearchesNodeId); - } else { - return this.createSavedSearchesNode(parentNodeId).pipe( - first(), - map((node) => { - localStorage.setItem(this.currentUserLocalStorageKey, node.entry.id); - return node.entry.id; - }) - ); - } - this.savedSearchFileNodeId = savedSearchesNodeId; - return savedSearchesNodeId; - }) - ) - ) + concatMap((configNode) => { + savedSearchesNodeId = configNode.entry.id; + localStorage.setItem(this.currentUserLocalStorageKey, savedSearchesNodeId); + this.savedSearchFileNodeId = savedSearchesNodeId; + return savedSearchesNodeId; + }), + catchError((error) => { + const errorStatusCode = JSON.parse(error.message).error.statusCode; + if (errorStatusCode === 404) { + return this.createSavedSearchesNode('-my-').pipe( + first(), + map((node) => { + localStorage.setItem(this.currentUserLocalStorageKey, node.entry.id); + return node.entry.id; + }) + ); + } else { + return throwError(() => error); + } + }) ); } else { this.savedSearchFileNodeId = savedSearchesNodeId; return of(savedSearchesNodeId); } } + private createSavedSearchesNode(parentNodeId: string): Observable { return from(this.nodesApi.createNode(parentNodeId, { name: 'config.json', nodeType: 'cm:content' })); } diff --git a/lib/content-services/src/lib/search/services/base-query-builder.service.ts b/lib/content-services/src/lib/search/services/base-query-builder.service.ts index 42ca177c29..744f46782c 100644 --- a/lib/content-services/src/lib/search/services/base-query-builder.service.ts +++ b/lib/content-services/src/lib/search/services/base-query-builder.service.ts @@ -596,7 +596,6 @@ export abstract class BaseQueryBuilderService { * @param searchUrl search url to navigate to */ async navigateToSearch(query: string, searchUrl: string) { - this.update(); this.userQuery = query; await this.execute(); await this.router.navigate([searchUrl], {