Skip to content

Commit 19d4380

Browse files
authored
Merge pull request #4289 from 4Science/task/main/DURACOM-326
[DURACOM-326] fix possible issue on missing value for eperson patch
2 parents e7359a3 + 7b9cd73 commit 19d4380

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';
@@ -281,6 +282,37 @@ describe('EPersonDataService', () => {
281282
});
282283
});
283284

285+
describe('updateEPerson with non existing metadata', () => {
286+
beforeEach(() => {
287+
spyOn(service, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(EPersonMockWithNoName));
288+
});
289+
describe('add name that was not previously set', () => {
290+
beforeEach(() => {
291+
const changedEPerson = Object.assign(new EPerson(), {
292+
id: EPersonMock.id,
293+
metadata: Object.assign(EPersonMock.metadata, {
294+
'eperson.firstname': [
295+
{
296+
language: null,
297+
value: 'User',
298+
},
299+
],
300+
}),
301+
email: EPersonMock.email,
302+
canLogIn: EPersonMock.canLogIn,
303+
requireCertificate: EPersonMock.requireCertificate,
304+
_links: EPersonMock._links,
305+
});
306+
service.updateEPerson(changedEPerson).subscribe();
307+
});
308+
it('should send PatchRequest with add email operation', () => {
309+
const operations = [{ op: 'add', path: '/eperson.firstname', value: [{ language: null, value: 'User' }] }];
310+
const expected = new PatchRequest(requestService.generateRequestId(), epersonsEndpoint + '/' + EPersonMock.uuid, operations);
311+
expect(requestService.send).toHaveBeenCalledWith(expected);
312+
});
313+
});
314+
});
315+
284316
describe('clearEPersonRequests', () => {
285317
beforeEach(() => {
286318
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+
99+
lastActive: '2018-05-14T12:25:42.411+0000',
100+
canLogIn: true,
101+
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)