Skip to content

Commit b86c467

Browse files
committed
Merge branch 'w2p-100302_Live-import-issues' into w2p-100302_Live-import-issues-7.5
2 parents 3851884 + 9f6616a commit b86c467

6 files changed

Lines changed: 129 additions & 17 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,62 @@ describe('BaseDataService', () => {
641641
});
642642
});
643643

644+
describe('hasCachedResponse', () => {
645+
it('should return false when the request will be dispatched', (done) => {
646+
const result = service.hasCachedResponse('test-href');
647+
648+
result.subscribe((hasCachedResponse) => {
649+
expect(hasCachedResponse).toBeFalse();
650+
done();
651+
});
652+
});
653+
654+
it('should return true when the request will not be dispatched', (done) => {
655+
(requestService.shouldDispatchRequest as jasmine.Spy).and.returnValue(false);
656+
const result = service.hasCachedResponse('test-href');
657+
658+
result.subscribe((hasCachedResponse) => {
659+
expect(hasCachedResponse).toBeTrue();
660+
done();
661+
});
662+
});
663+
});
664+
665+
describe('hasCachedErrorResponse', () => {
666+
it('should return false when no response is cached', (done) => {
667+
spyOn(service,'hasCachedResponse').and.returnValue(observableOf(false));
668+
const result = service.hasCachedErrorResponse('test-href');
669+
670+
result.subscribe((hasCachedErrorResponse) => {
671+
expect(hasCachedErrorResponse).toBeFalse();
672+
done();
673+
});
674+
});
675+
it('should return false when no error response is cached', (done) => {
676+
spyOn(service,'hasCachedResponse').and.returnValue(observableOf(true));
677+
spyOn(rdbService,'buildSingle').and.returnValue(createSuccessfulRemoteDataObject$({}));
678+
679+
const result = service.hasCachedErrorResponse('test-href');
680+
681+
result.subscribe((hasCachedErrorResponse) => {
682+
expect(hasCachedErrorResponse).toBeFalse();
683+
done();
684+
});
685+
});
686+
687+
it('should return true when an error response is cached', (done) => {
688+
spyOn(service,'hasCachedResponse').and.returnValue(observableOf(true));
689+
spyOn(rdbService,'buildSingle').and.returnValue(createFailedRemoteDataObject$());
690+
691+
const result = service.hasCachedErrorResponse('test-href');
692+
693+
result.subscribe((hasCachedErrorResponse) => {
694+
expect(hasCachedErrorResponse).toBeTrue();
695+
done();
696+
});
697+
});
698+
});
699+
644700
describe('addDependency', () => {
645701
let addDependencySpy;
646702

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,37 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
341341
}
342342
}
343343

344+
hasCachedResponse(href$: string | Observable<string>): Observable<boolean> {
345+
if (isNotEmpty(href$)) {
346+
if (typeof href$ === 'string') {
347+
href$ = observableOf(href$);
348+
}
349+
return href$.pipe(
350+
isNotEmptyOperator(),
351+
take(1),
352+
map((href: string) => {
353+
const requestId = this.requestService.generateRequestId();
354+
const request = new GetRequest(requestId, href);
355+
return !this.requestService.shouldDispatchRequest(request, true);
356+
}),
357+
);
358+
}
359+
}
360+
361+
hasCachedErrorResponse(href$: string | Observable<string>): Observable<boolean> {
362+
return this.hasCachedResponse(href$).pipe(
363+
switchMap((hasCachedResponse) => {
364+
if (hasCachedResponse) {
365+
return this.rdbService.buildSingle(href$).pipe(
366+
getFirstCompletedRemoteData(),
367+
map((rd => rd.isError || rd.isErrorStale))
368+
);
369+
}
370+
return observableOf(false);
371+
})
372+
);
373+
}
374+
344375
/**
345376
* Return the links to traverse from the root of the api to the
346377
* endpoint this DataService represents

src/app/core/data/external-source-data.service.spec.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ExternalSourceEntry } from '../shared/external-source-entry.model';
55
import { of as observableOf } from 'rxjs';
66
import { GetRequest } from './request.models';
77
import { testSearchDataImplementation } from './base/search-data.spec';
8+
import { take } from 'rxjs/operators';
89

910
describe('ExternalSourceService', () => {
1011
let service: ExternalSourceDataService;
@@ -64,19 +65,42 @@ describe('ExternalSourceService', () => {
6465
});
6566

6667
describe('getExternalSourceEntries', () => {
67-
let result;
6868

69-
beforeEach(() => {
70-
result = service.getExternalSourceEntries('test');
71-
});
69+
describe('when no error response is cached', () => {
70+
let result;
71+
beforeEach(() => {
72+
spyOn(service, 'hasCachedErrorResponse').and.returnValue(observableOf(false));
73+
result = service.getExternalSourceEntries('test');
74+
});
7275

73-
it('should send a GetRequest', () => {
74-
expect(requestService.send).toHaveBeenCalledWith(jasmine.any(GetRequest), true);
76+
it('should send a GetRequest', () => {
77+
result.pipe(take(1)).subscribe();
78+
expect(requestService.send).toHaveBeenCalledWith(jasmine.any(GetRequest), true);
79+
});
80+
81+
it('should return the entries', () => {
82+
result.subscribe((resultRD) => {
83+
expect(resultRD.payload.page).toBe(entries);
84+
});
85+
});
7586
});
7687

77-
it('should return the entries', () => {
78-
result.subscribe((resultRD) => {
79-
expect(resultRD.payload.page).toBe(entries);
88+
describe('when an error response is cached', () => {
89+
let result;
90+
beforeEach(() => {
91+
spyOn(service, 'hasCachedErrorResponse').and.returnValue(observableOf(true));
92+
result = service.getExternalSourceEntries('test');
93+
});
94+
95+
it('should send a GetRequest', () => {
96+
result.pipe(take(1)).subscribe();
97+
expect(requestService.send).toHaveBeenCalledWith(jasmine.any(GetRequest), false);
98+
});
99+
100+
it('should return the entries', () => {
101+
result.subscribe((resultRD) => {
102+
expect(resultRD.payload.page).toBe(entries);
103+
});
80104
});
81105
});
82106
});

src/app/core/data/external-source-data.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ export class ExternalSourceDataService extends IdentifiableDataService<ExternalS
7474
);
7575

7676
// TODO create a dedicated ExternalSourceEntryDataService and move this entire method to it. Then the "as any"s won't be necessary
77-
return this.findListByHref(href$, undefined, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow as any) as any;
77+
78+
return this.hasCachedErrorResponse(href$).pipe(
79+
switchMap((hasCachedErrorResponse) => {
80+
return this.findListByHref(href$, undefined, !hasCachedErrorResponse, reRequestOnStale, ...linksToFollow as any);
81+
})
82+
) as any;
7883
}
7984

8085
/**

src/app/shared/mocks/request.service.mock.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function getMockRequestService(requestEntry$: Observable<RequestEntry> =
1414
removeByHrefSubstring: observableOf(true),
1515
setStaleByHrefSubstring: observableOf(true),
1616
setStaleByUUID: observableOf(true),
17-
hasByHref$: observableOf(false)
17+
hasByHref$: observableOf(false),
18+
shouldDispatchRequest: true
1819
});
1920
}

src/app/submission/import-external/submission-import-external.component.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,12 @@ export class SubmissionImportExternalComponent implements OnInit, OnDestroy {
188188
this.retrieveExternalSourcesSub = this.reload$.pipe(
189189
filter((sourceQueryObject: ExternalSourceData) => isNotEmpty(sourceQueryObject.sourceId) && isNotEmpty(sourceQueryObject.query)),
190190
switchMap((sourceQueryObject: ExternalSourceData) => {
191-
const currentEntry = this.entriesRD$.getValue();
192-
let useCache = true;
193-
if (hasValue(currentEntry) && currentEntry.isError) {
194-
useCache = false;
195-
}
196191
const query = sourceQueryObject.query;
197192
this.routeData = sourceQueryObject;
198193
return this.searchConfigService.paginatedSearchOptions.pipe(
199194
tap(() => this.isLoading$.next(true)),
200195
filter((searchOptions) => searchOptions.query === query),
201-
mergeMap((searchOptions) => this.externalService.getExternalSourceEntries(this.routeData.sourceId, searchOptions, useCache).pipe(
196+
mergeMap((searchOptions) => this.externalService.getExternalSourceEntries(this.routeData.sourceId, searchOptions).pipe(
202197
getFinishedRemoteData(),
203198
))
204199
);

0 commit comments

Comments
 (0)