Skip to content

Commit 25004d3

Browse files
authored
Merge pull request #4301 from DSpace/backport-4289-to-dspace-8_x
[Port dspace-8_x] [DURACOM-326] fix possible issue on missing value for eperson patch
2 parents d95974a + d02c06d commit 25004d3

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import {
3232
EPersonMock,
3333
EPersonMock2,
34+
EPersonMockWithNoName,
3435
} from '../../shared/testing/eperson.mock';
3536
import { GroupMock } from '../../shared/testing/group-mock';
3637
import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub';
@@ -280,6 +281,37 @@ describe('EPersonDataService', () => {
280281
});
281282
});
282283

284+
describe('updateEPerson with non existing metadata', () => {
285+
beforeEach(() => {
286+
spyOn(service, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(EPersonMockWithNoName));
287+
});
288+
describe('add name that was not previously set', () => {
289+
beforeEach(() => {
290+
const changedEPerson = Object.assign(new EPerson(), {
291+
id: EPersonMock.id,
292+
metadata: Object.assign(EPersonMock.metadata, {
293+
'eperson.firstname': [
294+
{
295+
language: null,
296+
value: 'User',
297+
},
298+
],
299+
}),
300+
email: EPersonMock.email,
301+
canLogIn: EPersonMock.canLogIn,
302+
requireCertificate: EPersonMock.requireCertificate,
303+
_links: EPersonMock._links,
304+
});
305+
service.updateEPerson(changedEPerson).subscribe();
306+
});
307+
it('should send PatchRequest with add email operation', () => {
308+
const operations = [{ op: 'add', path: '/eperson.firstname', value: [{ language: null, value: 'User' }] }];
309+
const expected = new PatchRequest(requestService.generateRequestId(), epersonsEndpoint + '/' + EPersonMock.uuid, operations);
310+
expect(requestService.send).toHaveBeenCalledWith(expected);
311+
});
312+
});
313+
});
314+
283315
describe('clearEPersonRequests', () => {
284316
beforeEach(() => {
285317
spyOn(halService, 'getEndpoint').and.callFake((linkPath: string) => {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ export class EPersonDataService extends IdentifiableDataService<EPerson> impleme
269269
* @param newEPerson
270270
*/
271271
private generateOperations(oldEPerson: EPerson, newEPerson: EPerson): Operation[] {
272-
let operations = this.comparator.diff(oldEPerson, newEPerson).filter((operation: Operation) => operation.op === 'replace');
272+
let operations = this.comparator.diff(oldEPerson, newEPerson)
273+
.filter((operation: Operation) => ['replace', 'add'].includes(operation.op));
273274
if (hasValue(oldEPerson.email) && oldEPerson.email !== newEPerson.email) {
274275
operations = [...operations, {
275276
op: 'replace', path: '/email', value: newEPerson.email,

src/app/shared/testing/eperson.mock.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,43 @@ export const EPersonMock2: EPerson = Object.assign(new EPerson(), {
9191
],
9292
},
9393
});
94+
95+
export const EPersonMockWithNoName: EPerson = Object.assign(new EPerson(), {
96+
handle: null,
97+
groups: [],
98+
netid: 'test@test.com',
99+
lastActive: '2018-05-14T12:25:42.411+0000',
100+
canLogIn: true,
101+
email: 'test@test.com',
102+
requireCertificate: false,
103+
selfRegistered: false,
104+
_links: {
105+
self: {
106+
href: 'https://rest.api/dspace-spring-rest/api/eperson/epersons/testid',
107+
},
108+
groups: { href: 'https://rest.api/dspace-spring-rest/api/eperson/epersons/testid/groups' },
109+
},
110+
id: 'testid',
111+
uuid: 'testid',
112+
type: 'eperson',
113+
metadata: {
114+
'dc.title': [
115+
{
116+
language: null,
117+
value: 'User Test',
118+
},
119+
],
120+
'eperson.lastname': [
121+
{
122+
language: null,
123+
value: 'Test',
124+
},
125+
],
126+
'eperson.language': [
127+
{
128+
language: null,
129+
value: 'en',
130+
},
131+
],
132+
},
133+
});

0 commit comments

Comments
 (0)