Skip to content

Commit

Permalink
Address PR reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
jayasanka-sack committed Dec 9, 2024
1 parent a1dd8a0 commit 1aea583
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public class HibernateVisitDAO implements VisitDAO {
@Autowired
private SessionFactory sessionFactory;

public List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient, int startIndex, int limit) {
public List<VisitWithDiagnoses> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

public interface VisitDAO {

List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient, int startIndex, int limit);
List<VisitWithDiagnoses> getVisitsWithNotesAndDiagnosesByPatient(Patient patient, int startIndex, int limit);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openmrs.module.emrapi.visit;

import org.openmrs.Patient;

import java.util.List;

public interface VisitWithDiagnosesService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public List<VisitWithDiagnoses> 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);
}
}
21 changes: 16 additions & 5 deletions api/src/test/java/org/openmrs/module/emrapi/db/VisitDAOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class VisitDAOTest extends EmrApiContextSensitiveTest {

@Before
public void setup() {
executeDataSet("baseTestDataset.xml");
executeDataSet("pastVisitSetup.xml");
}

Expand All @@ -32,11 +33,11 @@ public void shouldFetchVisitsByPatientId() {
Patient patient = new Patient();
patient.setPatientId(109);

List<VisitWithDiagnoses> visits = visitDAO.getVisitsByPatientId(patient,0,10);
List<VisitWithDiagnoses> 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<Encounter> firstVisitEncounters = firstVisit.getEncounters();
Set<Diagnosis> firstVisitDiagnoses = firstVisit.getDiagnoses();

Expand All @@ -49,7 +50,7 @@ public void shouldFetchVisitsByPatientId() {
assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid);
}

VisitWithDiagnoses secondVisit = visits.get(0);
VisitWithDiagnoses secondVisit = visits.get(1);
Set<Encounter> secondVisitEncounters = secondVisit.getEncounters();
Set<Diagnosis> secondVisitDiagnoses = secondVisit.getDiagnoses();

Expand All @@ -61,14 +62,24 @@ public void shouldFetchVisitsByPatientId() {
for (Encounter encounter : secondVisitEncounters) {
assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid);
}

VisitWithDiagnoses thirdVisit = visits.get(0);
Set<Encounter> thirdVisitEncounters = thirdVisit.getEncounters();
Set<Diagnosis> thirdVisitDiagnoses = thirdVisit.getDiagnoses();

assert thirdVisit.getId() == 1017;
assert thirdVisit.getPatient().getPatientId() == 109;
assert thirdVisitEncounters.isEmpty();
assert thirdVisitDiagnoses.isEmpty();

}

@Test
public void shouldFetchVisitsByPatientIdWithPagination() {
Patient patient = new Patient();
patient.setPatientId(109);

List<VisitWithDiagnoses> visits = visitDAO.getVisitsByPatientId(patient,0,1);
List<VisitWithDiagnoses> visits = visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,1);
assertNotNull(visits);
assert visits.size() == 1;

Expand Down
3 changes: 3 additions & 0 deletions api/src/test/resources/pastVisitSetup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<encounter_diagnosis diagnosis_id="3" encounter_id="2003" patient_id="109" creator="1" date_created="2023-11-01 10:15:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="aef38d76-ffde-47a1-8717-5c74fddc2c35"/>
<encounter_diagnosis diagnosis_id="4" encounter_id="2004" patient_id="109" creator="1" date_created="2023-11-01 10:25:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="b3241d9c-5d78-4d9c-bef6-0c264dfa3892"/>

<!-- Third Visit for Scott Clark -->
<visit visit_id="1017" patient_id="109" visit_type_id="1" date_started="2023-12-05 14:00:00.0" date_stopped="2023-12-05 14:30:00.0" location_id="1901" creator="1" date_created="2023-12-05 14:05:00.0" voided="0" uuid="3c72f2bc-9b18-11e0-93c3-18a905e044ec" />

<!-- Patient 2: John Doe -->
<person person_id="110" gender="M" dead="false" creator="1" birthdate_estimated="0" date_created="2023-10-20 09:50:00.0" voided="false" void_reason="" uuid="c672b5ee-452d-4c97-b2d2-8b5ea84c8d0b"/>
<person_name person_name_id="110" person_id="110" preferred="true" prefix="" given_name="John" middle_name="" family_name="Doe" family_name_suffix="" creator="1" date_created="2023-10-20 09:51:00.0" voided="false" void_reason="" uuid="5dc9b93f-4ebf-489e-98c6-fc52b362d12b"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<VisitWithDiagnoses> visits;
visits = visitWithDiagnosesService.getVisitsByPatientId(patientUuid, context.getStartIndex(), context.getLimit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -50,13 +51,13 @@ public void shouldGetVisitsByPatientId() throws Exception {
Map<String, Object> result = objectMapper.readValue(jsonResponse, Map.class);

assertNotNull(result);
assert result.get("totalCount").equals(2);
assert result.get("totalCount").equals(3);

List<Map<String, Object>> visits = (List<Map<String, Object>>) result.get("pageOfResults");
assert visits.size() == 2;
assert visits.size() == 3;

// extract the first visit and check its properties
Map<String, Object> firstVisit = visits.get(1);
Map<String, Object> firstVisit = visits.get(2);
List<Map<String, Object>> firstVisitEncounters = (List<Map<String, Object>>) firstVisit.get("encounters");
List<Map<String, Object>> firstVisitDiagnoses = (List<Map<String, Object>>) firstVisit.get("diagnoses");

Expand All @@ -69,7 +70,7 @@ public void shouldGetVisitsByPatientId() throws Exception {
}

// extract the second visit and check its properties
Map<String, Object> secondVisit = visits.get(0);
Map<String, Object> secondVisit = visits.get(1);
List<Map<String, Object>> secondVisitEncounters = (List<Map<String, Object>>) secondVisit.get("encounters");
List<Map<String, Object>> secondVisitDiagnoses = (List<Map<String, Object>>) secondVisit.get("diagnoses");

Expand All @@ -80,6 +81,15 @@ public void shouldGetVisitsByPatientId() throws Exception {
for (Map<String, Object> encounter : secondVisitEncounters) {
assert ((String) encounter.get("display")).startsWith("Visit Note");
}

// extract the third visit and check its properties (no notes or diagnoses)
Map<String, Object> thirdVisit = visits.get(0);
List<Map<String, Object>> thirdVisitEncounters = (List<Map<String, Object>>) thirdVisit.get("encounters");
List<Map<String, Object>> thirdVisitDiagnoses = (List<Map<String, Object>>) thirdVisit.get("diagnoses");

assert thirdVisit.get("uuid").equals(thirdVisitUuid);
assert thirdVisitEncounters.isEmpty();
assert thirdVisitDiagnoses.isEmpty();
}

@Test
Expand Down
3 changes: 3 additions & 0 deletions omod/src/test/resources/pastVisitSetup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<encounter_diagnosis diagnosis_id="3" encounter_id="2003" patient_id="109" creator="1" date_created="2023-11-01 10:15:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="aef38d76-ffde-47a1-8717-5c74fddc2c35"/>
<encounter_diagnosis diagnosis_id="4" encounter_id="2004" patient_id="109" creator="1" date_created="2023-11-01 10:25:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="b3241d9c-5d78-4d9c-bef6-0c264dfa3892"/>

<!-- Third Visit for Scott Clark -->
<visit visit_id="1017" patient_id="109" visit_type_id="1" date_started="2023-12-05 14:00:00.0" date_stopped="2023-12-05 14:30:00.0" location_id="1901" creator="1" date_created="2023-12-05 14:05:00.0" voided="0" uuid="3c72f2bc-9b18-11e0-93c3-18a905e044ec" />

<!-- Patient 2: John Doe -->
<person person_id="110" gender="M" dead="false" creator="1" birthdate_estimated="0" date_created="2023-10-20 09:50:00.0" voided="false" void_reason="" uuid="c672b5ee-452d-4c97-b2d2-8b5ea84c8d0b"/>
<person_name person_name_id="110" person_id="110" preferred="true" prefix="" given_name="John" middle_name="" family_name="Doe" family_name_suffix="" creator="1" date_created="2023-10-20 09:51:00.0" voided="false" void_reason="" uuid="5dc9b93f-4ebf-489e-98c6-fc52b362d12b"/>
Expand Down

0 comments on commit 1aea583

Please sign in to comment.