Skip to content

Commit 9960b93

Browse files
committed
Fix cache invalidation for versioning
1 parent 404ccd9 commit 9960b93

9 files changed

Lines changed: 103 additions & 37 deletions

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
*
66
* http://www.dspace.org/license/
77
*/
8-
import { FindListOptions } from '../find-list-options.model';
98
import { getMockRequestService } from '../../../shared/mocks/request.service.mock';
109
import { HALEndpointServiceStub } from '../../../shared/testing/hal-endpoint-service.stub';
1110
import { getMockRemoteDataBuildService } from '../../../shared/mocks/remote-data-build.service.mock';
1211
import { followLink } from '../../../shared/utils/follow-link-config.model';
1312
import { TestScheduler } from 'rxjs/testing';
1413
import { RemoteData } from '../remote-data';
1514
import { RequestEntryState } from '../request-entry-state.model';
16-
import { Observable, of as observableOf } from 'rxjs';
15+
import { of as observableOf } from 'rxjs';
1716
import { RequestService } from '../request.service';
1817
import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
1918
import { HALEndpointService } from '../../shared/hal-endpoint.service';
2019
import { ObjectCacheService } from '../../cache/object-cache.service';
2120
import { IdentifiableDataService } from './identifiable-data.service';
2221
import { EMBED_SEPARATOR } from './base-data.service';
2322

24-
const endpoint = 'https://rest.api/core';
23+
const base = 'https://rest.api/core';
24+
const endpoint = 'test';
2525

2626
class TestService extends IdentifiableDataService<any> {
2727
constructor(
@@ -30,11 +30,7 @@ class TestService extends IdentifiableDataService<any> {
3030
protected objectCache: ObjectCacheService,
3131
protected halService: HALEndpointService,
3232
) {
33-
super(undefined, requestService, rdbService, objectCache, halService);
34-
}
35-
36-
public getBrowseEndpoint(options: FindListOptions = {}, linkPath: string = this.linkPath): Observable<string> {
37-
return observableOf(endpoint);
33+
super(endpoint, requestService, rdbService, objectCache, halService);
3834
}
3935
}
4036

@@ -51,7 +47,7 @@ describe('IdentifiableDataService', () => {
5147

5248
function initTestService(): TestService {
5349
requestService = getMockRequestService();
54-
halService = new HALEndpointServiceStub('url') as any;
50+
halService = new HALEndpointServiceStub(base) as any;
5551
rdbService = getMockRemoteDataBuildService();
5652
objectCache = {
5753

@@ -143,4 +139,12 @@ describe('IdentifiableDataService', () => {
143139
expect(result).toEqual(expected);
144140
});
145141
});
142+
143+
describe('invalidateById', () => {
144+
it('should invalidate the correct resource by href', () => {
145+
spyOn(service, 'invalidateByHref').and.returnValue(observableOf(true));
146+
service.invalidateById('123');
147+
expect(service.invalidateByHref).toHaveBeenCalledWith(`${base}/${endpoint}/123`);
148+
});
149+
});
146150
});

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { CacheableObject } from '../../cache/cacheable-object.model';
99
import { FollowLinkConfig } from '../../../shared/utils/follow-link-config.model';
1010
import { Observable } from 'rxjs';
11-
import { map } from 'rxjs/operators';
11+
import { map, switchMap, take } from 'rxjs/operators';
1212
import { RemoteData } from '../remote-data';
1313
import { BaseDataService } from './base-data.service';
1414
import { RequestService } from '../request.service';
@@ -80,4 +80,19 @@ export class IdentifiableDataService<T extends CacheableObject> extends BaseData
8080
return this.getEndpoint().pipe(
8181
map((endpoint: string) => this.getIDHref(endpoint, resourceID, ...linksToFollow)));
8282
}
83+
84+
/**
85+
* Invalidate a cached resource by its identifier
86+
* @param resourceID the identifier of the resource to invalidate
87+
*/
88+
invalidateById(resourceID: string): Observable<boolean> {
89+
const ok$ = this.getIDHrefObs(resourceID).pipe(
90+
take(1),
91+
switchMap((href) => this.invalidateByHref(href))
92+
);
93+
94+
ok$.subscribe();
95+
96+
return ok$;
97+
}
8398
}

src/app/core/data/version-history-data.service.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ describe('VersionHistoryDataService', () => {
6565
},
6666
},
6767
});
68+
const version1WithDraft = Object.assign(new Version(), {
69+
...version1,
70+
versionhistory: createSuccessfulRemoteDataObject$(versionHistoryDraft),
71+
});
6872
const versions = [version1, version2];
6973
versionHistory.versions = createSuccessfulRemoteDataObject$(createPaginatedList(versions));
7074
const item1 = Object.assign(new Item(), {
@@ -186,21 +190,18 @@ describe('VersionHistoryDataService', () => {
186190
});
187191

188192
describe('hasDraftVersion$', () => {
189-
beforeEach(waitForAsync(() => {
190-
versionService.findByHref.and.returnValue(createSuccessfulRemoteDataObject$<Version>(version1));
191-
}));
192193
it('should return false if draftVersion is false', fakeAsync(() => {
193-
versionService.getHistoryFromVersion.and.returnValue(of(versionHistory));
194+
versionService.findByHref.and.returnValue(createSuccessfulRemoteDataObject$<Version>(version1));
194195
service.hasDraftVersion$('href').subscribe((res) => {
195196
expect(res).toBeFalse();
196197
});
197198
}));
199+
198200
it('should return true if draftVersion is true', fakeAsync(() => {
199-
versionService.getHistoryFromVersion.and.returnValue(of(versionHistoryDraft));
201+
versionService.findByHref.and.returnValue(createSuccessfulRemoteDataObject$<Version>(version1WithDraft));
200202
service.hasDraftVersion$('href').subscribe((res) => {
201203
expect(res).toBeTrue();
202204
});
203205
}));
204206
});
205-
206207
});

src/app/core/data/version-history-data.service.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,24 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
9191
requestHeaders = requestHeaders.append('Content-Type', 'text/uri-list');
9292
requestOptions.headers = requestHeaders;
9393

94-
return this.halService.getEndpoint(this.versionsEndpoint).pipe(
94+
const response$ = this.halService.getEndpoint(this.versionsEndpoint).pipe(
9595
take(1),
9696
map((endpointUrl: string) => (summary?.length > 0) ? `${endpointUrl}?summary=${summary}` : `${endpointUrl}`),
9797
map((endpointURL: string) => new PostRequest(this.requestService.generateRequestId(), endpointURL, itemHref, requestOptions)),
9898
sendRequest(this.requestService),
9999
switchMap((restRequest: RestRequest) => this.rdbService.buildFromRequestUUID(restRequest.uuid)),
100100
getFirstCompletedRemoteData()
101101
) as Observable<RemoteData<Version>>;
102+
103+
response$.subscribe((versionRD: RemoteData<Version>) => {
104+
// invalidate version history
105+
// note: we should do this regardless of whether the request succeeds,
106+
// because it may have failed due to cached data that is out of date
107+
this.requestService.setStaleByHrefSubstring(versionRD.payload._links.self.href);
108+
this.requestService.setStaleByHrefSubstring(versionRD.payload._links.versionhistory.href);
109+
});
110+
111+
return response$;
102112
}
103113

104114
/**
@@ -158,14 +168,20 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
158168
* @returns `true` if a workspace item exists, `false` otherwise, or `null` if a version history does not exist
159169
*/
160170
hasDraftVersion$(versionHref: string): Observable<boolean> {
161-
return this.versionDataService.findByHref(versionHref, true, true, followLink('versionhistory')).pipe(
171+
return this.versionDataService.findByHref(versionHref, false, true, followLink('versionhistory')).pipe(
162172
getFirstCompletedRemoteData(),
163173
switchMap((res) => {
164174
if (res.hasSucceeded && !res.hasNoContent) {
165-
return of(res).pipe(
166-
getFirstSucceededRemoteDataPayload(),
167-
switchMap((version) => this.versionDataService.getHistoryFromVersion(version)),
168-
map((versionHistory) => versionHistory ? versionHistory.draftVersion : false),
175+
return res.payload.versionhistory.pipe(
176+
getFirstCompletedRemoteData(),
177+
map((versionHistoryRD) => {
178+
if (res.hasSucceeded) {
179+
const versionHistory = versionHistoryRD.payload;
180+
return versionHistory ? versionHistory.draftVersion : false;
181+
} else {
182+
return false;
183+
}
184+
}),
169185
);
170186
} else {
171187
return of(false);

src/app/core/submission/workflowitem-data.service.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { RemoteData } from '../data/remote-data';
1313
import { NoContent } from '../shared/NoContent.model';
1414
import { getFirstCompletedRemoteData } from '../shared/operators';
1515
import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
16-
import { WorkspaceItem } from './models/workspaceitem.model';
1716
import { RequestParam } from '../cache/models/request-param.model';
1817
import { FindListOptions } from '../data/find-list-options.model';
1918
import { IdentifiableDataService } from '../data/base/identifiable-data.service';
@@ -28,7 +27,6 @@ import { dataService } from '../data/base/data-service.decorator';
2827
@Injectable()
2928
@dataService(WorkflowItem.type)
3029
export class WorkflowItemDataService extends IdentifiableDataService<WorkflowItem> implements SearchData<WorkflowItem>, DeleteData<WorkflowItem> {
31-
protected linkPath = 'workflowitems';
3230
protected searchByItemLinkPath = 'item';
3331
protected responseMsToLive = 10 * 1000;
3432

@@ -42,7 +40,7 @@ export class WorkflowItemDataService extends IdentifiableDataService<WorkflowIte
4240
protected halService: HALEndpointService,
4341
protected notificationsService: NotificationsService,
4442
) {
45-
super('workspaceitems', requestService, rdbService, objectCache, halService);
43+
super('workflowitems', requestService, rdbService, objectCache, halService);
4644

4745
this.searchData = new SearchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
4846
this.deleteData = new DeleteDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, notificationsService, this.responseMsToLive, this.constructIdEndpoint);
@@ -105,7 +103,7 @@ export class WorkflowItemDataService extends IdentifiableDataService<WorkflowIte
105103
* @param options The {@link FindListOptions} object
106104
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved
107105
*/
108-
public findByItem(uuid: string, useCachedVersionIfAvailable = false, reRequestOnStale = true, options: FindListOptions = {}, ...linksToFollow: FollowLinkConfig<WorkspaceItem>[]): Observable<RemoteData<WorkspaceItem>> {
106+
public findByItem(uuid: string, useCachedVersionIfAvailable = false, reRequestOnStale = true, options: FindListOptions = {}, ...linksToFollow: FollowLinkConfig<WorkflowItem>[]): Observable<RemoteData<WorkflowItem>> {
109107
const findListOptions = new FindListOptions();
110108
findListOptions.searchParams = [new RequestParam('uuid', encodeURIComponent(uuid))];
111109
const href$ = this.searchData.getSearchByHref(this.searchByItemLinkPath, findListOptions, ...linksToFollow);
@@ -126,7 +124,7 @@ export class WorkflowItemDataService extends IdentifiableDataService<WorkflowIte
126124
* @return {Observable<RemoteData<PaginatedList<T>>}
127125
* Return an observable that emits response from the server
128126
*/
129-
public searchBy(searchMethod: string, options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig<WorkspaceItem>[]): Observable<RemoteData<PaginatedList<WorkspaceItem>>> {
127+
public searchBy(searchMethod: string, options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig<WorkflowItem>[]): Observable<RemoteData<PaginatedList<WorkflowItem>>> {
130128
return this.searchData.searchBy(searchMethod, options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
131129
}
132130

src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Injector, NO_ERRORS_SCHEMA } from '@angular/co
22
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
5-
import { of, of as observableOf } from 'rxjs';
5+
import { of as observableOf } from 'rxjs';
66

77
import { ClaimedTaskActionsApproveComponent } from './claimed-task-actions-approve.component';
88
import { TranslateLoaderMock } from '../../../mocks/translate-loader.mock';
@@ -18,6 +18,7 @@ import { Router } from '@angular/router';
1818
import { RouterStub } from '../../../testing/router.stub';
1919
import { SearchService } from '../../../../core/shared/search/search.service';
2020
import { RequestService } from '../../../../core/data/request.service';
21+
import { WorkflowItemDataService } from '../../../../core/submission/workflowitem-data.service';
2122

2223
let component: ClaimedTaskActionsApproveComponent;
2324
let fixture: ComponentFixture<ClaimedTaskActionsApproveComponent>;
@@ -27,6 +28,7 @@ const searchService = getMockSearchService();
2728
const requestService = getMockRequestService();
2829

2930
let mockPoolTaskDataService: PoolTaskDataService;
31+
let mockWorkflowItemDataService: WorkflowItemDataService;
3032

3133
describe('ClaimedTaskActionsApproveComponent', () => {
3234
const object = Object.assign(new ClaimedTask(), { id: 'claimed-task-1' });
@@ -36,6 +38,10 @@ describe('ClaimedTaskActionsApproveComponent', () => {
3638

3739
beforeEach(waitForAsync(() => {
3840
mockPoolTaskDataService = new PoolTaskDataService(null, null, null, null);
41+
mockWorkflowItemDataService = jasmine.createSpyObj('WorkflowItemDataService', {
42+
'invalidateByHref': observableOf(false),
43+
});
44+
3945
TestBed.configureTestingModule({
4046
imports: [
4147
TranslateModule.forRoot({
@@ -53,6 +59,7 @@ describe('ClaimedTaskActionsApproveComponent', () => {
5359
{ provide: SearchService, useValue: searchService },
5460
{ provide: RequestService, useValue: requestService },
5561
{ provide: PoolTaskDataService, useValue: mockPoolTaskDataService },
62+
{ provide: WorkflowItemDataService, useValue: mockWorkflowItemDataService },
5663
],
5764
declarations: [ClaimedTaskActionsApproveComponent],
5865
schemas: [NO_ERRORS_SCHEMA]
@@ -89,7 +96,7 @@ describe('ClaimedTaskActionsApproveComponent', () => {
8996

9097
beforeEach(() => {
9198
spyOn(component.processCompleted, 'emit');
92-
spyOn(component, 'startActionExecution').and.returnValue(of(null));
99+
spyOn(component, 'startActionExecution').and.returnValue(observableOf(null));
93100

94101
expectedBody = {
95102
[component.option]: 'true'

src/app/shared/mydspace-actions/claimed-task/approve/claimed-task-actions-approve.component.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { TranslateService } from '@ngx-translate/core';
1010
import { SearchService } from '../../../../core/shared/search/search.service';
1111
import { RequestService } from '../../../../core/data/request.service';
1212
import { ClaimedApprovedTaskSearchResult } from '../../../object-collection/shared/claimed-approved-task-search-result.model';
13+
import { WorkflowItemDataService } from '../../../../core/submission/workflowitem-data.service';
1314

1415
export const WORKFLOW_TASK_OPTION_APPROVE = 'submit_approve';
1516

@@ -28,12 +29,15 @@ export class ClaimedTaskActionsApproveComponent extends ClaimedTaskActionsAbstra
2829
*/
2930
option = WORKFLOW_TASK_OPTION_APPROVE;
3031

31-
constructor(protected injector: Injector,
32-
protected router: Router,
33-
protected notificationsService: NotificationsService,
34-
protected translate: TranslateService,
35-
protected searchService: SearchService,
36-
protected requestService: RequestService) {
32+
constructor(
33+
protected injector: Injector,
34+
protected router: Router,
35+
protected notificationsService: NotificationsService,
36+
protected translate: TranslateService,
37+
protected searchService: SearchService,
38+
protected requestService: RequestService,
39+
protected workflowItemDataService: WorkflowItemDataService,
40+
) {
3741
super(injector, router, notificationsService, translate, searchService, requestService);
3842
}
3943

@@ -48,4 +52,13 @@ export class ClaimedTaskActionsApproveComponent extends ClaimedTaskActionsAbstra
4852
return reloadedObject;
4953
}
5054

55+
public handleReloadableActionResponse(result: boolean, dso: DSpaceObject): void {
56+
super.handleReloadableActionResponse(result, dso);
57+
58+
// Item page version table includes labels for workflow Items, determined
59+
// based on the result of /workflowitems/search/item?uuid=...
60+
// In order for this label to be in sync with the workflow state, we should
61+
// invalidate WFIs as they are approved.
62+
this.workflowItemDataService.invalidateByHref(this.object?._links.workflowitem?.href);
63+
}
5164
}

src/app/submission/objects/submission-objects.effects.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ describe('SubmissionObjectEffects test suite', () => {
6666
let submissionServiceStub;
6767
let submissionJsonPatchOperationsServiceStub;
6868
let submissionObjectDataServiceStub;
69+
let workspaceItemDataService;
70+
6971
const collectionId: string = mockSubmissionCollectionId;
7072
const submissionId: string = mockSubmissionId;
7173
const submissionDefinitionResponse: any = mockSubmissionDefinitionResponse;
@@ -82,6 +84,10 @@ describe('SubmissionObjectEffects test suite', () => {
8284

8385
submissionServiceStub.hasUnsavedModification.and.returnValue(observableOf(true));
8486

87+
workspaceItemDataService = jasmine.createSpyObj('WorkspaceItemDataService', {
88+
invalidateById: observableOf(true),
89+
});
90+
8591
TestBed.configureTestingModule({
8692
imports: [
8793
StoreModule.forRoot({}, storeModuleConfig),
@@ -106,6 +112,7 @@ describe('SubmissionObjectEffects test suite', () => {
106112
{ provide: WorkflowItemDataService, useValue: {} },
107113
{ provide: HALEndpointService, useValue: {} },
108114
{ provide: SubmissionObjectDataService, useValue: submissionObjectDataServiceStub },
115+
{ provide: WorkspaceitemDataService, useValue: workspaceItemDataService },
109116
],
110117
});
111118

src/app/submission/objects/submission-objects.effects.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import parseSectionErrorPaths, { SectionErrorPath } from '../utils/parseSectionE
5555
import { FormState } from '../../shared/form/form.reducer';
5656
import { SubmissionSectionObject } from './submission-section-object.model';
5757
import { SubmissionSectionError } from './submission-section-error.model';
58+
import { WorkspaceitemDataService } from '../../core/submission/workspaceitem-data.service';
5859

5960
@Injectable()
6061
export class SubmissionObjectEffects {
@@ -258,6 +259,7 @@ export class SubmissionObjectEffects {
258259
depositSubmissionSuccess$ = createEffect(() => this.actions$.pipe(
259260
ofType(SubmissionObjectActionTypes.DEPOSIT_SUBMISSION_SUCCESS),
260261
tap(() => this.notificationsService.success(null, this.translate.get('submission.sections.general.deposit_success_notice'))),
262+
tap((action: DepositSubmissionSuccessAction) => this.workspaceItemDataService.invalidateById(action.payload.submissionId)),
261263
tap(() => this.submissionService.redirectToMyDSpace())), { dispatch: false });
262264

263265
/**
@@ -326,14 +328,17 @@ export class SubmissionObjectEffects {
326328
ofType(SubmissionObjectActionTypes.DISCARD_SUBMISSION_ERROR),
327329
tap(() => this.notificationsService.error(null, this.translate.get('submission.sections.general.discard_error_notice')))), { dispatch: false });
328330

329-
constructor(private actions$: Actions,
331+
constructor(
332+
private actions$: Actions,
330333
private notificationsService: NotificationsService,
331334
private operationsService: SubmissionJsonPatchOperationsService,
332335
private sectionService: SectionsService,
333336
private store$: Store<any>,
334337
private submissionService: SubmissionService,
335338
private submissionObjectService: SubmissionObjectDataService,
336-
private translate: TranslateService) {
339+
private translate: TranslateService,
340+
private workspaceItemDataService: WorkspaceitemDataService,
341+
) {
337342
}
338343

339344
/**

0 commit comments

Comments
 (0)