Skip to content

Commit 8409853

Browse files
committed
[DURACOM-191] Clean up suggestions-data.service implementation
1 parent 0047913 commit 8409853

3 files changed

Lines changed: 18 additions & 218 deletions

File tree

src/app/core/notifications/suggestion-data.service.spec.ts renamed to src/app/core/notifications/suggestions-data.service.spec.ts

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,12 @@ import { RemoteDataBuildService } from '../cache/builders/remote-data-build.serv
1212
import { RequestParam } from '../cache/models/request-param.model';
1313
import { ObjectCacheService } from '../cache/object-cache.service';
1414
import { RestResponse } from '../cache/response.models';
15-
import { DefaultChangeAnalyzer } from '../data/default-change-analyzer.service';
1615
import { RemoteData } from '../data/remote-data';
1716
import { RequestService } from '../data/request.service';
1817
import { RequestEntry } from '../data/request-entry.model';
1918
import { RequestEntryState } from '../data/request-entry-state.model';
2019
import { HALEndpointService } from '../shared/hal-endpoint.service';
21-
import { Suggestion } from './models/suggestion.model';
22-
import { SuggestionSource } from './models/suggestion-source.model';
23-
import { SuggestionTarget } from './models/suggestion-target.model';
24-
import { SuggestionSourceDataService } from './source/suggestion-source-data.service';
25-
import {
26-
SuggestionDataServiceImpl,
27-
SuggestionsDataService,
28-
} from './suggestions-data.service';
29-
import { SuggestionTargetDataService } from './target/suggestion-target-data.service';
20+
import { SuggestionsDataService } from './suggestions-data.service';
3021

3122
describe('SuggestionDataService test', () => {
3223
let scheduler: TestScheduler;
@@ -37,12 +28,6 @@ describe('SuggestionDataService test', () => {
3728
let halService: HALEndpointService;
3829
let notificationsService: NotificationsService;
3930
let http: HttpClient;
40-
let comparatorSuggestion: DefaultChangeAnalyzer<Suggestion>;
41-
let comparatorSuggestionSource: DefaultChangeAnalyzer<SuggestionSource>;
42-
let comparatorSuggestionTarget: DefaultChangeAnalyzer<SuggestionTarget>;
43-
let suggestionSourcesDataService: SuggestionSourceDataService;
44-
let suggestionTargetsDataService: SuggestionTargetDataService;
45-
let suggestionsDataService: SuggestionDataServiceImpl;
4631
let responseCacheEntry: RequestEntry;
4732

4833

@@ -62,9 +47,6 @@ describe('SuggestionDataService test', () => {
6247
halService,
6348
notificationsService,
6449
http,
65-
comparatorSuggestion,
66-
comparatorSuggestionSource,
67-
comparatorSuggestionTarget,
6850
);
6951
}
7052

@@ -74,9 +56,6 @@ describe('SuggestionDataService test', () => {
7456
objectCache = {} as ObjectCacheService;
7557
http = {} as HttpClient;
7658
notificationsService = {} as NotificationsService;
77-
comparatorSuggestion = {} as DefaultChangeAnalyzer<Suggestion>;
78-
comparatorSuggestionTarget = {} as DefaultChangeAnalyzer<SuggestionTarget>;
79-
comparatorSuggestionSource = {} as DefaultChangeAnalyzer<SuggestionSource>;
8059
responseCacheEntry = new RequestEntry();
8160
responseCacheEntry.request = { href: 'https://rest.api/' } as any;
8261
responseCacheEntry.response = new RestResponse(true, 200, 'Success');
@@ -99,75 +78,23 @@ describe('SuggestionDataService test', () => {
9978
buildList: cold('a', { a: remoteDataMocks.Success }),
10079
});
10180

102-
103-
suggestionSourcesDataService = jasmine.createSpyObj('suggestionSourcesDataService', {
104-
getSources: observableOf(null),
105-
});
106-
107-
suggestionTargetsDataService = jasmine.createSpyObj('suggestionTargetsDataService', {
108-
getTargets: observableOf(null),
109-
getTargetsByUser: observableOf(null),
110-
findById: observableOf(null),
111-
});
112-
113-
suggestionsDataService = jasmine.createSpyObj('suggestionsDataService', {
114-
searchBy: observableOf(null),
115-
delete: observableOf(null),
116-
});
117-
118-
11981
service = initTestService();
120-
/* eslint-disable-next-line @typescript-eslint/dot-notation */
121-
service['suggestionSourcesDataService'] = suggestionSourcesDataService;
122-
/* eslint-disable-next-line @typescript-eslint/dot-notation */
123-
service['suggestionTargetsDataService'] = suggestionTargetsDataService;
124-
/* eslint-disable-next-line @typescript-eslint/dot-notation */
125-
service['suggestionsDataService'] = suggestionsDataService;
126-
});
127-
128-
describe('Suggestion targets service', () => {
129-
it('should call suggestionSourcesDataService.getTargets', () => {
130-
const options = {
131-
searchParams: [new RequestParam('source', testSource)],
132-
};
133-
service.getTargets(testSource);
134-
expect(suggestionTargetsDataService.getTargets).toHaveBeenCalledWith('findBySource', options);
135-
});
136-
137-
it('should call suggestionSourcesDataService.getTargetsByUser', () => {
138-
const options = {
139-
searchParams: [new RequestParam('target', testUserId)],
140-
};
141-
service.getTargetsByUser(testUserId);
142-
expect(suggestionTargetsDataService.getTargetsByUser).toHaveBeenCalledWith(testUserId, options);
143-
});
144-
145-
it('should call suggestionSourcesDataService.getTargetById', () => {
146-
service.getTargetById('1');
147-
expect(suggestionTargetsDataService.findById).toHaveBeenCalledWith('1');
148-
});
149-
});
150-
151-
152-
describe('Suggestion sources service', () => {
153-
it('should call suggestionSourcesDataService.getSources', () => {
154-
service.getSources();
155-
expect(suggestionSourcesDataService.getSources).toHaveBeenCalled();
156-
});
15782
});
15883

15984
describe('Suggestion service', () => {
16085
it('should call suggestionsDataService.searchBy', () => {
86+
spyOn((service as any).searchData, 'searchBy').and.returnValue(observableOf(null));
16187
const options = {
16288
searchParams: [new RequestParam('target', testUserId), new RequestParam('source', testSource)],
16389
};
16490
service.getSuggestionsByTargetAndSource(testUserId, testSource);
165-
expect(suggestionsDataService.searchBy).toHaveBeenCalledWith('findByTargetAndSource', options, false, true);
91+
expect((service as any).searchData.searchBy).toHaveBeenCalledWith('findByTargetAndSource', options, false, true);
16692
});
16793

16894
it('should call suggestionsDataService.delete', () => {
95+
spyOn((service as any).deleteData, 'delete').and.returnValue(observableOf(null));
16996
service.deleteSuggestion('1');
170-
expect(suggestionsDataService.delete).toHaveBeenCalledWith('1');
97+
expect((service as any).deleteData.delete).toHaveBeenCalledWith('1');
17198
});
17299
});
173100

src/app/core/notifications/suggestions-data.service.ts

Lines changed: 12 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,66 @@
1-
/* eslint-disable max-classes-per-file */
21
import { HttpClient } from '@angular/common/http';
32
import { Injectable } from '@angular/core';
4-
import { Store } from '@ngrx/store';
53
import { Observable } from 'rxjs';
64

75
import { NotificationsService } from '../../shared/notifications/notifications.service';
86
import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
97
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
108
import { RequestParam } from '../cache/models/request-param.model';
119
import { ObjectCacheService } from '../cache/object-cache.service';
12-
import { CoreState } from '../core-state.model';
10+
import {
11+
DeleteData,
12+
DeleteDataImpl,
13+
} from '../data/base/delete-data';
1314
import { IdentifiableDataService } from '../data/base/identifiable-data.service';
14-
import { ChangeAnalyzer } from '../data/change-analyzer';
15-
import { DefaultChangeAnalyzer } from '../data/default-change-analyzer.service';
15+
import { SearchDataImpl } from '../data/base/search-data';
1616
import { FindListOptions } from '../data/find-list-options.model';
1717
import { PaginatedList } from '../data/paginated-list.model';
1818
import { RemoteData } from '../data/remote-data';
1919
import { RequestService } from '../data/request.service';
20-
import { UpdateDataServiceImpl } from '../data/update-data.service';
2120
import { HALEndpointService } from '../shared/hal-endpoint.service';
2221
import { NoContent } from '../shared/NoContent.model';
2322
import { Suggestion } from './models/suggestion.model';
24-
import { SuggestionSource } from './models/suggestion-source.model';
25-
import { SuggestionTarget } from './models/suggestion-target.model';
26-
import { SuggestionSourceDataService } from './source/suggestion-source-data.service';
27-
import { SuggestionTargetDataService } from './target/suggestion-target-data.service';
28-
29-
/* tslint:disable:max-classes-per-file */
30-
31-
/**
32-
* A private UpdateDataServiceImpl implementation to delegate specific methods to.
33-
*/
34-
export class SuggestionDataServiceImpl extends UpdateDataServiceImpl<Suggestion> {
35-
36-
/**
37-
* Initialize service variables
38-
* @param {RequestService} requestService
39-
* @param {RemoteDataBuildService} rdbService
40-
* @param {Store<CoreState>} store
41-
* @param {ObjectCacheService} objectCache
42-
* @param {HALEndpointService} halService
43-
* @param {NotificationsService} notificationsService
44-
* @param {HttpClient} http
45-
* @param {ChangeAnalyzer<Suggestion>} comparator
46-
* @param responseMsToLive
47-
*/
48-
constructor(
49-
protected requestService: RequestService,
50-
protected rdbService: RemoteDataBuildService,
51-
protected store: Store<CoreState>,
52-
protected objectCache: ObjectCacheService,
53-
protected halService: HALEndpointService,
54-
protected notificationsService: NotificationsService,
55-
protected http: HttpClient,
56-
protected comparator: ChangeAnalyzer<Suggestion>,
57-
protected responseMsToLive: number,
58-
) {
59-
super('suggestions', requestService, rdbService, objectCache, halService, notificationsService, comparator ,responseMsToLive);
60-
}
61-
}
6223

6324
/**
6425
* The service handling all Suggestion Target REST requests.
6526
*/
6627
@Injectable({ providedIn: 'root' })
6728
export class SuggestionsDataService extends IdentifiableDataService<Suggestion> {
29+
6830
protected searchFindBySourceMethod = 'findBySource';
6931
protected searchFindByTargetAndSourceMethod = 'findByTargetAndSource';
7032

71-
/**
72-
* A private UpdateDataServiceImpl implementation to delegate specific methods to.
73-
*/
74-
private suggestionsDataService: SuggestionDataServiceImpl;
75-
76-
/**
77-
* A private UpdateDataServiceImpl implementation to delegate specific methods to.
78-
*/
79-
private suggestionSourcesDataService: SuggestionSourceDataService;
80-
81-
/**
82-
* A private UpdateDataServiceImpl implementation to delegate specific methods to.
83-
*/
84-
private suggestionTargetsDataService: SuggestionTargetDataService;
33+
private deleteData: DeleteData<Suggestion>;
34+
private searchData: SearchDataImpl<Suggestion>;
8535

8636
/**
8737
* Initialize service variables
8838
* @param {RequestService} requestService
8939
* @param {RemoteDataBuildService} rdbService
90-
* @param {Store} store
9140
* @param {ObjectCacheService} objectCache
9241
* @param {HALEndpointService} halService
9342
* @param {NotificationsService} notificationsService
9443
* @param {HttpClient} http
95-
* @param {DefaultChangeAnalyzer<Suggestion>} comparatorSuggestions
96-
* @param {DefaultChangeAnalyzer<SuggestionSource>} comparatorSources
97-
* @param {DefaultChangeAnalyzer<SuggestionTarget>} comparatorTargets
9844
*/
9945
constructor(
10046
protected requestService: RequestService,
10147
protected rdbService: RemoteDataBuildService,
102-
protected store: Store<CoreState>,
10348
protected objectCache: ObjectCacheService,
10449
protected halService: HALEndpointService,
10550
protected notificationsService: NotificationsService,
10651
protected http: HttpClient,
107-
protected comparatorSuggestions: DefaultChangeAnalyzer<Suggestion>,
108-
protected comparatorSources: DefaultChangeAnalyzer<SuggestionSource>,
109-
protected comparatorTargets: DefaultChangeAnalyzer<SuggestionTarget>,
11052
) {
11153
super('suggestions', requestService, rdbService, objectCache, halService);
112-
this.suggestionsDataService = new SuggestionDataServiceImpl(requestService, rdbService, null, objectCache, halService, notificationsService, http, comparatorSuggestions, this.responseMsToLive);
113-
this.suggestionSourcesDataService = new SuggestionSourceDataService(requestService, rdbService, null, objectCache, halService, notificationsService, http, comparatorSources);
114-
this.suggestionTargetsDataService = new SuggestionTargetDataService(requestService, rdbService, null, objectCache, halService, notificationsService, http, comparatorTargets);
115-
}
116-
117-
/**
118-
* Return the list of Suggestion Sources
119-
*
120-
* @param options
121-
* Find list options object.
122-
* @return Observable<RemoteData<PaginatedList<SuggestionSource>>>
123-
* The list of Suggestion Sources.
124-
*/
125-
public getSources(options: FindListOptions = {}): Observable<RemoteData<PaginatedList<SuggestionSource>>> {
126-
return this.suggestionSourcesDataService.getSources(options);
127-
}
128-
129-
/**
130-
* Return the list of Suggestion Target for a given source
131-
*
132-
* @param source
133-
* The source for which to find targets.
134-
* @param options
135-
* Find list options object.
136-
* @param linksToFollow
137-
* List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved.
138-
* @return Observable<RemoteData<PaginatedList<SuggestionTarget>>>
139-
* The list of Suggestion Target.
140-
*/
141-
public getTargets(
142-
source: string,
143-
options: FindListOptions = {},
144-
...linksToFollow: FollowLinkConfig<SuggestionTarget>[]
145-
): Observable<RemoteData<PaginatedList<SuggestionTarget>>> {
146-
options.searchParams = [new RequestParam('source', source)];
147-
148-
return this.suggestionTargetsDataService.getTargets(this.searchFindBySourceMethod, options, ...linksToFollow);
149-
}
150-
151-
/**
152-
* Return the list of Suggestion Target for a given user
153-
*
154-
* @param userId
155-
* The user Id for which to find targets.
156-
* @param options
157-
* Find list options object.
158-
* @param linksToFollow
159-
* List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved.
160-
* @return Observable<RemoteData<PaginatedList<SuggestionTarget>>>
161-
* The list of Suggestion Target.
162-
*/
163-
public getTargetsByUser(
164-
userId: string,
165-
options: FindListOptions = {},
166-
...linksToFollow: FollowLinkConfig<SuggestionTarget>[]
167-
): Observable<RemoteData<PaginatedList<SuggestionTarget>>> {
168-
options.searchParams = [new RequestParam('target', userId)];
169-
return this.suggestionTargetsDataService.getTargetsByUser(userId, options, ...linksToFollow);
170-
}
171-
172-
/**
173-
* Return a Suggestion Target for a given id
174-
*
175-
* @param targetId
176-
* The target id to retrieve.
177-
*
178-
* @return Observable<RemoteData<SuggestionTarget>>
179-
* The list of Suggestion Target.
180-
*/
181-
public getTargetById(targetId: string): Observable<RemoteData<SuggestionTarget>> {
182-
return this.suggestionTargetsDataService.findById(targetId);
54+
this.deleteData = new DeleteDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, notificationsService, this.responseMsToLive, this.constructIdEndpoint);
55+
this.searchData = new SearchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
18356
}
18457

18558
/**
18659
* Used to delete Suggestion
18760
* @suggestionId
18861
*/
18962
public deleteSuggestion(suggestionId: string): Observable<RemoteData<NoContent>> {
190-
return this.suggestionsDataService.delete(suggestionId);
63+
return this.deleteData.delete(suggestionId);
19164
}
19265

19366
/**
@@ -215,7 +88,7 @@ export class SuggestionsDataService extends IdentifiableDataService<Suggestion>
21588
new RequestParam('source', source),
21689
];
21790

218-
return this.suggestionsDataService.searchBy(this.searchFindByTargetAndSourceMethod, options, false, true, ...linksToFollow);
91+
return this.searchData.searchBy(this.searchFindByTargetAndSourceMethod, options, false, true, ...linksToFollow);
21992
}
22093

22194
/**

src/app/notifications/suggestions.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class SuggestionsService {
171171
if (isNotEmpty(profile) && profile.hasSucceeded && isNotEmpty(profile.payload)) {
172172
return this.researcherProfileService.findRelatedItemId(profile.payload).pipe(
173173
mergeMap((itemId: string) => {
174-
return this.suggestionsDataService.getTargetsByUser(itemId).pipe(
174+
return this.suggestionTargetDataService.getTargetsByUser(itemId).pipe(
175175
getFirstSucceededRemoteListPayload(),
176176
);
177177
}),

0 commit comments

Comments
 (0)