Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,34 @@ describe('SavedSearchesService', () => {
});
});

it('should fallback to preferences API if getting saved searches node ID fails', (done) => {
spyOn(authService, 'getUsername').and.returnValue(testUserName);
spyOn(localStorage, 'getItem').and.returnValue('');
const error = new Error(JSON.stringify({ error: { statusCode: 500 } }));
(service.nodesApi.getNode as jasmine.Spy).and.returnValue(Promise.reject(error));

service.getSavedSearches().subscribe((searches) => {
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
expect(searches.length).toBe(2);
done();
});
});

it('should handle 404 from getNode() by setting migration flag and falling back to preferences API', (done) => {
spyOn(authService, 'getUsername').and.returnValue(testUserName);
spyOn(localStorage, 'getItem').and.returnValue('');
spyOn(localStorage, 'setItem');
const notFoundError = new Error(JSON.stringify({ error: { statusCode: 404 } }));
(service.nodesApi.getNode as jasmine.Spy).and.returnValue(Promise.reject(notFoundError));

service.getSavedSearches().subscribe((searches) => {
expect(localStorage.setItem).toHaveBeenCalledWith(LOCAL_STORAGE_KEY, 'true');
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
expect(searches.length).toBe(2);
done();
});
});

/**
* Prepares default mocks for service
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export class SavedSearchesService {

readonly savedSearches$ = new ReplaySubject<SavedSearch[]>(1);

constructor(private readonly apiService: AlfrescoApiService, private readonly authService: AuthenticationService) {}
constructor(
private readonly apiService: AlfrescoApiService,
private readonly authService: AuthenticationService
) {}

init(): void {
this.fetchSavedSearches();
Expand All @@ -70,22 +73,19 @@ export class SavedSearchesService {
getSavedSearches(): Observable<SavedSearch[]> {
const savedSearchesMigrated = localStorage.getItem(this.getLocalStorageKey()) ?? '';
if (savedSearchesMigrated === 'true') {
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe(
map((preference) => JSON.parse(preference.entry.value)),
catchError(() => of([]))
);
return this.getSavedSearchesFromPreferenceApi();
} else {
return this.getSavedSearchesNodeId().pipe(
take(1),
concatMap(() => {
if (this.savedSearchFileNodeId !== '') {
return this.migrateSavedSearches();
} else {
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe(
map((preference) => JSON.parse(preference.entry.value)),
catchError(() => of([]))
);
return this.getSavedSearchesFromPreferenceApi();
}
}),
catchError(() => {
return this.getSavedSearchesFromPreferenceApi();
})
);
}
Expand Down Expand Up @@ -237,10 +237,8 @@ export class SavedSearchesService {
const errorStatusCode = JSON.parse(error.message).error.statusCode;
if (errorStatusCode === 404) {
localStorage.setItem(this.getLocalStorageKey(), 'true');
return '';
} else {
return throwError(() => error);
}
return throwError(() => error);
})
);
}
Expand Down Expand Up @@ -271,4 +269,11 @@ export class SavedSearchesService {
})
);
}

private getSavedSearchesFromPreferenceApi(): Observable<SavedSearch[]> {
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe(
map((preference) => JSON.parse(preference.entry.value)),
catchError(() => of([]))
);
}
}
Loading