Skip to content

Commit c3fce61

Browse files
authored
Merge pull request DSpace#5467 from 4Science/task/main/DURACOM-469
Fix DSpace#5410: fix issue with missing update on CMS metadata and end user agreement
2 parents 993c89a + c6ef1d9 commit c3fce61

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
lines changed

src/app/core/data/site-data.service.spec.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import {
55
import { TestScheduler } from 'rxjs/testing';
66

77
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
8+
import { RequestParam } from '../cache/models/request-param.model';
89
import { ObjectCacheService } from '../cache/object-cache.service';
910
import { HALEndpointService } from '../shared/hal-endpoint.service';
1011
import { Site } from '../shared/site.model';
1112
import { createPaginatedList } from '../testing/utils.test';
12-
import { createSuccessfulRemoteDataObject } from '../utilities/remote-data.utils';
13+
import {
14+
createFailedRemoteDataObject,
15+
createSuccessfulRemoteDataObject,
16+
} from '../utilities/remote-data.utils';
1317
import { testFindAllDataImplementation } from './base/find-all-data.spec';
1418
import { DefaultChangeAnalyzer } from './default-change-analyzer.service';
1519
import { FindListOptions } from './find-list-options.model';
@@ -78,14 +82,45 @@ describe('SiteDataService', () => {
7882
});
7983

8084
describe('find', () => {
81-
it('should return the Site object', () => {
82-
83-
spyOn(service, 'findAll').and.returnValue(cold('a', {
85+
it('should call findAll with allLanguages projection searchParam and return the first Site object', () => {
86+
const findAllSpy = spyOn(service, 'findAll').and.returnValue(cold('a', {
8487
a: createSuccessfulRemoteDataObject(createPaginatedList([testObject])),
8588
}));
8689

90+
const result = service.find(options);
8791
const expected = cold('(b|)', { b: testObject });
88-
const result = service.find();
92+
93+
expect(result).toBeObservable(expected);
94+
95+
const calledOptions: FindListOptions = findAllSpy.calls.mostRecent().args[0];
96+
const projectionParam = calledOptions.searchParams?.find((p: RequestParam) => p.fieldName === 'projection');
97+
expect(projectionParam).toBeDefined();
98+
expect(projectionParam.fieldValue).toBe('allLanguages');
99+
});
100+
101+
it('should fall back to findAll without searchParams when first call fails, and return the Site object', () => {
102+
const findAllSpy = spyOn(service, 'findAll').and.returnValues(
103+
cold('a', { a: createFailedRemoteDataObject<any>('Error', 500) }),
104+
cold('b', { b: createSuccessfulRemoteDataObject(createPaginatedList([testObject])) }),
105+
);
106+
107+
const result = service.find(options);
108+
const expected = cold('(b|)', { b: testObject });
109+
110+
expect(result).toBeObservable(expected);
111+
112+
// Second call should use original options (without injected searchParams)
113+
const fallbackOptions: FindListOptions = findAllSpy.calls.argsFor(1)[0];
114+
expect(fallbackOptions).toEqual(options);
115+
});
116+
117+
it('should return null when both findAll calls fail', () => {
118+
spyOn(service, 'findAll').and.returnValue(
119+
cold('a', { a: createFailedRemoteDataObject<any>('Error', 500) }),
120+
);
121+
122+
const result = service.find(options);
123+
const expected = cold('(b|)', { b: null });
89124

90125
expect(result).toBeObservable(expected);
91126
});

src/app/core/data/site-data.service.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { Injectable } from '@angular/core';
22
import { FollowLinkConfig } from '@dspace/core/shared/follow-link-config.model';
33
import { Operation } from 'fast-json-patch/module/core';
4-
import { Observable } from 'rxjs';
5-
import { map } from 'rxjs/operators';
4+
import {
5+
Observable,
6+
of,
7+
} from 'rxjs';
8+
import {
9+
map,
10+
switchMap,
11+
} from 'rxjs/operators';
612

713
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
14+
import { RequestParam } from '../cache/models/request-param.model';
815
import { ObjectCacheService } from '../cache/object-cache.service';
916
import { HALEndpointService } from '../shared/hal-endpoint.service';
10-
import { getFirstSucceededRemoteData } from '../shared/operators';
17+
import { getFirstCompletedRemoteData } from '../shared/operators';
1118
import { Site } from '../shared/site.model';
1219
import { BaseDataService } from './base/base-data.service';
1320
import {
@@ -48,12 +55,32 @@ export class SiteDataService extends BaseDataService<Site> implements FindAllDat
4855

4956
/**
5057
* Retrieve the Site Object
58+
*
59+
* @param options Find list options object
60+
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
61+
* no valid cached version. Defaults to true
62+
* @param reRequestOnStale Whether or not the request should automatically be re-
63+
* requested after the response becomes stale
64+
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
65+
* {@link HALLink}s should be automatically resolved
66+
* @return {Observable<RemoteData<PaginatedList<T>>>}
67+
* Return an observable that emits object list
5168
*/
52-
find(): Observable<Site> {
53-
return this.findAll().pipe(
54-
getFirstSucceededRemoteData(),
55-
map((remoteData: RemoteData<PaginatedList<Site>>) => remoteData.payload),
56-
map((list: PaginatedList<Site>) => list.page[0]),
69+
find(options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig<Site>[]): Observable<Site> {
70+
const searchParams: RequestParam[] = [new RequestParam('projection', 'allLanguages')];
71+
const findOptions = Object.assign(new FindListOptions(), options, { searchParams });
72+
return this.findAll(findOptions, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow).pipe(
73+
getFirstCompletedRemoteData(),
74+
switchMap((remoteData: RemoteData<PaginatedList<Site>>) => {
75+
if (remoteData.hasSucceeded) {
76+
return of(remoteData.payload.page[0]);
77+
} else {
78+
return this.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow).pipe(
79+
getFirstCompletedRemoteData(),
80+
map((rd: RemoteData<PaginatedList<Site>>) => rd.hasSucceeded ? rd.payload.page[0] : null),
81+
);
82+
}
83+
}),
5784
);
5885
}
5986

0 commit comments

Comments
 (0)