Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Clean up completed data when status is not completed [DHIS2-16122] #18539

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,21 @@ private Event from(

result.setGeometry(event.getGeometry());

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

result.setStatus(event.getStatus());

if (previousStatus != result.getStatus() && result.isCompleted()) {
if (currentStatus != previousStatus && currentStatus == EventStatus.COMPLETED) {
result.setCompletedDate(now);
result.setCompletedBy(preheat.getUsername());
}

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

result.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.test.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 @@ -39,6 +40,7 @@
import java.util.Set;
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.Enrollment;
Expand Down Expand Up @@ -90,16 +92,13 @@ class EventTrackerConverterServiceTest extends TestBase {

private OrganisationUnit organisationUnit;

private Event event;

private DataElement dataElement;
private Event dbEvent;

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 All @@ -111,42 +110,66 @@ void setUpTest() {
TrackedEntity te = createTrackedEntity(organisationUnit);
Enrollment enrollment = createEnrollment(program, te, organisationUnit);
enrollment.setUid(ENROLLMENT_UID);
event = new Event();
event.setAutoFields();
event.setAttributeOptionCombo(createCategoryOptionCombo('C'));
event.setCreated(today);
event.setOccurredDate(today);
event.setEnrollment(enrollment);
event.setOrganisationUnit(organisationUnit);
event.setProgramStage(programStage);
event.setEventDataValues(Sets.newHashSet());
event.setScheduledDate(null);
event.setCompletedDate(null);
event.setStoredBy(user.getUsername());
event.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user));
event.setCreatedByUserInfo(UserInfoSnapshot.from(user));
dbEvent = new Event();
dbEvent.setAutoFields();
dbEvent.setAttributeOptionCombo(createCategoryOptionCombo('C'));
dbEvent.setCreated(today);
dbEvent.setOccurredDate(today);
dbEvent.setEnrollment(enrollment);
dbEvent.setOrganisationUnit(organisationUnit);
dbEvent.setProgramStage(programStage);
dbEvent.setEventDataValues(Sets.newHashSet());
dbEvent.setScheduledDate(null);
dbEvent.setCompletedDate(null);
dbEvent.setStoredBy(user.getUsername());
dbEvent.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user));
dbEvent.setCreatedByUserInfo(UserInfoSnapshot.from(user));
}

@Test
void testFromEvent() {
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.imports.domain.Event event =
event(dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value"));

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()));
org.hisp.dhis.tracker.imports.domain.Event event = event(dataValue);
Event 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 shouldConvertFromEventWithCompletedDataWhenStatusIsCompleted() {
setUpMocks();

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.imports.domain.Event event =
event(dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value"));
event.setStatus(EventStatus.COMPLETED);

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

Expand All @@ -157,7 +180,78 @@ void testFromEvent() {
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());
});
}

@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);
dbEvent.setStatus(EventStatus.COMPLETED);
when(preheat.getEvent(eventUid)).thenReturn(dbEvent);

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

Event 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);
dbEvent.setStatus(EventStatus.ACTIVE);
when(preheat.getEvent(eventUid)).thenReturn(dbEvent);

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

Event 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 -> {
Expand Down Expand Up @@ -295,26 +389,26 @@ 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));
event.getEventDataValues().add(eventDataValue);
dbEvent.getEventDataValues().add(eventDataValue);

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

assertEquals(ENROLLMENT_UID, event.getEnrollment());
assertEquals(event.getStoredBy(), user.getUsername());
event
.getDataValues()
.forEach(
e -> {
assertEquals(DateUtils.fromInstant(e.getCreatedAt()), this.event.getCreated());
assertEquals(DateUtils.fromInstant(e.getCreatedAt()), this.dbEvent.getCreated());
assertEquals(
e.getUpdatedBy().getUsername(),
this.event.getLastUpdatedByUserInfo().getUsername());
this.dbEvent.getLastUpdatedByUserInfo().getUsername());
assertEquals(
e.getUpdatedBy().getUsername(), this.event.getCreatedByUserInfo().getUsername());
e.getUpdatedBy().getUsername(),
this.dbEvent.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
Loading
Loading