From 7b9cd73ee0811cd3188f21b4bbf5e1e64280849e Mon Sep 17 00:00:00 2001 From: FrancescoMolinaro Date: Wed, 30 Apr 2025 14:51:42 +0200 Subject: [PATCH] [DURACOM-326] fix possible issue on missing value for eperson patch --- .../core/eperson/eperson-data.service.spec.ts | 32 +++++++++++++++ src/app/core/eperson/eperson-data.service.ts | 3 +- src/app/shared/testing/eperson.mock.ts | 40 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/app/core/eperson/eperson-data.service.spec.ts b/src/app/core/eperson/eperson-data.service.spec.ts index ac735d8e92..f3209cd34c 100644 --- a/src/app/core/eperson/eperson-data.service.spec.ts +++ b/src/app/core/eperson/eperson-data.service.spec.ts @@ -31,6 +31,7 @@ import { import { EPersonMock, EPersonMock2, + EPersonMockWithNoName, } from '../../shared/testing/eperson.mock'; import { GroupMock } from '../../shared/testing/group-mock'; import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub'; @@ -281,6 +282,37 @@ describe('EPersonDataService', () => { }); }); + describe('updateEPerson with non existing metadata', () => { + beforeEach(() => { + spyOn(service, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(EPersonMockWithNoName)); + }); + describe('add name that was not previously set', () => { + beforeEach(() => { + const changedEPerson = Object.assign(new EPerson(), { + id: EPersonMock.id, + metadata: Object.assign(EPersonMock.metadata, { + 'eperson.firstname': [ + { + language: null, + value: 'User', + }, + ], + }), + email: EPersonMock.email, + canLogIn: EPersonMock.canLogIn, + requireCertificate: EPersonMock.requireCertificate, + _links: EPersonMock._links, + }); + service.updateEPerson(changedEPerson).subscribe(); + }); + it('should send PatchRequest with add email operation', () => { + const operations = [{ op: 'add', path: '/eperson.firstname', value: [{ language: null, value: 'User' }] }]; + const expected = new PatchRequest(requestService.generateRequestId(), epersonsEndpoint + '/' + EPersonMock.uuid, operations); + expect(requestService.send).toHaveBeenCalledWith(expected); + }); + }); + }); + describe('clearEPersonRequests', () => { beforeEach(() => { spyOn(halService, 'getEndpoint').and.callFake((linkPath: string) => { diff --git a/src/app/core/eperson/eperson-data.service.ts b/src/app/core/eperson/eperson-data.service.ts index 33a02de4d7..a493f55405 100644 --- a/src/app/core/eperson/eperson-data.service.ts +++ b/src/app/core/eperson/eperson-data.service.ts @@ -269,7 +269,8 @@ export class EPersonDataService extends IdentifiableDataService impleme * @param newEPerson */ private generateOperations(oldEPerson: EPerson, newEPerson: EPerson): Operation[] { - let operations = this.comparator.diff(oldEPerson, newEPerson).filter((operation: Operation) => operation.op === 'replace'); + let operations = this.comparator.diff(oldEPerson, newEPerson) + .filter((operation: Operation) => ['replace', 'add'].includes(operation.op)); if (hasValue(oldEPerson.email) && oldEPerson.email !== newEPerson.email) { operations = [...operations, { op: 'replace', path: '/email', value: newEPerson.email, diff --git a/src/app/shared/testing/eperson.mock.ts b/src/app/shared/testing/eperson.mock.ts index 002ee9326f..36daad1e57 100644 --- a/src/app/shared/testing/eperson.mock.ts +++ b/src/app/shared/testing/eperson.mock.ts @@ -91,3 +91,43 @@ export const EPersonMock2: EPerson = Object.assign(new EPerson(), { ], }, }); + +export const EPersonMockWithNoName: EPerson = Object.assign(new EPerson(), { + handle: null, + groups: [], + netid: 'test@test.com', + lastActive: '2018-05-14T12:25:42.411+0000', + canLogIn: true, + email: 'test@test.com', + requireCertificate: false, + selfRegistered: false, + _links: { + self: { + href: 'https://rest.api/dspace-spring-rest/api/eperson/epersons/testid', + }, + groups: { href: 'https://rest.api/dspace-spring-rest/api/eperson/epersons/testid/groups' }, + }, + id: 'testid', + uuid: 'testid', + type: 'eperson', + metadata: { + 'dc.title': [ + { + language: null, + value: 'User Test', + }, + ], + 'eperson.lastname': [ + { + language: null, + value: 'Test', + }, + ], + 'eperson.language': [ + { + language: null, + value: 'en', + }, + ], + }, +});