-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: MichalKinas <michal.kinas@hyland.com>
- Loading branch information
1 parent
537b4f6
commit d146225
Showing
45 changed files
with
1,591 additions
and
812 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
# Saved Searches Service | ||
|
||
Manages operations related to saving and retrieving user-defined searches in the Alfresco Process Services (APS) environment. | ||
|
||
## Class members | ||
|
||
### Properties | ||
|
||
- **savedSearches$**: [`ReplaySubject`](https://rxjs.dev/api/index/class/ReplaySubject)`<SavedSearch[]>`<br/> | ||
Stores the list of saved searches and emits new value whenever there is a change. | ||
|
||
### Methods | ||
|
||
#### getSavedSearches(): [`Observable`](https://rxjs.dev/api/index/class/Observable)`<SavedSearch[]>` | ||
|
||
Fetches the file with list of saved searches either from a locally cached node ID or by querying the APS server. Then it reads the file and maps JSON objects into SavedSearches | ||
|
||
- **Returns**: | ||
- [`Observable`](https://rxjs.dev/api/index/class/Observable)`<SavedSearch[]>` - An observable that emits the list of saved searches. | ||
|
||
#### saveSearch(newSaveSearch: Pick<SavedSearch, 'name' | 'description' | 'encodedUrl'>): [`Observable`](https://rxjs.dev/api/index/class/Observable)`<NodeEntry>` | ||
|
||
Saves a new search and updates the existing list of saved searches stored in file and in service property savedSearches$. | ||
|
||
- **Parameters**: | ||
- `newSaveSearch`: An object containing the `name`, `description`, and `encodedUrl` of the new search. | ||
|
||
- **Returns**: | ||
- [`Observable`](https://rxjs.dev/api/index/class/Observable)`<NodeEntry>` - An observable that emits the response of the node entry after saving. | ||
|
||
### Usage Examples | ||
|
||
#### Fetching Saved Searches | ||
|
||
The following example shows how to fetch saved searches: | ||
|
||
```typescript | ||
this.savedSearchService.getSavedSearches().subscribe((searches: SavedSearch[]) => { | ||
console.log('Saved searches:', searches); | ||
}); | ||
``` | ||
|
||
#### Saving a New Search | ||
|
||
To save a new search: | ||
|
||
```typescript | ||
const newSearch = { name: 'New Search', description: 'A sample search', encodedUrl: 'url3' }; | ||
this.savedSearchService.saveSearch(newSearch).subscribe((response) => { | ||
console.log('Saved new search:', response); | ||
}); | ||
``` | ||
|
||
#### Creating Saved Searches Node | ||
|
||
When the saved searches file does not exist, it will be created: | ||
|
||
```typescript | ||
this.savedSearchService.createSavedSearchesNode('parent-node-id').subscribe((node) => { | ||
console.log('Created saved-searches.json node:', node); | ||
}); | ||
``` | ||
|
23 changes: 23 additions & 0 deletions
23
lib/content-services/src/lib/common/interfaces/saved-search.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/*! | ||
* @license | ||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export interface SavedSearch { | ||
name: string; | ||
description?: string; | ||
encodedUrl: string; | ||
order: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
lib/content-services/src/lib/common/services/saved-searches.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/*! | ||
* @license | ||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { TestBed } from '@angular/core/testing'; | ||
import { AlfrescoApiService } from '../../services/alfresco-api.service'; | ||
import { NodeEntry } from '@alfresco/js-api'; | ||
import { SavedSearchesService } from './saved-searches.service'; | ||
import { AlfrescoApiServiceMock } from '@alfresco/adf-content-services'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { AuthenticationService } from '@alfresco/adf-core'; | ||
import { Subject } from 'rxjs'; | ||
|
||
describe('SavedSearchesService', () => { | ||
let service: SavedSearchesService; | ||
let authService: AuthenticationService; | ||
let testUserName: string; | ||
|
||
const testNodeId = 'test-node-id'; | ||
const SAVED_SEARCHES_NODE_ID = 'saved-searches-node-id__'; | ||
const SAVED_SEARCHES_CONTENT = JSON.stringify([ | ||
{ name: 'Search 1', description: 'Description 1', encodedUrl: 'url1', order: 0 }, | ||
{ name: 'Search 2', description: 'Description 2', encodedUrl: 'url2', order: 1 } | ||
]); | ||
|
||
/** | ||
* Creates a stub with Promise returning a Blob | ||
* | ||
* @returns Promise with Blob | ||
*/ | ||
function createBlob() { | ||
return Promise.resolve(new Blob([SAVED_SEARCHES_CONTENT])); | ||
} | ||
|
||
beforeEach(() => { | ||
testUserName = 'test-user'; | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [ | ||
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock }, | ||
{ provide: AuthenticationService, useValue: { getUsername: () => {}, onLogin: new Subject() } }, | ||
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: [] } })); | ||
}); | ||
|
||
afterEach(() => { | ||
localStorage.removeItem(SAVED_SEARCHES_NODE_ID + testUserName); | ||
}); | ||
|
||
it('should retrieve saved searches from the saved-searches.json file', (done) => { | ||
spyOn(authService, 'getUsername').and.callFake(() => testUserName); | ||
spyOn(localStorage, 'getItem').and.callFake(() => testNodeId); | ||
spyOn(service.nodesApi, 'getNodeContent').and.callFake(() => createBlob()); | ||
service.innit(); | ||
|
||
service.getSavedSearches().subscribe((searches) => { | ||
expect(localStorage.getItem).toHaveBeenCalledWith(SAVED_SEARCHES_NODE_ID + testUserName); | ||
expect(service.nodesApi.getNodeContent).toHaveBeenCalledWith(testNodeId); | ||
expect(searches.length).toBe(2); | ||
expect(searches[0].name).toBe('Search 1'); | ||
expect(searches[1].name).toBe('Search 2'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should create saved-searches.json file if it does not exist', (done) => { | ||
spyOn(authService, 'getUsername').and.callFake(() => testUserName); | ||
spyOn(service.nodesApi, 'createNode').and.callFake(() => Promise.resolve({ entry: { id: 'new-node-id' } })); | ||
spyOn(service.nodesApi, 'getNodeContent').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: 'saved-searches.json' })); | ||
expect(searches.length).toBe(0); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should save a new search', (done) => { | ||
spyOn(authService, 'getUsername').and.callFake(() => testUserName); | ||
const nodeId = 'saved-searches-node-id'; | ||
spyOn(localStorage, 'getItem').and.callFake(() => nodeId); | ||
spyOn(service.nodesApi, 'getNodeContent').and.callFake(() => createBlob()); | ||
const newSearch = { name: 'Search 3', description: 'Description 3', encodedUrl: 'url3' }; | ||
spyOn(service.nodesApi, 'updateNodeContent').and.callFake(() => Promise.resolve({ entry: {} } as NodeEntry)); | ||
service.innit(); | ||
|
||
service.saveSearch(newSearch).subscribe(() => { | ||
expect(service.nodesApi.updateNodeContent).toHaveBeenCalledWith(nodeId, jasmine.any(String)); | ||
expect(service.savedSearches$).toBeDefined(); | ||
service.savedSearches$.subscribe((searches) => { | ||
expect(searches.length).toBe(3); | ||
expect(searches[2].name).toBe('Search 3'); | ||
expect(searches[2].order).toBe(2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should emit initial saved searches on subscription', (done) => { | ||
const nodeId = 'saved-searches-node-id'; | ||
spyOn(localStorage, 'getItem').and.returnValue(nodeId); | ||
spyOn(service.nodesApi, 'getNodeContent').and.returnValue(createBlob()); | ||
service.innit(); | ||
|
||
service.savedSearches$.pipe().subscribe((searches) => { | ||
expect(searches.length).toBe(2); | ||
expect(searches[0].name).toBe('Search 1'); | ||
done(); | ||
}); | ||
|
||
service.getSavedSearches().subscribe(); | ||
}); | ||
|
||
it('should emit updated saved searches after saving a new search', (done) => { | ||
spyOn(authService, 'getUsername').and.callFake(() => testUserName); | ||
spyOn(localStorage, 'getItem').and.callFake(() => testNodeId); | ||
spyOn(service.nodesApi, 'getNodeContent').and.callFake(() => createBlob()); | ||
const newSearch = { name: 'Search 3', description: 'Description 3', encodedUrl: 'url3' }; | ||
spyOn(service.nodesApi, 'updateNodeContent').and.callFake(() => Promise.resolve({ entry: {} } as NodeEntry)); | ||
service.innit(); | ||
|
||
let emissionCount = 0; | ||
|
||
service.savedSearches$.subscribe((searches) => { | ||
emissionCount++; | ||
if (emissionCount === 1) { | ||
expect(searches.length).toBe(2); | ||
} | ||
if (emissionCount === 2) { | ||
expect(searches.length).toBe(3); | ||
expect(searches[2].name).toBe('Search 3'); | ||
done(); | ||
} | ||
}); | ||
|
||
service.saveSearch(newSearch).subscribe(); | ||
}); | ||
}); |
Oops, something went wrong.