From 63f7c4e0b156b932bdce01b9bec65b4059dd967c Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Mon, 26 Feb 2024 11:24:05 +0530 Subject: [PATCH] Kavitha | add date check and tests for waitlist appointments --- .../dao/impl/AppointmentDaoImpl.java | 10 ++++++- .../controller/AppointmentControllerIT.java | 9 ++++++ .../controller/AppointmentControllerTest.java | 30 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/org/openmrs/module/appointments/dao/impl/AppointmentDaoImpl.java b/api/src/main/java/org/openmrs/module/appointments/dao/impl/AppointmentDaoImpl.java index d6f92bc8e..1b67cf95b 100644 --- a/api/src/main/java/org/openmrs/module/appointments/dao/impl/AppointmentDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/appointments/dao/impl/AppointmentDaoImpl.java @@ -8,6 +8,7 @@ import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.hibernate.sql.JoinType; +import org.openmrs.api.context.Context; import org.openmrs.module.appointments.dao.AppointmentDao; import org.openmrs.module.appointments.model.Appointment; import org.openmrs.module.appointments.model.AppointmentSearchRequest; @@ -160,13 +161,20 @@ public List search(AppointmentSearchRequest appointmentSearchReques Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Appointment.class); criteria.add(Restrictions.eq("voided", false)); - criteria.addOrder(Order.asc("startDateTime")); + if (appointmentSearchRequest.getStartDate() != null) { + criteria.addOrder(Order.asc("startDateTime")); + } else { + criteria.addOrder(Order.asc("dateCreated")); + } setDateCriteria(appointmentSearchRequest, criteria); setPatientCriteria(appointmentSearchRequest, criteria); setLimitCriteria(appointmentSearchRequest, criteria); setProviderCriteria(appointmentSearchRequest, criteria); setStatusCriteria(appointmentSearchRequest, criteria); + String limit = Context.getAdministrationService().getGlobalProperty("webservices.rest.maxResultsDefault"); + if(StringUtils.isNotEmpty(limit)) + criteria.setMaxResults(Integer.parseInt(limit)); return criteria.list(); } diff --git a/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerIT.java b/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerIT.java index 90b040625..edaa69d42 100644 --- a/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerIT.java +++ b/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerIT.java @@ -166,6 +166,15 @@ public void should_SearchForDatelessAppointments() throws Exception { assertEquals(200, response.getStatus()); } + @Test + public void should_SearchForWaitListAppointments() throws Exception { + String content = "{ \"status\": \"WaitList\" }"; + + MockHttpServletResponse response = handle(newPostRequest("/rest/v1/appointment/search", content)); + assertNotNull(response); + assertEquals(200, response.getStatus()); + } + @Test public void shouldCreateAuditEventsWhenDetailsChangesOnEditAppointment() throws Exception { String content = "{ \"uuid\": \"c36006e5-9fbb-4f20-866b-0ece245615a7\", " + diff --git a/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerTest.java b/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerTest.java index 5e7f80c33..532ae652a 100644 --- a/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerTest.java @@ -270,6 +270,36 @@ public void shouldSearchForDatelessAppointments() throws Exception{ assertEquals(expectedAppointmentResponse.getUuid(), appointmentResponse.getUuid()); } + @Test + public void shouldSearchForWaitListAppointments() throws Exception{ + List appointments = new ArrayList<>(); + Appointment appointment = new Appointment(); + appointment.setUuid("appointmentUuid"); + appointment.setStatus(AppointmentStatus.WaitList); + Patient patient = new Patient(); + patient.setUuid("somePatientUuid"); + appointment.setPatient(patient); + appointments.add(appointment); + AppointmentQuery appointmentQuery = new AppointmentQuery(); + appointmentQuery.setStatus("WaitList"); + + + AppointmentDefaultResponse appointmentDefaultResponse = new AppointmentDefaultResponse(); + appointmentDefaultResponse.setUuid("appointmentUuid"); + + List appointmentDefaultResponses = new ArrayList<>(); + appointmentDefaultResponses.add(appointmentDefaultResponse); + + when(appointmentMapper.mapQueryToAppointment(appointmentQuery)).thenReturn(appointment); + when(appointmentsService.search(appointment)).thenReturn(appointments); + when(appointmentMapper.constructResponse(appointments)).thenReturn(appointmentDefaultResponses); + + List appointmentResponses = appointmentController.searchAppointments(appointmentQuery); + AppointmentDefaultResponse appointmentResponse = appointmentResponses.get(0); + AppointmentDefaultResponse expectedAppointmentResponse = appointmentDefaultResponses.get(0); + assertEquals(expectedAppointmentResponse.getUuid(), appointmentResponse.getUuid()); + } + @Test public void shouldSaveAnAppointment() throws Exception{ AppointmentRequest appointmentRequest = new AppointmentRequest();