93889: Invalidate EPerson when their Groups change and groups when a subgroup is added

This commit is contained in:
Alexandre Vryghem
2022-08-16 18:27:01 +02:00
parent f6d2014bf4
commit 762f8930b7
5 changed files with 222 additions and 16 deletions

View File

@@ -377,7 +377,9 @@ export class GroupFormComponent implements OnInit, OnDestroy {
*/ */
setActiveGroup(groupId: string) { setActiveGroup(groupId: string) {
this.groupDataService.cancelEditGroup(); this.groupDataService.cancelEditGroup();
this.groupDataService.findById(groupId) const nextGroup$ = this.groupDataService.findById(groupId);
this.subs.push(nextGroup$.subscribe());
nextGroup$
.pipe( .pipe(
getFirstSucceededRemoteData(), getFirstSucceededRemoteData(),
getRemoteDataPayload()) getRemoteDataPayload())

View File

@@ -174,7 +174,7 @@ export class MembersListComponent implements OnInit, OnDestroy {
return this.ePersonDataService.findAllByHref(group._links.epersons.href, { return this.ePersonDataService.findAllByHref(group._links.epersons.href, {
currentPage: 1, currentPage: 1,
elementsPerPage: 9999 elementsPerPage: 9999
}, false) })
.pipe( .pipe(
getFirstSucceededRemoteData(), getFirstSucceededRemoteData(),
getRemoteDataPayload(), getRemoteDataPayload(),

View File

@@ -26,6 +26,12 @@ import { EPersonMock, EPersonMock2 } from '../../shared/testing/eperson.mock';
import { createPaginatedList, createRequestEntry$ } from '../../shared/testing/utils.test'; import { createPaginatedList, createRequestEntry$ } from '../../shared/testing/utils.test';
import { CoreState } from '../core-state.model'; import { CoreState } from '../core-state.model';
import { FindListOptions } from '../data/find-list-options.model'; import { FindListOptions } from '../data/find-list-options.model';
import { DataService } from '../data/data.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { getMockLinkService } from '../../shared/mocks/link-service.mock';
import { DataServiceStub } from '../../shared/testing/data-service.stub';
import { of as observableOf } from 'rxjs';
import { ObjectCacheEntry } from '../cache/object-cache.reducer';
describe('GroupDataService', () => { describe('GroupDataService', () => {
let service: GroupDataService; let service: GroupDataService;
@@ -38,6 +44,8 @@ describe('GroupDataService', () => {
let groups$; let groups$;
let halService; let halService;
let rdbService; let rdbService;
let objectCache: ObjectCacheService;
let dataService: DataServiceStub;
function init() { function init() {
restEndpointURL = 'https://dspace.4science.it/dspace-spring-rest/api/eperson'; restEndpointURL = 'https://dspace.4science.it/dspace-spring-rest/api/eperson';
@@ -46,6 +54,8 @@ describe('GroupDataService', () => {
groups$ = createSuccessfulRemoteDataObject$(createPaginatedList(groups)); groups$ = createSuccessfulRemoteDataObject$(createPaginatedList(groups));
rdbService = getMockRemoteDataBuildServiceHrefMap(undefined, { 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups': groups$ }); rdbService = getMockRemoteDataBuildServiceHrefMap(undefined, { 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups': groups$ });
halService = new HALEndpointServiceStub(restEndpointURL); halService = new HALEndpointServiceStub(restEndpointURL);
objectCache = new ObjectCacheService(store, getMockLinkService());
dataService = new DataServiceStub();
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
CommonModule, CommonModule,
@@ -58,7 +68,9 @@ describe('GroupDataService', () => {
}), }),
], ],
declarations: [], declarations: [],
providers: [], providers: [
{ provide: DataService, useValue: dataService },
],
schemas: [CUSTOM_ELEMENTS_SCHEMA] schemas: [CUSTOM_ELEMENTS_SCHEMA]
}); });
} }
@@ -71,7 +83,7 @@ describe('GroupDataService', () => {
requestService, requestService,
rdbService, rdbService,
store, store,
null, objectCache,
halService, halService,
null, null,
); );
@@ -109,6 +121,10 @@ describe('GroupDataService', () => {
describe('addSubGroupToGroup', () => { describe('addSubGroupToGroup', () => {
beforeEach(() => { beforeEach(() => {
spyOn(objectCache, 'getByHref').and.returnValue(observableOf({
requestUUIDs: ['request1', 'request2']
} as ObjectCacheEntry));
spyOn(dataService, 'invalidateByHref');
service.addSubGroupToGroup(GroupMock, GroupMock2).subscribe(); service.addSubGroupToGroup(GroupMock, GroupMock2).subscribe();
}); });
it('should send PostRequest to eperson/groups/group-id/subgroups endpoint with new subgroup link in body', () => { it('should send PostRequest to eperson/groups/group-id/subgroups endpoint with new subgroup link in body', () => {
@@ -119,20 +135,40 @@ describe('GroupDataService', () => {
const expected = new PostRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.subgroupsEndpoint, GroupMock2.self, options); const expected = new PostRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.subgroupsEndpoint, GroupMock2.self, options);
expect(requestService.send).toHaveBeenCalledWith(expected); expect(requestService.send).toHaveBeenCalledWith(expected);
}); });
it('should invalidate the previous requests of the parent group', () => {
expect(objectCache.getByHref).toHaveBeenCalledWith(GroupMock._links.self.href);
expect(requestService.setStaleByUUID).toHaveBeenCalledTimes(2);
expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request1');
expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request2');
});
}); });
describe('deleteSubGroupFromGroup', () => { describe('deleteSubGroupFromGroup', () => {
beforeEach(() => { beforeEach(() => {
spyOn(objectCache, 'getByHref').and.returnValue(observableOf({
requestUUIDs: ['request1', 'request2']
} as ObjectCacheEntry));
spyOn(dataService, 'invalidateByHref');
service.deleteSubGroupFromGroup(GroupMock, GroupMock2).subscribe(); service.deleteSubGroupFromGroup(GroupMock, GroupMock2).subscribe();
}); });
it('should send DeleteRequest to eperson/groups/group-id/subgroups/group-id endpoint', () => { it('should send DeleteRequest to eperson/groups/group-id/subgroups/group-id endpoint', () => {
const expected = new DeleteRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.subgroupsEndpoint + '/' + GroupMock2.id); const expected = new DeleteRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.subgroupsEndpoint + '/' + GroupMock2.id);
expect(requestService.send).toHaveBeenCalledWith(expected); expect(requestService.send).toHaveBeenCalledWith(expected);
}); });
it('should invalidate the previous requests of the parent group\'', () => {
expect(objectCache.getByHref).toHaveBeenCalledWith(GroupMock._links.self.href);
expect(requestService.setStaleByUUID).toHaveBeenCalledTimes(2);
expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request1');
expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request2');
});
}); });
describe('addMemberToGroup', () => { describe('addMemberToGroup', () => {
beforeEach(() => { beforeEach(() => {
spyOn(objectCache, 'getByHref').and.returnValue(observableOf({
requestUUIDs: ['request1', 'request2']
} as ObjectCacheEntry));
spyOn(dataService, 'invalidateByHref');
service.addMemberToGroup(GroupMock, EPersonMock2).subscribe(); service.addMemberToGroup(GroupMock, EPersonMock2).subscribe();
}); });
it('should send PostRequest to eperson/groups/group-id/epersons endpoint with new eperson member in body', () => { it('should send PostRequest to eperson/groups/group-id/epersons endpoint with new eperson member in body', () => {
@@ -143,20 +179,38 @@ describe('GroupDataService', () => {
const expected = new PostRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.ePersonsEndpoint, EPersonMock2.self, options); const expected = new PostRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.ePersonsEndpoint, EPersonMock2.self, options);
expect(requestService.send).toHaveBeenCalledWith(expected); expect(requestService.send).toHaveBeenCalledWith(expected);
}); });
it('should invalidate the previous requests of the EPerson and the group', () => {
expect(objectCache.getByHref).toHaveBeenCalledWith(EPersonMock2._links.self.href);
expect(objectCache.getByHref).toHaveBeenCalledWith(GroupMock._links.self.href);
expect(requestService.setStaleByUUID).toHaveBeenCalledTimes(4);
expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request1');
expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request2');
});
}); });
describe('deleteMemberFromGroup', () => { describe('deleteMemberFromGroup', () => {
beforeEach(() => { beforeEach(() => {
spyOn(objectCache, 'getByHref').and.returnValue(observableOf({
requestUUIDs: ['request1', 'request2']
} as ObjectCacheEntry));
spyOn(dataService, 'invalidateByHref');
service.deleteMemberFromGroup(GroupMock, EPersonMock).subscribe(); service.deleteMemberFromGroup(GroupMock, EPersonMock).subscribe();
}); });
it('should send DeleteRequest to eperson/groups/group-id/epersons/eperson-id endpoint', () => { it('should send DeleteRequest to eperson/groups/group-id/epersons/eperson-id endpoint', () => {
const expected = new DeleteRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.ePersonsEndpoint + '/' + EPersonMock.id); const expected = new DeleteRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.ePersonsEndpoint + '/' + EPersonMock.id);
expect(requestService.send).toHaveBeenCalledWith(expected); expect(requestService.send).toHaveBeenCalledWith(expected);
}); });
it('should invalidate the previous requests of the EPerson and the group', () => {
expect(objectCache.getByHref).toHaveBeenCalledWith(EPersonMock._links.self.href);
expect(objectCache.getByHref).toHaveBeenCalledWith(GroupMock._links.self.href);
expect(requestService.setStaleByUUID).toHaveBeenCalledTimes(4);
expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request1');
expect(requestService.setStaleByUUID).toHaveBeenCalledWith('request2');
});
}); });
describe('editGroup', () => { describe('editGroup', () => {
it('should dispatch a EDIT_GROUP action with the groupp to start editing', () => { it('should dispatch a EDIT_GROUP action with the group to start editing', () => {
service.editGroup(GroupMock); service.editGroup(GroupMock);
expect(store.dispatch).toHaveBeenCalledWith(new GroupRegistryEditGroupAction(GroupMock)); expect(store.dispatch).toHaveBeenCalledWith(new GroupRegistryEditGroupAction(GroupMock));
}); });

View File

@@ -2,8 +2,8 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { createSelector, select, Store } from '@ngrx/store'; import { createSelector, select, Store } from '@ngrx/store';
import { Observable } from 'rxjs'; import { Observable, AsyncSubject, zip as observableZip } from 'rxjs';
import { filter, map, take } from 'rxjs/operators'; import { filter, map, take, switchMap } from 'rxjs/operators';
import { import {
GroupRegistryCancelGroupAction, GroupRegistryCancelGroupAction,
GroupRegistryEditGroupAction GroupRegistryEditGroupAction
@@ -110,7 +110,8 @@ export class GroupDataService extends DataService<Group> {
} }
/** /**
* Adds given subgroup as a subgroup to the given active group * Adds given subgroup as a subgroup to the given active group and waits until the {@link activeGroup} and
* the {@link subgroup} are invalidated.
* @param activeGroup Group we want to add subgroup to * @param activeGroup Group we want to add subgroup to
* @param subgroup Group we want to add as subgroup to activeGroup * @param subgroup Group we want to add as subgroup to activeGroup
*/ */
@@ -123,11 +124,46 @@ export class GroupDataService extends DataService<Group> {
const postRequest = new PostRequest(requestId, activeGroup.self + '/' + this.subgroupsEndpoint, subgroup.self, options); const postRequest = new PostRequest(requestId, activeGroup.self + '/' + this.subgroupsEndpoint, subgroup.self, options);
this.requestService.send(postRequest); this.requestService.send(postRequest);
return this.rdbService.buildFromRequestUUID(requestId); const response$ = this.rdbService.buildFromRequestUUID(requestId);
const invalidated$ = new AsyncSubject<boolean>();
response$.pipe(
getFirstCompletedRemoteData(),
switchMap((rd: RemoteData<Group>) => {
if (rd.hasSucceeded) {
return observableZip(
this.invalidateByHref(activeGroup._links.self.href),
this.requestService.setStaleByHrefSubstring(activeGroup._links.subgroups.href).pipe(take(1)),
).pipe(
map((arr: boolean[]) => arr.every((b: boolean) => b === true))
);
} else {
return [true];
}
})
).subscribe(() => {
invalidated$.next(true);
invalidated$.complete();
});
return response$.pipe(
switchMap((rd: RemoteData<Group>) => {
if (rd.hasSucceeded) {
return invalidated$.pipe(
filter((invalidated: boolean) => invalidated),
map(() => rd)
);
} else {
return [rd];
}
})
);
} }
/** /**
* Deletes a given subgroup from the subgroups of the given active group * Deletes a given subgroup from the subgroups of the given active group and waits until the {@link activeGroup} and
* the {@link subgroup} are invalidated.
* are invalidated.
* @param activeGroup Group we want to delete subgroup from * @param activeGroup Group we want to delete subgroup from
* @param subgroup Subgroup we want to delete from activeGroup * @param subgroup Subgroup we want to delete from activeGroup
*/ */
@@ -136,11 +172,45 @@ export class GroupDataService extends DataService<Group> {
const deleteRequest = new DeleteRequest(requestId, activeGroup.self + '/' + this.subgroupsEndpoint + '/' + subgroup.id); const deleteRequest = new DeleteRequest(requestId, activeGroup.self + '/' + this.subgroupsEndpoint + '/' + subgroup.id);
this.requestService.send(deleteRequest); this.requestService.send(deleteRequest);
return this.rdbService.buildFromRequestUUID(requestId); const response$ = this.rdbService.buildFromRequestUUID(requestId);
const invalidated$ = new AsyncSubject<boolean>();
response$.pipe(
getFirstCompletedRemoteData(),
switchMap((rd: RemoteData<NoContent>) => {
if (rd.hasSucceeded) {
return observableZip(
this.invalidateByHref(activeGroup._links.self.href),
this.requestService.setStaleByHrefSubstring(activeGroup._links.subgroups.href).pipe(take(1)),
).pipe(
map((arr: boolean[]) => arr.every((b: boolean) => b === true))
);
} else {
return [true];
}
})
).subscribe(() => {
invalidated$.next(true);
invalidated$.complete();
});
return response$.pipe(
switchMap((rd: RemoteData<NoContent>) => {
if (rd.hasSucceeded) {
return invalidated$.pipe(
filter((invalidated: boolean) => invalidated),
map(() => rd)
);
} else {
return [rd];
}
})
);
} }
/** /**
* Adds given ePerson as member to given group * Adds given ePerson as member to a given group and invalidates the ePerson and waits until the {@link ePerson} and
* the {@link activeGroup} are invalidated.
* @param activeGroup Group we want to add member to * @param activeGroup Group we want to add member to
* @param ePerson EPerson we want to add as member to given activeGroup * @param ePerson EPerson we want to add as member to given activeGroup
*/ */
@@ -153,11 +223,47 @@ export class GroupDataService extends DataService<Group> {
const postRequest = new PostRequest(requestId, activeGroup.self + '/' + this.ePersonsEndpoint, ePerson.self, options); const postRequest = new PostRequest(requestId, activeGroup.self + '/' + this.ePersonsEndpoint, ePerson.self, options);
this.requestService.send(postRequest); this.requestService.send(postRequest);
return this.rdbService.buildFromRequestUUID(requestId); const response$ = this.rdbService.buildFromRequestUUID(requestId);
const invalidated$ = new AsyncSubject<boolean>();
response$.pipe(
getFirstCompletedRemoteData(),
switchMap((rd: RemoteData<NoContent>) => {
if (rd.hasSucceeded) {
return observableZip(
this.invalidateByHref(ePerson._links.self.href),
this.invalidateByHref(activeGroup._links.self.href),
this.requestService.setStaleByHrefSubstring(ePerson._links.groups.href).pipe(take(1)),
this.requestService.setStaleByHrefSubstring(activeGroup._links.epersons.href).pipe(take(1)),
).pipe(
map((arr: boolean[]) => arr.every((b: boolean) => b === true))
);
} else {
return [true];
}
})
).subscribe(() => {
invalidated$.next(true);
invalidated$.complete();
});
return response$.pipe(
switchMap((rd: RemoteData<Group>) => {
if (rd.hasSucceeded) {
return invalidated$.pipe(
filter((invalidated: boolean) => invalidated),
map(() => rd)
);
} else {
return [rd];
}
})
);
} }
/** /**
* Deletes a given ePerson from the members of the given active group * Deletes a given ePerson from the members of the given active group and waits until the {@link ePerson} and the
* {@link activeGroup} are invalidated.
* @param activeGroup Group we want to delete member from * @param activeGroup Group we want to delete member from
* @param ePerson EPerson we want to delete from members of given activeGroup * @param ePerson EPerson we want to delete from members of given activeGroup
*/ */
@@ -166,7 +272,42 @@ export class GroupDataService extends DataService<Group> {
const deleteRequest = new DeleteRequest(requestId, activeGroup.self + '/' + this.ePersonsEndpoint + '/' + ePerson.id); const deleteRequest = new DeleteRequest(requestId, activeGroup.self + '/' + this.ePersonsEndpoint + '/' + ePerson.id);
this.requestService.send(deleteRequest); this.requestService.send(deleteRequest);
return this.rdbService.buildFromRequestUUID(requestId); const response$ = this.rdbService.buildFromRequestUUID(requestId);
const invalidated$ = new AsyncSubject<boolean>();
response$.pipe(
getFirstCompletedRemoteData(),
switchMap((rd: RemoteData<NoContent>) => {
if (rd.hasSucceeded) {
return observableZip(
this.invalidateByHref(ePerson._links.self.href),
this.invalidateByHref(activeGroup._links.self.href),
this.requestService.setStaleByHrefSubstring(ePerson._links.groups.href).pipe(take(1)),
this.requestService.setStaleByHrefSubstring(activeGroup._links.epersons.href).pipe(take(1)),
).pipe(
map((arr: boolean[]) => arr.every((b: boolean) => b === true))
);
} else {
return [true];
}
})
).subscribe(() => {
invalidated$.next(true);
invalidated$.complete();
});
return response$.pipe(
switchMap((rd: RemoteData<NoContent>) => {
if (rd.hasSucceeded) {
return invalidated$.pipe(
filter((invalidated: boolean) => invalidated),
map(() => rd)
);
} else {
return [rd];
}
})
);
} }
/** /**
@@ -262,7 +403,7 @@ export class GroupDataService extends DataService<Group> {
* @param role The name of the role for which to create a group * @param role The name of the role for which to create a group
* @param link The REST endpoint to create the group * @param link The REST endpoint to create the group
*/ */
createComcolGroup(dso: Community|Collection, role: string, link: string): Observable<RemoteData<Group>> { createComcolGroup(dso: Community | Collection, role: string, link: string): Observable<RemoteData<Group>> {
const requestId = this.requestService.generateRequestId(); const requestId = this.requestService.generateRequestId();
const group = Object.assign(new Group(), { const group = Object.assign(new Group(), {

View File

@@ -0,0 +1,9 @@
import { Observable, of as observableOf } from 'rxjs';
export class DataServiceStub {
invalidateByHref(_href: string): Observable<boolean> {
return observableOf(true);
}
}