diff --git a/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java b/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java index c343b6e3..d8df56cb 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java +++ b/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java @@ -23,15 +23,15 @@ public class HibernateVisitDAO implements VisitDAO { @Autowired private SessionFactory sessionFactory; - public List getVisitsByPatientId(Patient patient, int startIndex, int limit) { + public List getVisitsWithNotesAndDiagnosesByPatient(Patient patient, int startIndex, int limit) { String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79"; String hqlVisit="SELECT DISTINCT v FROM Visit v " + - "JOIN FETCH v.encounters enc " + - "JOIN enc.encounterType et " + + "LEFT JOIN FETCH v.encounters enc " + + "LEFT JOIN enc.encounterType et " + "WHERE v.patient.id = :patientId " + - "AND et.uuid = :encounterTypeUuid " + + "AND (et.uuid = :encounterTypeUuid OR enc IS NULL) " + "ORDER BY v.startDatetime DESC"; Query visitQuery = sessionFactory.getCurrentSession().createQuery(hqlVisit); diff --git a/api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java b/api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java index 24d3f1e5..c90acec3 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java +++ b/api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java @@ -7,5 +7,5 @@ public interface VisitDAO { - List getVisitsByPatientId(Patient patient, int startIndex, int limit); + List getVisitsWithNotesAndDiagnosesByPatient(Patient patient, int startIndex, int limit); } diff --git a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesService.java b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesService.java index 07bd2917..4a5c2ed3 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesService.java +++ b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesService.java @@ -1,5 +1,7 @@ package org.openmrs.module.emrapi.visit; +import org.openmrs.Patient; + import java.util.List; public interface VisitWithDiagnosesService { diff --git a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java index edba3edd..4c3e9abb 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java @@ -28,6 +28,6 @@ public List getVisitsByPatientId(String patientUuid, int sta throw new ObjectNotFoundException("No patient found with uuid " + patientUuid, Patient.class.getName()); } - return visitDAO.getVisitsByPatientId(patient, startIndex, limit); + return visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient, startIndex, limit); } } diff --git a/api/src/test/java/org/openmrs/module/emrapi/db/VisitDAOTest.java b/api/src/test/java/org/openmrs/module/emrapi/db/VisitDAOTest.java index b4d917df..ba65ca32 100644 --- a/api/src/test/java/org/openmrs/module/emrapi/db/VisitDAOTest.java +++ b/api/src/test/java/org/openmrs/module/emrapi/db/VisitDAOTest.java @@ -22,6 +22,7 @@ public class VisitDAOTest extends EmrApiContextSensitiveTest { @Before public void setup() { + executeDataSet("baseTestDataset.xml"); executeDataSet("pastVisitSetup.xml"); } @@ -32,11 +33,11 @@ public void shouldFetchVisitsByPatientId() { Patient patient = new Patient(); patient.setPatientId(109); - List visits = visitDAO.getVisitsByPatientId(patient,0,10); + List visits = visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,10); assertNotNull(visits); - assert visits.size() == 2; + assert visits.size() == 3; - VisitWithDiagnoses firstVisit = visits.get(1); + VisitWithDiagnoses firstVisit = visits.get(2); Set firstVisitEncounters = firstVisit.getEncounters(); Set firstVisitDiagnoses = firstVisit.getDiagnoses(); @@ -49,7 +50,7 @@ public void shouldFetchVisitsByPatientId() { assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid); } - VisitWithDiagnoses secondVisit = visits.get(0); + VisitWithDiagnoses secondVisit = visits.get(1); Set secondVisitEncounters = secondVisit.getEncounters(); Set secondVisitDiagnoses = secondVisit.getDiagnoses(); @@ -61,6 +62,16 @@ public void shouldFetchVisitsByPatientId() { for (Encounter encounter : secondVisitEncounters) { assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid); } + + VisitWithDiagnoses thirdVisit = visits.get(0); + Set thirdVisitEncounters = thirdVisit.getEncounters(); + Set thirdVisitDiagnoses = thirdVisit.getDiagnoses(); + + assert thirdVisit.getId() == 1017; + assert thirdVisit.getPatient().getPatientId() == 109; + assert thirdVisitEncounters.isEmpty(); + assert thirdVisitDiagnoses.isEmpty(); + } @Test @@ -68,7 +79,7 @@ public void shouldFetchVisitsByPatientIdWithPagination() { Patient patient = new Patient(); patient.setPatientId(109); - List visits = visitDAO.getVisitsByPatientId(patient,0,1); + List visits = visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,1); assertNotNull(visits); assert visits.size() == 1; diff --git a/api/src/test/resources/pastVisitSetup.xml b/api/src/test/resources/pastVisitSetup.xml index 03ad5d6a..dbfc183c 100644 --- a/api/src/test/resources/pastVisitSetup.xml +++ b/api/src/test/resources/pastVisitSetup.xml @@ -38,6 +38,9 @@ + + + diff --git a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java index 2772c01a..409a67af 100644 --- a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java +++ b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java @@ -2,6 +2,7 @@ import org.hibernate.ObjectNotFoundException; import org.openmrs.Diagnosis; +import org.openmrs.Patient; import org.openmrs.module.emrapi.visit.VisitWithDiagnoses; import org.openmrs.module.emrapi.visit.VisitWithDiagnosesService; import org.openmrs.module.webservices.rest.SimpleObject; @@ -29,11 +30,11 @@ public class VisitController { @Autowired VisitWithDiagnosesService visitWithDiagnosesService; - @RequestMapping(method = RequestMethod.GET, value = "/rest/**/emrapi/patient/{patientUuid}/visit") + @RequestMapping(method = RequestMethod.GET, value = "/rest/**/emrapi/patient/{patientUuid}/visitWithNotesAndDiagnoses") public ResponseEntity getVisitsByPatientId( HttpServletRequest request, HttpServletResponse response, - @PathVariable String patientUuid) { + @PathVariable("patientUuid") String patientUuid) { RequestContext context = RestUtil.getRequestContext(request, response, Representation.DEFAULT); List visits; visits = visitWithDiagnosesService.getVisitsByPatientId(patientUuid, context.getStartIndex(), context.getLimit()); diff --git a/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java b/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java index 4dc65beb..468f79b2 100644 --- a/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java @@ -39,8 +39,9 @@ public void shouldGetVisitsByPatientId() throws Exception { String patientUuid = "8604d42e-3ca8-11e3-bf2b-0d0c09861e97"; String firstVisitUuid = "1esd5218-6b78-11e0-93c3-18a905e044dc"; String secondVisitUuid = "1c72e1ac-9b18-11e0-93c3-18a905e044dc"; + String thirdVisitUuid = "3c72f2bc-9b18-11e0-93c3-18a905e044ec"; - MvcResult response = mockMvc.perform(get("/rest/v1/emrapi/patient/" + patientUuid + "/visit") + MvcResult response = mockMvc.perform(get("/rest/v1/emrapi/patient/" + patientUuid+"/visitWithNotesAndDiagnoses") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn(); @@ -50,13 +51,13 @@ public void shouldGetVisitsByPatientId() throws Exception { Map result = objectMapper.readValue(jsonResponse, Map.class); assertNotNull(result); - assert result.get("totalCount").equals(2); + assert result.get("totalCount").equals(3); List> visits = (List>) result.get("pageOfResults"); - assert visits.size() == 2; + assert visits.size() == 3; // extract the first visit and check its properties - Map firstVisit = visits.get(1); + Map firstVisit = visits.get(2); List> firstVisitEncounters = (List>) firstVisit.get("encounters"); List> firstVisitDiagnoses = (List>) firstVisit.get("diagnoses"); @@ -69,7 +70,7 @@ public void shouldGetVisitsByPatientId() throws Exception { } // extract the second visit and check its properties - Map secondVisit = visits.get(0); + Map secondVisit = visits.get(1); List> secondVisitEncounters = (List>) secondVisit.get("encounters"); List> secondVisitDiagnoses = (List>) secondVisit.get("diagnoses"); @@ -80,6 +81,15 @@ public void shouldGetVisitsByPatientId() throws Exception { for (Map encounter : secondVisitEncounters) { assert ((String) encounter.get("display")).startsWith("Visit Note"); } + + // extract the third visit and check its properties (no notes or diagnoses) + Map thirdVisit = visits.get(0); + List> thirdVisitEncounters = (List>) thirdVisit.get("encounters"); + List> thirdVisitDiagnoses = (List>) thirdVisit.get("diagnoses"); + + assert thirdVisit.get("uuid").equals(thirdVisitUuid); + assert thirdVisitEncounters.isEmpty(); + assert thirdVisitDiagnoses.isEmpty(); } @Test diff --git a/omod/src/test/resources/pastVisitSetup.xml b/omod/src/test/resources/pastVisitSetup.xml index 03ad5d6a..dbfc183c 100644 --- a/omod/src/test/resources/pastVisitSetup.xml +++ b/omod/src/test/resources/pastVisitSetup.xml @@ -38,6 +38,9 @@ + + +