-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EA-207: Implement an API for Fetching Visits with Notes and Diagnosis #250
base: master
Are you sure you want to change the base?
Changes from all commits
876bdee
efa8cb7
539eb15
612175d
0828b67
2ede16b
a1dd8a0
1aea583
875b405
8b20d2d
7f8fe94
a23b7c0
f2fa209
cade351
494e9b9
e8c79b1
b155ca9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.openmrs.module.emrapi.visit; | ||
|
||
import java.util.List; | ||
|
||
public interface EmrApiVisitService { | ||
/** | ||
* Fetches visits with note encounters and diagnoses of a patient by patient ID. | ||
* | ||
* @param patientUuid the UUID of the patient | ||
* @return a list of visits that has note encounters and diagnoses | ||
*/ | ||
List<VisitWithDiagnoses> getVisitsWithNotesAndDiagnosesByPatient(String patientUuid, int startIndex, int limit); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.openmrs.module.emrapi.visit; | ||
|
||
import lombok.Setter; | ||
import org.hibernate.ObjectNotFoundException; | ||
import org.openmrs.Patient; | ||
import org.openmrs.api.PatientService; | ||
import org.openmrs.api.impl.BaseOpenmrsService; | ||
import org.openmrs.module.emrapi.db.EmrApiDAO; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Setter | ||
@Service | ||
public class EmrApiVisitServiceImpl extends BaseOpenmrsService implements EmrApiVisitService { | ||
|
||
PatientService patientService; | ||
EmrApiDAO emrApiDAO; | ||
|
||
@Override | ||
public List<VisitWithDiagnoses> getVisitsWithNotesAndDiagnosesByPatient(String patientUuid, int startIndex, int limit) { | ||
|
||
Patient patient = patientService.getPatientByUuid(patientUuid); | ||
|
||
if (patient == null) { | ||
throw new ObjectNotFoundException("No patient found with uuid " + patientUuid, Patient.class.getName()); | ||
} | ||
|
||
return emrApiDAO.getVisitsWithNotesAndDiagnosesByPatient(patient, startIndex, limit); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.openmrs.module.emrapi.visit; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.openmrs.Diagnosis; | ||
import org.openmrs.Visit; | ||
|
||
import java.util.Set; | ||
|
||
@Setter | ||
@Getter | ||
public class VisitWithDiagnoses { | ||
|
||
private Visit visit; | ||
private Set<Diagnosis> diagnoses; | ||
|
||
public VisitWithDiagnoses(Visit visit, Set<Diagnosis> diagnoses) { | ||
jayasanka-sack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.visit = visit; | ||
this.diagnoses = diagnoses; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.openmrs.module.emrapi.db; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openmrs.Diagnosis; | ||
import org.openmrs.Encounter; | ||
import org.openmrs.Patient; | ||
import org.openmrs.module.emrapi.EmrApiContextSensitiveTest; | ||
import org.openmrs.module.emrapi.visit.VisitWithDiagnoses; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class EmrApiDaoTest extends EmrApiContextSensitiveTest { | ||
|
||
@Autowired | ||
private EmrApiDAO emrApiDAO; | ||
|
||
@Before | ||
public void setup() { | ||
executeDataSet("baseTestDataset.xml"); | ||
executeDataSet("pastVisitSetup.xml"); | ||
} | ||
|
||
@Test | ||
public void shouldFetchVisitsWithNotesAndDiagnosesByPatientId() { | ||
String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79"; | ||
|
||
Patient patient = new Patient(); | ||
patient.setPatientId(109); | ||
|
||
List<VisitWithDiagnoses> visits = emrApiDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,10); | ||
assertNotNull(visits); | ||
assert visits.size() == 3; | ||
|
||
VisitWithDiagnoses firstVisit = visits.get(2); | ||
Set<Encounter> firstVisitEncounters = firstVisit.getVisit().getEncounters(); | ||
Set<Diagnosis> firstVisitDiagnoses = firstVisit.getDiagnoses(); | ||
|
||
assert firstVisit.getVisit().getId() == 1014; | ||
assert firstVisit.getVisit().getPatient().getPatientId() == 109; | ||
assert firstVisitEncounters.size() == 2; | ||
assert firstVisitDiagnoses.size() == 3; | ||
|
||
for (Encounter encounter : firstVisitEncounters) { | ||
assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid); | ||
} | ||
|
||
VisitWithDiagnoses secondVisit = visits.get(1); | ||
Set<Encounter> secondVisitEncounters = secondVisit.getVisit().getEncounters(); | ||
Set<Diagnosis> secondVisitDiagnoses = secondVisit.getDiagnoses(); | ||
|
||
assert secondVisit.getVisit().getId() == 1015; | ||
assert secondVisit.getVisit().getPatient().getPatientId() == 109; | ||
assert secondVisitEncounters.size() == 1; | ||
assert secondVisitDiagnoses.size() == 2; | ||
|
||
for (Encounter encounter : secondVisitEncounters) { | ||
assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid); | ||
} | ||
|
||
VisitWithDiagnoses thirdVisit = visits.get(0); | ||
Set<Encounter> thirdVisitEncounters = thirdVisit.getVisit().getEncounters(); | ||
Set<Diagnosis> thirdVisitDiagnoses = thirdVisit.getDiagnoses(); | ||
|
||
assert thirdVisit.getVisit().getId() == 1017; | ||
assert thirdVisit.getVisit().getPatient().getPatientId() == 109; | ||
assert thirdVisitEncounters.isEmpty(); | ||
assert thirdVisitDiagnoses.isEmpty(); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldFetchVisitsWithNotesAndDiagnosesWithPagination() { | ||
Patient patient = new Patient(); | ||
patient.setPatientId(109); | ||
|
||
List<VisitWithDiagnoses> visits = emrApiDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,1); | ||
assertNotNull(visits); | ||
assert visits.size() == 1; | ||
|
||
VisitWithDiagnoses mostRecentVisit = visits.get(0); | ||
assert mostRecentVisit.getVisit().getId() == 1017; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this class being used, or is everything handled in the other "pastVisitSetup" class below? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both files are used in tests in both modules. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the distinction between them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah the organization is a little weird. It looks like there may have been some convention for all the metadata to go into a file like this and the data into a file like the other one, but both of them mix data and metadata, so it's confusing why we need the two. |
||
|
||
<dataset> | ||
|
||
<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"/> | ||
|
||
<!-- Patient Record --> | ||
<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 --> | ||
<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) --> | ||
<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"/> | ||
|
||
</dataset> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I wasn't aware of this FETCH syntax.... this is saying when fetching the encounters associated with the visit, limit to those which are notes? ie, although this query only returns visits, you can fetch the encounters via visit.encounters, and those will be limited to notes? @jayasanka-sack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you’re right!
P.S.
I used the FETCH syntax to load encounters eagerly instead of lazily.