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

feat: Add atClient dates order capabilities for events [DHIS2-15900] #15733

Merged
merged 3 commits into from
Nov 27, 2023
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 @@ -169,7 +169,9 @@ class JdbcEventStore implements EventStore {
private static final String COLUMN_EVENT_LAST_UPDATED_BY = "ev_lastupdatedbyuserinfo";
private static final String COLUMN_EVENT_CREATED_BY = "ev_createdbyuserinfo";
private static final String COLUMN_EVENT_CREATED = "ev_created";
private static final String COLUMN_EVENT_CREATED_AT_CLIENT = "ev_createdatclient";
private static final String COLUMN_EVENT_LAST_UPDATED = "ev_lastupdated";
private static final String COLUMN_EVENT_LAST_UPDATED_AT_CLIENT = "ev_lastupdatedatclient";
private static final String COLUMN_EVENT_COMPLETED_BY = "ev_completedby";
private static final String COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_UID = "coc_uid";
private static final String COLUMN_EVENT_COMPLETED_DATE = "ev_completeddate";
Expand Down Expand Up @@ -206,7 +208,9 @@ class JdbcEventStore implements EventStore {
entry("lastUpdatedBy", COLUMN_EVENT_LAST_UPDATED_BY),
entry("createdBy", COLUMN_EVENT_CREATED_BY),
entry("created", COLUMN_EVENT_CREATED),
entry("createdAtClient", COLUMN_EVENT_CREATED_AT_CLIENT),
entry("lastUpdated", COLUMN_EVENT_LAST_UPDATED),
entry("lastUpdatedAtClient", COLUMN_EVENT_LAST_UPDATED_AT_CLIENT),
entry("completedBy", COLUMN_EVENT_COMPLETED_BY),
entry("attributeOptionCombo.uid", COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_UID),
entry("completedDate", COLUMN_EVENT_COMPLETED_DATE),
Expand Down Expand Up @@ -329,10 +333,13 @@ private List<Event> fetchEvents(EventQueryParams queryParams, PageParams pagePar
event.setScheduledDate(resultSet.getTimestamp(COLUMN_EVENT_SCHEDULED_DATE));
event.setOccurredDate(resultSet.getTimestamp(COLUMN_EVENT_OCCURRED_DATE));
event.setCreated(resultSet.getTimestamp(COLUMN_EVENT_CREATED));
event.setCreatedAtClient(resultSet.getTimestamp(COLUMN_EVENT_CREATED_AT_CLIENT));
event.setCreatedByUserInfo(
EventUtils.jsonToUserInfo(
resultSet.getString(COLUMN_EVENT_CREATED_BY), jsonMapper));
event.setLastUpdated(resultSet.getTimestamp(COLUMN_EVENT_LAST_UPDATED));
event.setLastUpdatedAtClient(
resultSet.getTimestamp(COLUMN_EVENT_LAST_UPDATED_AT_CLIENT));
event.setLastUpdatedByUserInfo(
EventUtils.jsonToUserInfo(
resultSet.getString(COLUMN_EVENT_LAST_UPDATED_BY), jsonMapper));
Expand Down Expand Up @@ -745,10 +752,15 @@ private String getEventSelectQuery(
.append(", ")
.append("ev.created as ")
.append(COLUMN_EVENT_CREATED)
.append(", ")
.append("ev.createdatclient as ")
.append(COLUMN_EVENT_CREATED_AT_CLIENT)
.append(", ev.createdbyuserinfo as ")
.append(COLUMN_EVENT_CREATED_BY)
.append(", ev.lastupdated as ")
.append(COLUMN_EVENT_LAST_UPDATED)
.append(", ev.lastupdatedatclient as ")
.append(COLUMN_EVENT_LAST_UPDATED_AT_CLIENT)
.append(", ev.lastupdatedbyuserinfo as ")
.append(COLUMN_EVENT_LAST_UPDATED_BY)
.append(", ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.hisp.dhis.tracker.imports.TrackerImportService;
import org.hisp.dhis.user.User;
import org.hisp.dhis.util.DateUtils;
import org.hisp.dhis.webapi.controller.event.mapper.SortDirection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand Down Expand Up @@ -179,6 +180,62 @@ void shouldReturnEventsWithNotes() throws ForbiddenException, BadRequestExceptio
() -> assertNote(importUser, "comment value", notes.get(1)));
}

@Test
void shouldOrderEventsByCreatedAtClientInAscOrder()
throws ForbiddenException, BadRequestException {
EventOperationParams params =
operationParamsBuilder
.programStageUid(programStage.getUid())
.orderBy("createdAtClient", SortDirection.ASC)
.build();

List<String> events = getEvents(params);

assertEquals(List.of("D9PbzJY8bJM", "pTzf9KYMk72"), events);
}

@Test
void shouldOrderEventsByCreatedAtClientInDescOrder()
throws ForbiddenException, BadRequestException {
EventOperationParams params =
operationParamsBuilder
.programStageUid(programStage.getUid())
.orderBy("createdAtClient", SortDirection.DESC)
.build();

List<String> events = getEvents(params);

assertEquals(List.of("pTzf9KYMk72", "D9PbzJY8bJM"), events);
}

@Test
void shouldOrderEventsByUpdatedAtClientInAscOrder()
throws ForbiddenException, BadRequestException {
EventOperationParams params =
operationParamsBuilder
.programStageUid(programStage.getUid())
.orderBy("lastUpdatedAtClient", SortDirection.ASC)
.build();

List<String> events = getEvents(params);

assertEquals(List.of("pTzf9KYMk72", "D9PbzJY8bJM"), events);
}

@Test
void shouldOrderEventsByUpdatedAtClientInDescOrder()
throws ForbiddenException, BadRequestException {
EventOperationParams params =
operationParamsBuilder
.programStageUid(programStage.getUid())
.orderBy("lastUpdatedAtClient", SortDirection.DESC)
.build();

List<String> events = getEvents(params);

assertEquals(List.of("D9PbzJY8bJM", "pTzf9KYMk72"), events);
}

@Test
void testExportEvents() throws ForbiddenException, BadRequestException {
EventOperationParams params =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@
"relationships": [],
"occurredAt": "2019-01-25T12:10:38.100",
"scheduledAt": "2019-01-28T12:32:38.100",
"createdAtClient": "2020-01-25T12:10:38.100",
"updatedAtClient": "2020-01-26T12:10:38.100",
"storedBy": "admin",
"followUp": true,
"deleted": false,
Expand Down Expand Up @@ -586,6 +588,8 @@
"relationships": [],
"occurredAt": "2020-01-28T00:00:00.000",
"scheduledAt": "2019-01-28T12:10:38.100",
"createdAtClient": "2019-01-25T12:10:38.100",
"updatedAtClient": "2021-01-26T12:10:38.100",
"storedBy": "admin",
"followUp": true,
"deleted": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,38 +292,37 @@ void getEventByIdContainsCreatedByAndUpdateByAndAssignedUserInDataValues() {

TrackedEntity te = trackedEntity();
Enrollment enrollment = enrollment(te);
Event programStageInstance = event(enrollment);
programStageInstance.setCreatedByUserInfo(UserInfoSnapshot.from(user));
programStageInstance.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user));
programStageInstance.setAssignedUser(user);
Event event = event(enrollment);
event.setCreatedByUserInfo(UserInfoSnapshot.from(user));
event.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user));
event.setAssignedUser(user);
EventDataValue eventDataValue = new EventDataValue();
eventDataValue.setValue("6");

eventDataValue.setDataElement(de.getUid());
eventDataValue.setCreatedByUserInfo(UserInfoSnapshot.from(user));
eventDataValue.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user));
Set<EventDataValue> eventDataValues = Set.of(eventDataValue);
programStageInstance.setEventDataValues(eventDataValues);
manager.save(programStageInstance);

JsonObject event =
GET("/tracker/events/{id}", programStageInstance.getUid()).content(HttpStatus.OK);

assertTrue(event.isObject());
assertFalse(event.isEmpty());
assertEquals(programStageInstance.getUid(), event.getString("event").string());
assertEquals(enrollment.getUid(), event.getString("enrollment").string());
assertEquals(orgUnit.getUid(), event.getString("orgUnit").string());
assertEquals(user.getUsername(), event.getString("createdBy.username").string());
assertEquals(user.getUsername(), event.getString("updatedBy.username").string());
assertEquals(user.getDisplayName(), event.getString("assignedUser.displayName").string());
assertFalse(event.getArray("dataValues").isEmpty());
event.setEventDataValues(eventDataValues);
manager.save(event);

JsonObject jsonEvent = GET("/tracker/events/{id}", event.getUid()).content(HttpStatus.OK);

assertTrue(jsonEvent.isObject());
assertFalse(jsonEvent.isEmpty());
assertEquals(event.getUid(), jsonEvent.getString("event").string());
assertEquals(enrollment.getUid(), jsonEvent.getString("enrollment").string());
assertEquals(orgUnit.getUid(), jsonEvent.getString("orgUnit").string());
assertEquals(user.getUsername(), jsonEvent.getString("createdBy.username").string());
assertEquals(user.getUsername(), jsonEvent.getString("updatedBy.username").string());
assertEquals(user.getDisplayName(), jsonEvent.getString("assignedUser.displayName").string());
assertFalse(jsonEvent.getArray("dataValues").isEmpty());
assertEquals(
user.getUsername(),
event.getArray("dataValues").getObject(0).getString("createdBy.username").string());
jsonEvent.getArray("dataValues").getObject(0).getString("createdBy.username").string());
assertEquals(
user.getUsername(),
event.getArray("dataValues").getObject(0).getString("updatedBy.username").string());
jsonEvent.getArray("dataValues").getObject(0).getString("updatedBy.username").string());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public interface EventMapper
entry("completedAt", "completedDate"),
entry("completedBy", "completedBy"),
entry("createdAt", "created"),
entry("createdAtClient", "createdAtClient"),
entry("createdBy", "createdBy"),
entry("deleted", "deleted"),
entry("enrolledAt", "enrollment.enrollmentDate"),
Expand All @@ -81,6 +82,7 @@ public interface EventMapper
entry("storedBy", "storedBy"),
entry("trackedEntity", "enrollment.trackedEntity.uid"),
entry("updatedAt", "lastUpdated"),
entry("updatedAtClient", "lastUpdatedAtClient"),
entry("updatedBy", "lastUpdatedBy"));

@Mapping(target = "event", source = "uid")
Expand Down
Loading