[ACS-8991] Saved searches potential file conflict fix (#10393)

This commit is contained in:
MichalKinas
2024-11-18 13:14:42 +01:00
committed by GitHub
parent 07d68a1e4b
commit 3adccc132e
3 changed files with 27 additions and 42 deletions

View File

@@ -59,7 +59,6 @@ describe('SavedSearchesService', () => {
service = TestBed.inject(SavedSearchesService); service = TestBed.inject(SavedSearchesService);
authService = TestBed.inject(AuthenticationService); authService = TestBed.inject(AuthenticationService);
spyOn(service.nodesApi, 'getNode').and.callFake(() => Promise.resolve({ entry: { id: testNodeId } } as NodeEntry)); 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, 'createNode').and.callFake(() => Promise.resolve({ entry: { id: 'new-node-id' } }));
spyOn(service.nodesApi, 'updateNodeContent').and.callFake(() => Promise.resolve({ entry: {} } as NodeEntry)); spyOn(service.nodesApi, 'updateNodeContent').and.callFake(() => Promise.resolve({ entry: {} } as NodeEntry));
getNodeContentSpy = spyOn(service.nodesApi, 'getNodeContent').and.callFake(() => createBlob()); 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) => { 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); spyOn(authService, 'getUsername').and.callFake(() => testUserName);
service.nodesApi.getNode = jasmine.createSpy().and.returnValue(Promise.reject(error));
getNodeContentSpy.and.callFake(() => Promise.resolve(new Blob(['']))); getNodeContentSpy.and.callFake(() => Promise.resolve(new Blob([''])));
service.innit(); service.innit();
service.getSavedSearches().subscribe((searches) => { service.getSavedSearches().subscribe((searches) => {
expect(service.nodesApi.getNode).toHaveBeenCalledWith('-my-'); expect(service.nodesApi.getNode).toHaveBeenCalledWith('-my-', { relativePath: 'config.json' });
expect(service.searchApi.search).toHaveBeenCalled(); expect(service.nodesApi.createNode).toHaveBeenCalledWith('-my-', jasmine.objectContaining({ name: 'config.json' }));
expect(service.nodesApi.createNode).toHaveBeenCalledWith(testNodeId, jasmine.objectContaining({ name: 'config.json' }));
expect(searches.length).toBe(0); expect(searches.length).toBe(0);
done(); done();
}); });

View File

@@ -15,7 +15,7 @@
* limitations under the License. * 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 { Injectable } from '@angular/core';
import { Observable, of, from, ReplaySubject, throwError } from 'rxjs'; import { Observable, of, from, ReplaySubject, throwError } from 'rxjs';
import { catchError, concatMap, first, map, switchMap, take, tap } from 'rxjs/operators'; import { catchError, concatMap, first, map, switchMap, take, tap } from 'rxjs/operators';
@@ -27,12 +27,6 @@ import { AuthenticationService } from '@alfresco/adf-core';
providedIn: 'root' providedIn: 'root'
}) })
export class SavedSearchesService { export class SavedSearchesService {
private _searchApi: SearchApi;
get searchApi(): SearchApi {
this._searchApi = this._searchApi ?? new SearchApi(this.apiService.getInstance());
return this._searchApi;
}
private _nodesApi: NodesApi; private _nodesApi: NodesApi;
get nodesApi(): NodesApi { get nodesApi(): NodesApi {
this._nodesApi = this._nodesApi ?? new NodesApi(this.apiService.getInstance()); this._nodesApi = this._nodesApi ?? new NodesApi(this.apiService.getInstance());
@@ -209,43 +203,35 @@ export class SavedSearchesService {
this.currentUserLocalStorageKey = localStorageKey; this.currentUserLocalStorageKey = localStorageKey;
let savedSearchesNodeId = localStorage.getItem(this.currentUserLocalStorageKey) ?? ''; let savedSearchesNodeId = localStorage.getItem(this.currentUserLocalStorageKey) ?? '';
if (savedSearchesNodeId === '') { if (savedSearchesNodeId === '') {
return from(this.nodesApi.getNode('-my-')).pipe( return from(this.nodesApi.getNode('-my-', { relativePath: 'config.json' })).pipe(
first(), first(),
map((node) => node.entry.id), concatMap((configNode) => {
concatMap((parentNodeId) => savedSearchesNodeId = configNode.entry.id;
from( localStorage.setItem(this.currentUserLocalStorageKey, savedSearchesNodeId);
this.searchApi.search({ this.savedSearchFileNodeId = savedSearchesNodeId;
query: { return savedSearchesNodeId;
language: SEARCH_LANGUAGE.AFTS, }),
query: `cm:name:"config.json" AND PARENT:"${parentNodeId}"` catchError((error) => {
} const errorStatusCode = JSON.parse(error.message).error.statusCode;
}) if (errorStatusCode === 404) {
).pipe( return this.createSavedSearchesNode('-my-').pipe(
first(), first(),
concatMap((searchResult: ResultSetPaging) => { map((node) => {
if (searchResult.list.entries.length > 0) { localStorage.setItem(this.currentUserLocalStorageKey, node.entry.id);
savedSearchesNodeId = searchResult.list.entries[0].entry.id; return node.entry.id;
localStorage.setItem(this.currentUserLocalStorageKey, savedSearchesNodeId); })
} else { );
return this.createSavedSearchesNode(parentNodeId).pipe( } else {
first(), return throwError(() => error);
map((node) => { }
localStorage.setItem(this.currentUserLocalStorageKey, node.entry.id); })
return node.entry.id;
})
);
}
this.savedSearchFileNodeId = savedSearchesNodeId;
return savedSearchesNodeId;
})
)
)
); );
} else { } else {
this.savedSearchFileNodeId = savedSearchesNodeId; this.savedSearchFileNodeId = savedSearchesNodeId;
return of(savedSearchesNodeId); return of(savedSearchesNodeId);
} }
} }
private createSavedSearchesNode(parentNodeId: string): Observable<NodeEntry> { private createSavedSearchesNode(parentNodeId: string): Observable<NodeEntry> {
return from(this.nodesApi.createNode(parentNodeId, { name: 'config.json', nodeType: 'cm:content' })); return from(this.nodesApi.createNode(parentNodeId, { name: 'config.json', nodeType: 'cm:content' }));
} }

View File

@@ -596,7 +596,6 @@ export abstract class BaseQueryBuilderService {
* @param searchUrl search url to navigate to * @param searchUrl search url to navigate to
*/ */
async navigateToSearch(query: string, searchUrl: string) { async navigateToSearch(query: string, searchUrl: string) {
this.update();
this.userQuery = query; this.userQuery = query;
await this.execute(); await this.execute();
await this.router.navigate([searchUrl], { await this.router.navigate([searchUrl], {