diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/AppointmentService.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/AppointmentService.java index b34a6811..50ee11c0 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/AppointmentService.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/AppointmentService.java @@ -785,6 +785,7 @@ Set getAllLocationDescendants(Location location, * @param type - The appointment type * @param status - The appointment status * @return a list of appointments that satisfy the given constraints + * @should sort by associated time slot */ @Authorized(AppointmentUtils.PRIV_VIEW_APPOINTMENTS) List getAppointmentsByConstraints(Date fromDate, Date toDate, @@ -802,6 +803,7 @@ List getAppointmentsByConstraints(Date fromDate, Date toDate, * @param status - The appointment status * @param patient - The patient * @return a list of appointments that satisfy the given constraints + * @should sort by associated time slot */ @Authorized(AppointmentUtils.PRIV_VIEW_APPOINTMENTS) List getAppointmentsByConstraints(Date fromDate, Date toDate, @@ -819,6 +821,7 @@ List getAppointmentsByConstraints(Date fromDate, Date toDate, * @param patient - The patient * @param appointmentStatuses - The appointment status list * @return a list of appointments that satisfy the given constraints + * @should sort by associated time slot */ @Authorized(AppointmentUtils.PRIV_VIEW_APPOINTMENTS) List getAppointmentsByConstraints(Date fromDate, Date toDate, diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentDAO.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentDAO.java index 8b905243..d297d694 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentDAO.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentDAO.java @@ -111,6 +111,8 @@ public List getAppointmentsByConstraints(Date fromDate, stringQuery += " AND appointment.patient=:patient"; } + stringQuery += " ORDER BY appointment.timeSlot.startDate"; + Query query = super.sessionFactory.getCurrentSession().createQuery( stringQuery); diff --git a/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java b/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java index e4662380..ed1e08e7 100644 --- a/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java +++ b/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java @@ -329,9 +329,9 @@ public void shouldGetAllUnvoidedAppointmentsByType_getAppointmentsByConstraints( assertEquals(7, appointments.size()); type = service.getAppointmentType(3); - Appointment specificAppointment = service.getAppointment(4); + Appointment specificAppointment = service.getAppointment(5); appointments = service.getAppointmentsByConstraints(null, null, null, - null, type, null); + null, type, null); // apt #5 should be first because we are sorting by time slot assertEquals(specificAppointment, appointments.iterator().next()); assertEquals(3, appointments.size()); } @@ -402,6 +402,27 @@ public void shouldgetAllUnvoidedAppointmentsByPatientAndStatus_getAppointmentsBy assertEquals(patient, appointments.get(3).getPatient()); } + @Test + public void shouldProperlySortAppointmentsByTimeSlotDate_getAppointmentsByConstraints() { + + Patient patient = Context.getPatientService().getPatient(1); + + List appointmentStatuses = Arrays.asList( + AppointmentStatus.SCHEDULED, AppointmentStatus.RESCHEDULED); + + List appointments = service.getAppointmentsByConstraints( + null, null, null, null, null, patient, appointmentStatuses); + + assertNotNull(appointments); + assertEquals(4, appointments.size()); + + // verify they are in order + assertEquals(new Integer(6), appointments.get(0).getId()); + assertEquals(new Integer(1), appointments.get(1).getId()); + assertEquals(new Integer(4), appointments.get(2).getId()); + assertEquals(new Integer(7), appointments.get(3).getId()); + } + @Test @Verifies(value = "Should get correct appointments", method = "getAppointmentsByStatus(List)") public void shouldGetCorrectAppointments_getAppointmentsByStatus() { diff --git a/omod/src/test/java/org/openmrs/module/appointmentscheduling/rest/controller/AppointmentResource1_9ControllerTest.java b/omod/src/test/java/org/openmrs/module/appointmentscheduling/rest/controller/AppointmentResource1_9ControllerTest.java index 7281b210..334c57cf 100644 --- a/omod/src/test/java/org/openmrs/module/appointmentscheduling/rest/controller/AppointmentResource1_9ControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/appointmentscheduling/rest/controller/AppointmentResource1_9ControllerTest.java @@ -240,10 +240,11 @@ public void shouldFetchAppointmentsByPatient() throws Exception { List> appointments = (List>) deserialize( handle(req)).get("results"); Assert.assertEquals(2, appointments.size()); + Assert.assertEquals("c0c579b0-8e59-401d-8a4a-976a0b183606", + appointments.get(0).get("uuid")); Assert.assertEquals("c0c579b0-8e59-401d-8a4a-976a0b183602", - appointments.get(0).get("uuid")); - Assert.assertEquals("c0c579b0-8e59-401d-8a4a-976a0b183606", appointments.get(1).get("uuid")); + } @Test