Skip to content

Commit

Permalink
[ACS-8991] Saved searches potential file conflict fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKinas committed Nov 15, 2024
1 parent 07d68a1 commit 0cf4f3e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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());
Expand Down Expand Up @@ -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<NodeEntry> {
return from(this.nodesApi.createNode(parentNodeId, { name: 'config.json', nodeType: 'cm:content' }));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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], {
Expand Down

0 comments on commit 0cf4f3e

Please sign in to comment.