diff --git a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java index 3058782d67c4..ea8179e5baa8 100644 --- a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java +++ b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java @@ -1990,7 +1990,7 @@ private String getOrderQuery(EventQueryParams params) { } if (!orderFields.isEmpty()) { - return "order by " + StringUtils.join(orderFields, ',') + " "; + return "order by " + StringUtils.join(orderFields, ',') + ", psi_id desc "; } else { return "order by psi_lastupdated desc "; } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/OrderAndPaginationExporterTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/OrderAndPaginationExporterTest.java index 4fc53cc4d363..69874ecbd397 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/OrderAndPaginationExporterTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/OrderAndPaginationExporterTest.java @@ -133,6 +133,27 @@ void setUp() { injectAdminUser(); } + @Test + void shouldOrderEventsByStatusAndByDefaultOrder() { + List expected = + Stream.of( + get(ProgramStageInstance.class, "ck7DzdxqLqA"), + get(ProgramStageInstance.class, "kWjSezkXHVp"), + get(ProgramStageInstance.class, "OTmjvJDn0Fu")) + .sorted(Comparator.comparing(ProgramStageInstance::getId).reversed()) // reversed = desc + .map(ProgramStageInstance::getUid) + .collect(Collectors.toList()); + + EventQueryParams params = new EventQueryParams(); + params.setOrgUnit(get(OrganisationUnit.class, "DiszpKrYNg8")); + params.setEvents(Set.of("ck7DzdxqLqA", "kWjSezkXHVp", "OTmjvJDn0Fu")); + params.addOrders(List.of(new OrderParam("status", SortDirection.DESC))); + + List actual = getEvents(params); + + assertEquals(expected, actual); + } + @Test void shouldReturnPaginatedEventsWithNotesGivenNonDefaultPageSize() { EventQueryParams params = new EventQueryParams(); diff --git a/dhis-2/dhis-test-integration/src/test/resources/tracker/event_and_enrollment.json b/dhis-2/dhis-test-integration/src/test/resources/tracker/event_and_enrollment.json index 984f16da83ee..538b67f22a2d 100644 --- a/dhis-2/dhis-test-integration/src/test/resources/tracker/event_and_enrollment.json +++ b/dhis-2/dhis-test-integration/src/test/resources/tracker/event_and_enrollment.json @@ -1227,6 +1227,22 @@ "to": { "event": "pTzf9KYMk72" } + }, + { + "relationship": "fHn74P5T3r1", + "relationshipType": { + "idScheme": "UID", + "identifier": "TV9oB9LT3sh" + }, + "createdAtClient": "2018-11-01T13:24:37.118", + "bidirectional": false, + "deleted": false, + "from": { + "trackedEntity": "dUE514NMOlo" + }, + "to": { + "event": "D9PbzJY8bJM" + } } ], "username": "system-process"