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: Prioritise payload/bundled events over database events for enrolment[DHIS2-17806] #19330

Draft
wants to merge 1 commit into
base: 2.40
Choose a base branch
from
Draft
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 @@ -34,7 +34,6 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.hisp.dhis.dxf2.events.event.EventQueryParams;
Expand Down Expand Up @@ -207,19 +206,34 @@ private List<TrackedEntityAttributeValue> getAttributes(
// if they are present in both places
private Set<ProgramStageInstance> getEventsFromEnrollment(
String enrollmentUid, TrackerBundle bundle, TrackerPreheat preheat) {

// Fetch events linked to the enrollment from the database
EventQueryParams eventQueryParams = new EventQueryParams();
eventQueryParams.setProgramInstances(Set.of(enrollmentUid));
List<org.hisp.dhis.dxf2.events.event.Event> events =
List<org.hisp.dhis.dxf2.events.event.Event> dbEvents =
eventService.getEvents(eventQueryParams).getEvents();

Stream<ProgramStageInstance> programStageInstances =
events.stream().map(e -> programStageInstanceService.getProgramStageInstance(e.getUid()));
// Convert DB events to ProgramStageInstances
Map<String, ProgramStageInstance> dbProgramStageInstances =
dbEvents.stream()
.collect(
Collectors.toMap(
org.hisp.dhis.dxf2.events.event.Event::getUid,
e -> programStageInstanceService.getProgramStageInstance(e.getUid())));

Stream<ProgramStageInstance> bundleEvents =
// Fetch events from the payload for the given enrollment
Map<String, ProgramStageInstance> payloadProgramStageInstances =
bundle.getEvents().stream()
.filter(e -> e.getEnrollment().equals(enrollmentUid))
.map(event -> eventTrackerConverterService.fromForRuleEngine(preheat, event));
.filter(event -> event.getEnrollment().equals(enrollmentUid))
.collect(
Collectors.toMap(
Event::getUid,
event -> eventTrackerConverterService.fromForRuleEngine(preheat, event)));

// Merge payload events (prioritized) with DB events
dbProgramStageInstances.putAll(payloadProgramStageInstances);

return Stream.concat(programStageInstances, bundleEvents).collect(Collectors.toSet());
// Return a Set of unique ProgramStageInstances
return new HashSet<>(dbProgramStageInstances.values());
}
}