Skip to content

Commit

Permalink
fix: Clean up completed data when status is not completed [DHIS2-1612…
Browse files Browse the repository at this point in the history
…2] (#18539) (#18645)
  • Loading branch information
enricocolasante authored Sep 23, 2024
1 parent 95a3c86 commit 9fd3b14
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;

import com.google.common.base.Objects;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -235,16 +234,21 @@ private ProgramStageInstance from(

programStageInstance.setGeometry(event.getGeometry());

EventStatus currentStatus = event.getStatus();
EventStatus previousStatus = programStageInstance.getStatus();

programStageInstance.setStatus(event.getStatus());

if (!Objects.equal(previousStatus, programStageInstance.getStatus())
&& programStageInstance.isCompleted()) {
programStageInstance.setCompletedDate(new Date());
if (currentStatus != previousStatus && currentStatus == EventStatus.COMPLETED) {
programStageInstance.setCompletedDate(now);
programStageInstance.setCompletedBy(preheat.getUsername());
}

if (currentStatus != EventStatus.COMPLETED) {
programStageInstance.setCompletedDate(null);
programStageInstance.setCompletedBy(null);
}

programStageInstance.setStatus(currentStatus);

if (Boolean.TRUE.equals(programStage.isEnableUserAssignment())
&& event.getAssignedUser() != null
&& !event.getAssignedUser().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.hisp.dhis.utils.Assertions.assertContainsOnly;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

Expand All @@ -40,6 +41,7 @@
import org.hisp.dhis.DhisConvenienceTest;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.event.EventStatus;
import org.hisp.dhis.eventdatavalue.EventDataValue;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
Expand Down Expand Up @@ -93,14 +95,11 @@ class EventTrackerConverterServiceTest extends DhisConvenienceTest {

private ProgramStageInstance psi;

private DataElement dataElement;

private org.hisp.dhis.user.User user;

@BeforeEach
void setUpTest() {
converter = new EventTrackerConverterService(notesConverterService);
dataElement = createDataElement('D');
user = makeUser("U");
programStage = createProgramStage('A', 1);
programStage.setUid(PROGRAM_STAGE_UID);
Expand Down Expand Up @@ -129,37 +128,132 @@ void setUpTest() {
}

@Test
void testToProgramStageInstance() {
void shouldConvertFromEventWithNullCompletedDataWhenStatusIsActive() {
setUpMocks();

DataElement dataElement = new DataElement();
dataElement.setUid(CodeGenerator.generateUid());
when(preheat.getDataElement(MetadataIdentifier.ofUid(dataElement.getUid())))
.thenReturn(dataElement);

User user = User.builder().username(USERNAME).build();
org.hisp.dhis.tracker.domain.Event event =
event(dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value"));

ProgramStageInstance result = converter.from(preheat, event);

assertNotNull(result);
assertNotNull(result.getProgramStage());
assertNotNull(result.getProgramStage().getProgram());
assertNotNull(result.getOrganisationUnit());
assertEquals(PROGRAM_UID, result.getProgramStage().getProgram().getUid());
assertEquals(PROGRAM_STAGE_UID, result.getProgramStage().getUid());
assertEquals(ORGANISATION_UNIT_UID, result.getOrganisationUnit().getUid());
assertNull(result.getCompletedBy());
assertNull(result.getCompletedDate());
Set<EventDataValue> eventDataValues = result.getEventDataValues();
eventDataValues.forEach(
e -> {
assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername());
assertEquals(USERNAME, e.getLastUpdatedByUserInfo().getUsername());
});
}

DataValue dataValue = new DataValue();
dataValue.setValue("value");
dataValue.setCreatedBy(user);
dataValue.setUpdatedBy(user);
dataValue.setCreatedAt(Instant.now());
dataValue.setStoredBy(USERNAME);
dataValue.setUpdatedAt(Instant.now());
dataValue.setDataElement(MetadataIdentifier.ofUid(dataElement.getUid()));
Event event = event(dataValue);
@Test
void shouldConvertFromEventWithCompletedDataWhenStatusIsCompleted() {
setUpMocks();

ProgramStageInstance programStageInstance = converter.from(preheat, event);
DataElement dataElement = new DataElement();
dataElement.setUid(CodeGenerator.generateUid());
when(preheat.getDataElement(MetadataIdentifier.ofUid(dataElement.getUid())))
.thenReturn(dataElement);
when(preheat.getUsername()).thenReturn(USERNAME);

org.hisp.dhis.tracker.domain.Event event =
event(dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value"));
event.setStatus(EventStatus.COMPLETED);

ProgramStageInstance result = converter.from(preheat, event);

assertNotNull(result);
assertNotNull(result.getProgramStage());
assertNotNull(result.getProgramStage().getProgram());
assertNotNull(result.getOrganisationUnit());
assertEquals(PROGRAM_UID, result.getProgramStage().getProgram().getUid());
assertEquals(PROGRAM_STAGE_UID, result.getProgramStage().getUid());
assertEquals(ORGANISATION_UNIT_UID, result.getOrganisationUnit().getUid());
assertNotNull(result.getCompletedDate());
assertEquals(USERNAME, result.getCompletedBy());
Set<EventDataValue> eventDataValues = result.getEventDataValues();
eventDataValues.forEach(
e -> {
assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername());
assertEquals(USERNAME, e.getLastUpdatedByUserInfo().getUsername());
});
}

assertNotNull(programStageInstance);
assertNotNull(programStageInstance.getProgramStage());
assertNotNull(programStageInstance.getProgramStage().getProgram());
assertNotNull(programStageInstance.getOrganisationUnit());
assertEquals(PROGRAM_UID, programStageInstance.getProgramStage().getProgram().getUid());
assertEquals(PROGRAM_STAGE_UID, programStageInstance.getProgramStage().getUid());
assertEquals(ORGANISATION_UNIT_UID, programStageInstance.getOrganisationUnit().getUid());
assertEquals(ORGANISATION_UNIT_UID, programStageInstance.getOrganisationUnit().getUid());
Set<EventDataValue> eventDataValues = programStageInstance.getEventDataValues();
@Test
void shouldConvertFromExistingEventWithNullCompletedDataWhenStatusIsActive() {
setUpMocks();

DataElement dataElement = new DataElement();
dataElement.setUid(CodeGenerator.generateUid());
String eventUid = CodeGenerator.generateUid();
when(preheat.getDataElement(MetadataIdentifier.ofUid(dataElement.getUid())))
.thenReturn(dataElement);
psi.setStatus(EventStatus.COMPLETED);
when(preheat.getEvent(eventUid)).thenReturn(psi);

org.hisp.dhis.tracker.domain.Event event =
event(eventUid, dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value"));

ProgramStageInstance result = converter.from(preheat, event);

assertNotNull(result);
assertNotNull(result.getProgramStage());
assertNotNull(result.getProgramStage().getProgram());
assertNotNull(result.getOrganisationUnit());
assertEquals(PROGRAM_UID, result.getProgramStage().getProgram().getUid());
assertEquals(PROGRAM_STAGE_UID, result.getProgramStage().getUid());
assertEquals(ORGANISATION_UNIT_UID, result.getOrganisationUnit().getUid());
assertNull(result.getCompletedBy());
assertNull(result.getCompletedDate());
Set<EventDataValue> eventDataValues = result.getEventDataValues();
eventDataValues.forEach(
e -> {
assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername());
assertEquals(USERNAME, e.getLastUpdatedByUserInfo().getUsername());
});
}

@Test
void shouldConvertFromExistingEventWithCompletedDataWhenStatusIsCompleted() {
setUpMocks();

DataElement dataElement = new DataElement();
dataElement.setUid(CodeGenerator.generateUid());
String eventUid = CodeGenerator.generateUid();
when(preheat.getDataElement(MetadataIdentifier.ofUid(dataElement.getUid())))
.thenReturn(dataElement);
when(preheat.getUsername()).thenReturn(USERNAME);
psi.setStatus(EventStatus.ACTIVE);
when(preheat.getEvent(eventUid)).thenReturn(psi);

org.hisp.dhis.tracker.domain.Event event =
event(eventUid, dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value"));
event.setStatus(EventStatus.COMPLETED);

ProgramStageInstance result = converter.from(preheat, event);

assertNotNull(result);
assertNotNull(result.getProgramStage());
assertNotNull(result.getProgramStage().getProgram());
assertNotNull(result.getOrganisationUnit());
assertEquals(PROGRAM_UID, result.getProgramStage().getProgram().getUid());
assertEquals(PROGRAM_STAGE_UID, result.getProgramStage().getUid());
assertEquals(ORGANISATION_UNIT_UID, result.getOrganisationUnit().getUid());
assertNotNull(result.getCompletedDate());
assertEquals(USERNAME, result.getCompletedBy());
Set<EventDataValue> eventDataValues = result.getEventDataValues();
eventDataValues.forEach(
e -> {
assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername());
Expand Down Expand Up @@ -296,25 +390,25 @@ void testToEvent() {
eventDataValue.setAutoFields();
eventDataValue.setCreated(today);
eventDataValue.setValue("sample-value");
eventDataValue.setDataElement(dataElement.getUid());
eventDataValue.setStoredBy(user.getUsername());
eventDataValue.setCreatedByUserInfo(UserInfoSnapshot.from(user));
eventDataValue.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user));
psi.getEventDataValues().add(eventDataValue);

Event event = converter.to(psi);
org.hisp.dhis.tracker.domain.Event event = converter.to(this.psi);

assertEquals(PROGRAM_INSTANCE_UID, event.getEnrollment());
assertEquals(event.getStoredBy(), user.getUsername());
event
.getDataValues()
.forEach(
e -> {
assertEquals(DateUtils.fromInstant(e.getCreatedAt()), psi.getCreated());
assertEquals(DateUtils.fromInstant(e.getCreatedAt()), this.psi.getCreated());
assertEquals(
e.getUpdatedBy().getUsername(), psi.getLastUpdatedByUserInfo().getUsername());
e.getUpdatedBy().getUsername(),
this.psi.getLastUpdatedByUserInfo().getUsername());
assertEquals(
e.getUpdatedBy().getUsername(), psi.getCreatedByUserInfo().getUsername());
e.getUpdatedBy().getUsername(), this.psi.getCreatedByUserInfo().getUsername());
});
}

Expand Down Expand Up @@ -355,14 +449,14 @@ private EventDataValue eventDataValue(String dataElement, String value) {
}

private DataValue dataValue(MetadataIdentifier dataElement, String value) {
User user = User.builder().username(USERNAME).build();
User trackerUser = User.builder().username(USERNAME).build();

return DataValue.builder()
.dataElement(dataElement)
.value(value)
.providedElsewhere(true)
.createdBy(user)
.updatedBy(user)
.createdBy(trackerUser)
.updatedBy(trackerUser)
.createdAt(Instant.now())
.storedBy(USERNAME)
.updatedAt(Instant.now())
Expand Down

0 comments on commit 9fd3b14

Please sign in to comment.