Skip to content

Commit

Permalink
Handle invalid patient uuids
Browse files Browse the repository at this point in the history
  • Loading branch information
jayasanka-sack committed Oct 23, 2024
1 parent 876bdee commit efa8cb7
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ public class HibernateVisitDAO implements VisitDAO {
@Autowired
private SessionFactory sessionFactory;


// public void setSessionFactory(SessionFactory sessionFactory) {
// this.sessionFactory = sessionFactory;
// }


@Override
public List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient) {

String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79";
Expand Down Expand Up @@ -62,22 +55,11 @@ public List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient) {
.add(diagnosis);
}

//
List<VisitWithDiagnoses> visitWithDiagnoses = visits.stream()
.sorted(Comparator.comparing(Visit::getStartDatetime).reversed())
.map(visit -> new VisitWithDiagnoses(visit, visitToDiagnosesMap.getOrDefault(visit, new HashSet<>())))
.collect(Collectors.toList());

return visitWithDiagnoses;


// String queryString = "FROM Visit v WHERE v.patient.id = :patient_id";
//
// Query query = sessionFactory.getCurrentSession().createQuery(queryString);
// query.setParameter("patient_id", patient.getId());
//
// List list = query.list();
// return list;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public interface VisitWithDiagnosesService {
/**
* Fetches visits of a patient by patient ID.
*
* @param patientId the ID of the patient
* @param patientUuid the UUID of the patient
* @return a list of visits
*/
List<VisitWithDiagnoses> getVisitsByPatientId(Integer patientId);
}
List<VisitWithDiagnoses> getVisitsByPatientId(String patientUuid);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openmrs.module.emrapi.visit;

import org.hibernate.ObjectNotFoundException;
import org.openmrs.Patient;
import org.openmrs.api.PatientService;
import org.openmrs.api.impl.BaseOpenmrsService;
Expand All @@ -11,7 +12,6 @@

@Service
public class VisitWithDiagnosesServiceImpl extends BaseOpenmrsService implements VisitWithDiagnosesService {
// Existing methods and dependencies

@Autowired
PatientService patientService;
Expand All @@ -20,11 +20,14 @@ public class VisitWithDiagnosesServiceImpl extends BaseOpenmrsService implements
VisitDAO visitDAO;

@Override
public List<VisitWithDiagnoses> getVisitsByPatientId(Integer patientId) {
public List<VisitWithDiagnoses> getVisitsByPatientId(String patientUuid) {

Patient patient = patientService.getPatient(patientId);
Patient patient = patientService.getPatientByUuid(patientUuid);

if (patient == null) {
throw new ObjectNotFoundException("No patient found with uuid " + patientUuid, Patient.class.getName());
}

return visitDAO.getVisitsByPatientId(patient);
// return null;
}
}
}
9 changes: 0 additions & 9 deletions api/src/test/resources/metadata/pastVisitSetup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

<dataset>

<!-- <metadatamapping_metadata_source-->
<!-- metadata_source_id = "2134"-->
<!-- creator = "1"-->
<!-- date_created="2008-08-18 12:38:58.0"-->
<!-- uuid="cf25712e-f193-48d5-a85a-5fd8d0242223"-->
<!-- name="org.openmrs.module.emrapi"-->
<!-- retired = "false"-->
<!-- />-->

<encounter_type encounter_type_id="1003" name="Visit Note" description="Visit note" creator="1" date_created="2023-10-15 10:00:00.0" retired="false" uuid="d7151f82-c1f3-4152-a605-2f9ea7414a79"/>
<encounter_type encounter_type_id="1004" name="Vitals" description="For capturing vital signs" creator="1" date_created="2023-10-15 10:00:00.0" retired="false" uuid="d92ca5dc-7fd0-4fa2-b726-ab16fbc53fc1"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.openmrs.module.emrapi.web.controller;

import org.hibernate.ObjectNotFoundException;
import org.openmrs.module.emrapi.visit.VisitWithDiagnoses;
import org.openmrs.module.emrapi.visit.VisitWithDiagnosesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -15,12 +17,17 @@
public class VisitController {

@Autowired
VisitWithDiagnosesService customVisitService;
VisitWithDiagnosesService visitWithDiagnosesService;


@RequestMapping(method = RequestMethod.GET, value = "/patient/{patientId}")
public ResponseEntity<List<VisitWithDiagnoses>> getVisitsByPatientId(@PathVariable Integer patientId) {
List<VisitWithDiagnoses> visits = customVisitService.getVisitsByPatientId(patientId);
@RequestMapping(method = RequestMethod.GET, value = "/rest/**/emrapi/patient/{patientUuid}/visit")
public ResponseEntity<?> getVisitsByPatientId(@PathVariable String patientUuid) {
List<VisitWithDiagnoses> visits;
try {
visits = visitWithDiagnosesService.getVisitsByPatientId(patientUuid);
} catch (ObjectNotFoundException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.ok(visits);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.Before;
import org.junit.Test;
import org.openmrs.Diagnosis;
import org.openmrs.Encounter;
import org.openmrs.api.PatientService;
import org.openmrs.module.emrapi.visit.VisitWithDiagnoses;
Expand All @@ -13,6 +14,7 @@
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class VisitControllerTest extends BaseModuleWebContextSensitiveTest {
Expand All @@ -37,15 +39,49 @@ public void setUp() throws Exception {
public void shouldGetVisitsByPatientId() {

String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79";
String patientUuid = "8604d42e-3ca8-11e3-bf2b-0d0c09861e97";

ResponseEntity<List<VisitWithDiagnoses>> visitsResponse = (ResponseEntity<List<VisitWithDiagnoses>>) visitController.getVisitsByPatientId(patientUuid);
assertNotNull(visitsResponse);

List<VisitWithDiagnoses> visits = visitsResponse.getBody();

ResponseEntity<List<VisitWithDiagnoses>> visits = visitController.getVisitsByPatientId(109);
assertNotNull(visits);
assert visits.getBody().size() == 1;
Set<Encounter> encounters = visits.getBody().get(0).getEncounters();
assert visits.size() == 2;

VisitWithDiagnoses firstVisit = visits.get(1);
Set<Encounter> firstVisitEncounters = firstVisit.getEncounters();
Set<Diagnosis> firstVisitDiagnoses = firstVisit.getDiagnoses();

assert firstVisit.getId() == 1014;
assertEquals(firstVisit.getPatient().getUuid(), patientUuid);
assert firstVisitEncounters.size() == 2;
assert firstVisitDiagnoses.size() == 3;

for (Encounter encounter : encounters) {
System.out.println(encounter.getEncounterType().getUuid());
for (Encounter encounter : firstVisitEncounters) {
assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid);
}

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

assert secondVisit.getId() == 1015;
assertEquals(secondVisit.getPatient().getUuid(), patientUuid);
assert secondVisitEncounters.size() == 1;
assert secondVisitDiagnoses.size() == 2;

for (Encounter encounter : secondVisitEncounters) {
assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid);
}

}

@Test
public void shouldThrowExceptionWhenPatientUuidIsInvalid() {
String invalidPatientUuid = "invalid-uuid";
ResponseEntity<?> visitsResponse = visitController.getVisitsByPatientId(invalidPatientUuid);
assertNotNull(visitsResponse);
assert visitsResponse.getStatusCode().is4xxClientError();
}
}
71 changes: 43 additions & 28 deletions omod/src/test/resources/pastVisitSetup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,58 @@

<dataset>

<!-- <metadatamapping_metadata_source-->
<!-- metadata_source_id = "2134"-->
<!-- creator = "1"-->
<!-- date_created="2008-08-18 12:38:58.0"-->
<!-- uuid="cf25712e-f193-48d5-a85a-5fd8d0242223"-->
<!-- name="org.openmrs.module.emrapi"-->
<!-- retired = "false"-->
<!-- />-->

<!-- Encounter Types -->
<encounter_type encounter_type_id="1003" name="Visit Note" description="Visit note" creator="1" date_created="2023-10-15 10:00:00.0" retired="false" uuid="d7151f82-c1f3-4152-a605-2f9ea7414a79"/>
<encounter_type encounter_type_id="1004" name="Vitals" description="For capturing vital signs" creator="1" date_created="2023-10-15 10:00:00.0" retired="false" uuid="d92ca5dc-7fd0-4fa2-b726-ab16fbc53fc1"/>

<!-- Person (Patient) Information -->
<person person_id="109" gender="M" dead="false" creator="1" birthdate_estimated="0"
date_created="2023-10-15 10:30:00.0" voided="false" void_reason=""
uuid="8604d42e-3ca8-11e3-bf2b-0d0c09861e97"/>
<person_name person_name_id="109" person_id="109" preferred="true" prefix="" given_name="Scott" middle_name=""
family_name="Clark" family_name_suffix="" creator="1" date_created="2023-10-15 10:31:00.0"
voided="false" void_reason="" uuid="8f9e3a7b-6482-487d-9abe-c07b123c4f08"/>
<encounter_type encounter_type_id="1004" name="Consultation" description="Consultation encounter" creator="1" date_created="2023-10-15 10:00:00.0" retired="false" uuid="d92ca5dc-7fd0-4fa2-b726-ab16fbc53fc1"/>

<!-- Patient Record -->
<!-- Patient 1: Scott Clark -->
<person person_id="109" gender="M" dead="false" creator="1" birthdate_estimated="0" date_created="2023-10-15 10:30:00.0" voided="false" void_reason="" uuid="8604d42e-3ca8-11e3-bf2b-0d0c09861e97"/>
<person_name person_name_id="109" person_id="109" preferred="true" prefix="" given_name="Scott" middle_name="" family_name="Clark" family_name_suffix="" creator="1" date_created="2023-10-15 10:31:00.0" voided="false" void_reason="" uuid="8f9e3a7b-6482-487d-9abe-c07b123c4f08"/>
<patient patient_id="109" creator="1" date_created="2023-10-15 10:32:00.0" voided="false" />

<!-- Location Information -->
<location location_id="1901" name="Location tagged to visit" creator="1" date_created="2023-10-15 10:35:00.0" retired="false" uuid="f1771d8e-bf1f-4dc5-957f-9d40a5eebf08"/>

<!-- Visit Information -->
<!-- First Visit for Scott Clark -->
<visit visit_id="1014" patient_id="109" visit_type_id="1" date_started="2023-10-20 09:00:00.0" date_stopped="2023-10-20 09:30:00.0" location_id="1901" creator="1" date_created="2023-10-20 09:05:00.0" voided="0" uuid="1esd5218-6b78-11e0-93c3-18a905e044dc" />

<!-- Encounter Information (Visit Note) -->
<!-- First Visit - Encounters for Scott Clark (3 encounters) -->
<encounter encounter_id="2001" visit_id="1014" encounter_type="1003" patient_id="109" location_id="1901" form_id="1" encounter_datetime="2023-10-20 09:10:00.0" creator="1" date_created="2023-10-20 09:12:00.0" voided="false" uuid="a123d653-444b-4118-9c83-a3715b82d4ac"/>
<encounter encounter_id="2002" visit_id="1014" encounter_type="1004" patient_id="109" location_id="1901" form_id="1" encounter_datetime="2023-10-20 09:10:00.0" creator="1" date_created="2023-10-20 09:12:00.0" voided="false" uuid="a9a41017-2593-442a-bb88-c80358692d1e"/>

<!-- &lt;!&ndash; Metadata mapping for Visit Note encounter type &ndash;&gt;-->
<!-- <metadatamapping_metadata_term_mapping metadata_term_mapping_id="3945" metadata_source_id="2134"-->
<!-- creator="1" date_created="2023-10-20 12:38:58.0" uuid="111bbad0-d565-11e3-9c1a-0800200c9a665"-->
<!-- retired="false" code="emr.visitNoteEncounterType" name="emr.visitNoteEncounterType"-->
<!-- metadata_uuid="06087e80-d565-11e3-9c1a-0800200c9a66" metadata_class="org.openmrs.EncounterType"/>-->
<encounter encounter_id="2002" visit_id="1014" encounter_type="1004" patient_id="109" location_id="1901" form_id="1" encounter_datetime="2023-10-20 09:20:00.0" creator="1" date_created="2023-10-20 09:22:00.0" voided="false" uuid="a9a41017-2593-442a-bb88-c80358692d1e"/>
<encounter encounter_id="2008" visit_id="1014" encounter_type="1003" patient_id="109" location_id="1901" form_id="1" encounter_datetime="2023-10-20 09:25:00.0" creator="1" date_created="2023-10-20 09:27:00.0" voided="false" uuid="b2f67239-8a69-4f68-8fbe-6a248efc2b44"/>

<!-- Diagnoses for First Visit -->
<encounter_diagnosis diagnosis_id="1" encounter_id="2001" patient_id="109" creator="1" date_created="2023-10-20 09:15:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="47dd2d5e-5cfe-4fdd-b284-3147e60d4d25"/>
<encounter_diagnosis diagnosis_id="2" encounter_id="2002" patient_id="109" creator="1" date_created="2023-10-20 09:25:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="b9dd36c2-3bf7-4a20-8ddc-88ccf75ea4f0"/>
<encounter_diagnosis diagnosis_id="8" encounter_id="2008" patient_id="109" creator="1" date_created="2023-10-20 09:27:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="28c583be-d9ae-4d0f-8d88-df7fcffb2d22"/>

<!-- Second Visit for Scott Clark -->
<visit visit_id="1015" patient_id="109" visit_type_id="1" date_started="2023-11-01 10:00:00.0" date_stopped="2023-11-01 10:30:00.0" location_id="1901" creator="1" date_created="2023-11-01 10:05:00.0" voided="0" uuid="1c72e1ac-9b18-11e0-93c3-18a905e044dc" />

<!-- Second Visit - Encounters for Scott Clark (2 encounters) -->
<encounter encounter_id="2003" visit_id="1015" encounter_type="1003" patient_id="109" location_id="1901" form_id="1" encounter_datetime="2023-11-01 10:10:00.0" creator="1" date_created="2023-11-01 10:12:00.0" voided="false" uuid="b1f847e9-444c-4a8c-bba5-cc8b8dc1b123"/>
<encounter encounter_id="2004" visit_id="1015" encounter_type="1004" patient_id="109" location_id="1901" form_id="1" encounter_datetime="2023-11-01 10:20:00.0" creator="1" date_created="2023-11-01 10:25:00.0" voided="false" uuid="d58e3e83-dbd4-4ba8-a61f-b4ff5c1bc1e8"/>

<!-- Diagnoses for Second Visit -->
<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"/>

<!-- 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"/>
<patient patient_id="110" creator="1" date_created="2023-10-20 09:52:00.0" voided="false" />

<!-- Visit Information for John Doe -->
<visit visit_id="1016" patient_id="110" visit_type_id="1" date_started="2023-10-25 08:30:00.0" date_stopped="2023-10-25 09:00:00.0" location_id="1901" creator="1" date_created="2023-10-25 08:35:00.0" voided="0" uuid="2a1c9274-791b-41d3-82f9-65482d22df65" />

<!-- Encounters for John Doe (3 encounters) -->
<encounter encounter_id="2005" visit_id="1016" encounter_type="1003" patient_id="110" location_id="1901" form_id="1" encounter_datetime="2023-10-25 08:40:00.0" creator="1" date_created="2023-10-25 08:42:00.0" voided="false" uuid="d14f3c6a-4cf6-48d4-97be-97f5f73d6c16"/>
<encounter encounter_id="2006" visit_id="1016" encounter_type="1004" patient_id="110" location_id="1901" form_id="1" encounter_datetime="2023-10-25 08:45:00.0" creator="1" date_created="2023-10-25 08:47:00.0" voided="false" uuid="e354f65a-8b36-4eaf-9a54-1342f84faabc"/>
<encounter encounter_id="2007" visit_id="1016" encounter_type="1003" patient_id="110" location_id="1901" form_id="1" encounter_datetime="2023-11-05 09:00:00.0" creator="1" date_created="2023-11-05 09:05:00.0" voided="false" uuid="f6f8c39b-76f9-4879-8712-4cd178d2c2dc"/>

<!-- Diagnoses for John Doe -->
<encounter_diagnosis diagnosis_id="5" encounter_id="2005" patient_id="110" creator="1" date_created="2023-10-25 08:42:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="c0a9f874-2b47-4f58-93fc-d8a0d233b77f"/>
<encounter_diagnosis diagnosis_id="6" encounter_id="2006" patient_id="110" creator="1" date_created="2023-10-25 08:47:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="0b75c6d4-45a5-477a-bf7a-f5a0476a47d8"/>
<encounter_diagnosis diagnosis_id="7" encounter_id="2007" patient_id="110" creator="1" date_created="2023-11-05 09:05:00.0" rank="1" certainty="CONFIRMED" voided="false" uuid="33a9f72c-d911-42b9-950f-4f1a6201a20a"/>

</dataset>
11 changes: 0 additions & 11 deletions omod/src/test/resources/test-hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@

<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>

<!-- Format SQL for better readability -->
<property name="hibernate.format_sql">true</property>

<!-- Include comments in the generated SQL (optional but helpful for debugging) -->
<property name="hibernate.use_sql_comments">true</property>

<!-- Optionally, log parameter values for the SQL queries (using Hibernate 5.2+) -->
<property name="hibernate.type">TRACE</property>

<mapping resource="Provider.hbm.xml"/>
<mapping resource="ProviderRole.hbm.xml"/>
<mapping resource="ExportedPackage.hbm.xml" />
Expand Down

0 comments on commit efa8cb7

Please sign in to comment.