From 816d28701c8b5a65203fb41ac352c217ca56bf1b Mon Sep 17 00:00:00 2001 From: Enrico Date: Tue, 15 Oct 2024 09:07:25 +0200 Subject: [PATCH] fix: Set lastUpdatedBy when creating relationships [DHIS2-18226] --- .../converter/RelationshipTrackerConverterService.java | 5 +++-- .../converter/RelationshipTrackerConverterServiceTest.java | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/converter/RelationshipTrackerConverterService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/converter/RelationshipTrackerConverterService.java index 968d2983e1b4..28bf0dc60f16 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/converter/RelationshipTrackerConverterService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/converter/RelationshipTrackerConverterService.java @@ -121,14 +121,15 @@ private org.hisp.dhis.relationship.Relationship from( org.hisp.dhis.relationship.RelationshipItem toItem = new org.hisp.dhis.relationship.RelationshipItem(); + Date now = new Date(); if (toRelationship == null) { - Date now = new Date(); toRelationship = new org.hisp.dhis.relationship.Relationship(); toRelationship.setUid(fromRelationship.getRelationship()); toRelationship.setCreated(now); - toRelationship.setLastUpdated(now); } + toRelationship.setLastUpdated(now); + toRelationship.setLastUpdatedBy(preheat.getUser()); toRelationship.setRelationshipType(relationshipType); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/converter/RelationshipTrackerConverterServiceTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/converter/RelationshipTrackerConverterServiceTest.java index d7e3c45bc637..e8d909af2275 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/converter/RelationshipTrackerConverterServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/converter/RelationshipTrackerConverterServiceTest.java @@ -45,6 +45,7 @@ import org.hisp.dhis.tracker.domain.Relationship; import org.hisp.dhis.tracker.domain.RelationshipItem; import org.hisp.dhis.tracker.preheat.TrackerPreheat; +import org.hisp.dhis.user.User; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -84,10 +85,13 @@ class RelationshipTrackerConverterServiceTest extends DhisConvenienceTest { private TrackerConverterService relationshipConverterService; + private User user; + @Mock public TrackerPreheat preheat; @BeforeEach protected void setupTest() { + user = makeUser("A"); OrganisationUnit organisationUnit = createOrganisationUnit('A'); Program program = createProgram('A'); TrackedEntityType teiType = createTrackedEntityType('A'); @@ -120,6 +124,7 @@ void testConverterFromRelationships() { when(preheat.getTrackedEntity(TEI)).thenReturn(tei); when(preheat.getEnrollment(ENROLLMENT)).thenReturn(pi); when(preheat.getEvent(EVENT)).thenReturn(psi); + when(preheat.getUser()).thenReturn(user); List from = relationshipConverterService.from(preheat, List.of(relationshipA(), relationshipB())); @@ -140,6 +145,7 @@ void testConverterFromRelationships() { } assertNotNull(relationship.getFrom()); assertNotNull(relationship.getTo()); + assertEquals(user.getUid(), relationship.getLastUpdatedBy().getUid()); }); }