From a1367c711c3fb73f465afc6038e7a1d011d64d28 Mon Sep 17 00:00:00 2001 From: Mark Goodrich Date: Mon, 5 Aug 2024 16:18:15 -0400 Subject: [PATCH] EA-198: Add support for configuring Mother Child relationships within the EMR API module --- .../module/emrapi/EmrApiConstants.java | 2 + .../module/emrapi/EmrApiProperties.java | 5 + .../emrapi/maternal/MaternalService.java | 23 + .../emrapi/maternal/MaternalServiceImpl.java | 62 +++ .../module/emrapi/maternal/Newborn.java | 11 + .../main/resources/hql/newborns_by_mother.hql | 24 ++ .../resources/moduleApplicationContext.xml | 27 ++ .../maternal/MaternalServiceImplTest.java | 408 ++++++++++++++++++ api/src/test/resources/baseTestDataset.xml | 9 +- 9 files changed, 570 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalService.java create mode 100644 api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java create mode 100644 api/src/main/java/org/openmrs/module/emrapi/maternal/Newborn.java create mode 100644 api/src/main/resources/hql/newborns_by_mother.hql create mode 100644 api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java diff --git a/api/src/main/java/org/openmrs/module/emrapi/EmrApiConstants.java b/api/src/main/java/org/openmrs/module/emrapi/EmrApiConstants.java index b180c722..3f6807d9 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/EmrApiConstants.java +++ b/api/src/main/java/org/openmrs/module/emrapi/EmrApiConstants.java @@ -215,4 +215,6 @@ public class EmrApiConstants { )); public static final String GP_USE_LEGACY_DIAGNOSIS_SERVICE = "emrapi.useLegacyDiagnosisService"; + + public static final String METADATA_MAPPING_MOTHER_CHILD_RELATIONSHIP_TYPE = "emrapi.motherChildRelationshipType"; } diff --git a/api/src/main/java/org/openmrs/module/emrapi/EmrApiProperties.java b/api/src/main/java/org/openmrs/module/emrapi/EmrApiProperties.java index 59c612b8..585aefe7 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/EmrApiProperties.java +++ b/api/src/main/java/org/openmrs/module/emrapi/EmrApiProperties.java @@ -26,6 +26,7 @@ import org.openmrs.PatientIdentifierType; import org.openmrs.PersonAttributeType; import org.openmrs.Provider; +import org.openmrs.RelationshipType; import org.openmrs.Role; import org.openmrs.VisitType; import org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata; @@ -347,4 +348,8 @@ public List getDispositions() { public DispositionDescriptor getDispositionDescriptor() { return dispositionService.getDispositionDescriptor(); } + + public RelationshipType getMotherChildRelationshipType() { + return getEmrApiMetadataByCode(RelationshipType.class, EmrApiConstants.METADATA_MAPPING_MOTHER_CHILD_RELATIONSHIP_TYPE, false); + } } diff --git a/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalService.java b/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalService.java new file mode 100644 index 00000000..87c05c64 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalService.java @@ -0,0 +1,23 @@ +package org.openmrs.module.emrapi.maternal; + +import java.util.List; + +import org.openmrs.Location; +import org.openmrs.Patient; +import org.openmrs.api.OpenmrsService; + +public interface MaternalService extends OpenmrsService { + + /** + * Returns all "newborns" of the specified patient, where "newborn" is defined as a patient who is: + * - linked to the specified patient by a relationship of type emrapi.motherChildRelationshipType + * - has as an active visit (at the visitLocation, if specified) + * - has a birthdate that is on or after the start date of the mother's active visit (at the visitLocation, if specified) (note matches on date, not datetime to account for retrospective data entry or only have a date component of birthdate) + * + * @param mother + * @param visitLocation if not null, restrict matching visits to only those at the specified location + * @return + */ + public List getNewbornsByMother(Patient mother, Location visitLocation); + +} diff --git a/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java b/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java new file mode 100644 index 00000000..57b80321 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java @@ -0,0 +1,62 @@ +package org.openmrs.module.emrapi.maternal; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.openmrs.Location; +import org.openmrs.Patient; +import org.openmrs.RelationshipType; +import org.openmrs.Visit; +import org.openmrs.api.APIException; +import org.openmrs.api.impl.BaseOpenmrsService; +import org.openmrs.module.emrapi.EmrApiProperties; +import org.openmrs.module.emrapi.db.EmrApiDAO; + +public class MaternalServiceImpl extends BaseOpenmrsService implements MaternalService { + + private EmrApiProperties emrApiProperties; + + private EmrApiDAO emrApiDAO; + + public void setEmrApiProperties(EmrApiProperties emrApiProperties) { + this.emrApiProperties = emrApiProperties; + } + + public void setEmrApiDAO(EmrApiDAO emrApiDAO) { + this.emrApiDAO = emrApiDAO; + } + + public List getNewbornsByMother(Patient mother, Location visitLocation) { + + RelationshipType motherChildRelationshipType = emrApiProperties.getMotherChildRelationshipType(); + + if (motherChildRelationshipType == null) { + throw new APIException("Mother-Child relationship type has not been configured"); + } + + if (mother == null) { + throw new APIException("Mother cannot be null"); + } + + Map parameters = new HashMap<>(); + parameters.put("mother", mother); + parameters.put("motherChildRelationshipType", motherChildRelationshipType); + parameters.put("visitLocation", visitLocation); + + List l = emrApiDAO.executeHqlFromResource("hql/newborns_by_mother.hql", parameters, List.class); + + List ret = new ArrayList<>(); + + for (Object req : l) { + Object[] row = (Object[]) req; + Newborn newborn = new Newborn(); + newborn.setNewborn((Patient) row[0]); + newborn.setNewbornVisit((Visit) row[1]); + ret.add(newborn); + } + + return ret; + } +} diff --git a/api/src/main/java/org/openmrs/module/emrapi/maternal/Newborn.java b/api/src/main/java/org/openmrs/module/emrapi/maternal/Newborn.java new file mode 100644 index 00000000..5a3cf23b --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/Newborn.java @@ -0,0 +1,11 @@ +package org.openmrs.module.emrapi.maternal; + +import lombok.Data; +import org.openmrs.Patient; +import org.openmrs.Visit; + +@Data +public class Newborn { + private Patient newborn; + private Visit newbornVisit; +} diff --git a/api/src/main/resources/hql/newborns_by_mother.hql b/api/src/main/resources/hql/newborns_by_mother.hql new file mode 100644 index 00000000..b668e06b --- /dev/null +++ b/api/src/main/resources/hql/newborns_by_mother.hql @@ -0,0 +1,24 @@ +select + baby, + babyVisit +from + Person as mother, + Person as baby, + Relationship as motherChildRelationship, + Visit as motherVisit, + Visit as babyVisit +where + mother = :mother + and motherChildRelationship.personA = mother + and motherChildRelationship.personB = baby + and motherChildRelationship.relationshipType = :motherChildRelationshipType + and motherVisit.patient = mother and motherVisit.stopDatetime is null + and babyVisit.patient = baby and babyVisit.stopDatetime is null + and year(baby.birthdate) >= year(motherVisit.startDatetime) + and month(baby.birthdate) >= month(motherVisit.startDatetime) + and day(baby.birthdate) >= day(motherVisit.startDatetime) + and (:visitLocation is null or (motherVisit.location = :visitLocation and babyVisit.location = :visitLocation)) + and mother.voided = false and baby.voided = false and motherChildRelationship.voided = false and motherVisit.voided = false and babyVisit.voided = false + + + diff --git a/api/src/main/resources/moduleApplicationContext.xml b/api/src/main/resources/moduleApplicationContext.xml index 835c0e07..ee921b1a 100644 --- a/api/src/main/resources/moduleApplicationContext.xml +++ b/api/src/main/resources/moduleApplicationContext.xml @@ -71,6 +71,33 @@ + + + + + + + + + + + + + + + + + + + + + + org.openmrs.module.emrapi.maternal.MaternalService + + + + + diff --git a/api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java b/api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java new file mode 100644 index 00000000..269d1b2d --- /dev/null +++ b/api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java @@ -0,0 +1,408 @@ +package org.openmrs.module.emrapi.maternal; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +import org.joda.time.DateTime; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.Location; +import org.openmrs.Patient; +import org.openmrs.Relationship; +import org.openmrs.Visit; +import org.openmrs.api.PatientService; +import org.openmrs.api.PersonService; +import org.openmrs.api.VisitService; +import org.openmrs.contrib.testdata.TestDataManager; +import org.openmrs.module.emrapi.EmrApiContextSensitiveTest; +import org.openmrs.module.emrapi.EmrApiProperties; +import org.springframework.beans.factory.annotation.Autowired; + +public class MaternalServiceImplTest extends EmrApiContextSensitiveTest { + + @Autowired + private MaternalService maternalService; + + @Autowired + private PatientService patientService; + + @Autowired + private PersonService personService; + + @Autowired + private VisitService visitService; + + @Autowired + private EmrApiProperties emrApiProperties; + + @Autowired + private TestDataManager testDataManager; + + @Before + public void setUp() throws Exception { + executeDataSet("baseTestDataset.xml"); + } + + @Test + public void shouldGetNewbornByMother() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(1)); + assertThat(newborns.get(0).getNewborn(), equalTo(baby)); + assertThat(newborns.get(0).getNewbornVisit(), equalTo(babyVisit)); + } + + @Test + public void shouldNotGetNewbornByMotherIfMotherDoesNotHaveActiveVisit() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).stopped(now).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldNotGetNewbornByMotherIfNewbornDoesNotHaveActiveVisit() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).stopped(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldNotGetNewbornByMotherIfNewbornNotLinkedByMotherChildRelationship() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldNotGetNewbornByMotherIfNewbornBornADayOrMoreBeforeVisit() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + Date oneDayAgo = new DateTime().minusDays(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(oneDayAgo).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + // this test will fail when run *exactly* at midnight, on the second + @Test + public void shouldGetNewbornByMotherIfNewbornBornBeforeVisitStartButSameDay() throws Exception { + Date now = new Date(); + Date oneSecondAgo = new DateTime().minusSeconds(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(oneSecondAgo).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(now).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(),equalTo(1)); + assertThat(newborns.get(0).getNewborn(),equalTo(baby)); + assertThat(newborns.get(0).getNewbornVisit(),equalTo(babyVisit)); + } + + @Test + public void shouldGetMultipleNewbornByMother() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby1 = testDataManager.randomPatient().birthdate(now).save(); + Patient baby2= testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship1 = new Relationship(); + motherChildRelationship1.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship1.setPersonA(mother); + motherChildRelationship1.setPersonB(baby1); + personService.saveRelationship(motherChildRelationship1); + + Relationship motherChildRelationship2 = new Relationship(); + motherChildRelationship2.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship2.setPersonA(mother); + motherChildRelationship2.setPersonB(baby2); + personService.saveRelationship(motherChildRelationship2); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit baby1Visit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby1).started(now).save(); + Visit baby2Visit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby2).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(2)); + List babyList = newborns.stream().map(Newborn::getNewborn).collect(Collectors.toList()); + assertTrue(babyList.contains(baby1)); + assertTrue(babyList.contains(baby2)); + List babyVisitList = newborns.stream().map(Newborn::getNewbornVisit).collect(Collectors.toList()); + assertTrue(babyVisitList.contains(baby1Visit)); + assertTrue(babyVisitList.contains(baby2Visit)); + } + + @Test + public void shouldNotGetNewbornByMotherIfMotherVoided() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + patientService.voidPatient(mother, "test"); + + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldNotGetNewbornByMotherIfNewbornVoided() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + patientService.voidPatient(baby, "test"); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldNotGetNewbornByMotherIfRelationshipVoided() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + personService.voidRelationship(motherChildRelationship, "test"); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldNotGetNewbornByMotherIfMotherVisitVoided() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + visitService.voidVisit(motherVisit, "test"); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldNotGetNewbornByMotherIfBabyVisitVoided() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).save(); + visitService.voidVisit(babyVisit, "test"); + + List newborns = maternalService.getNewbornsByMother(mother, null); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldGetNewbornByMotherIfVisitLocationsMatchQueriedVisitLocation() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + Location location = testDataManager.location().name("Test Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).location(location).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).location(location).save(); + + List newborns = maternalService.getNewbornsByMother(mother, location); + + assertThat(newborns.size(), equalTo(1)); + assertThat(newborns.get(0).getNewborn(), equalTo(baby)); + assertThat(newborns.get(0).getNewbornVisit(), equalTo(babyVisit)); + } + + @Test + public void shouldGetNewbornByMotherIfMotherVisitLocationDoesNotMatchQueriedVisitLocation() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + Location location = testDataManager.location().name("Test Location").save(); + Location anotherLocation = testDataManager.location().name("Another Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).location(anotherLocation).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).location(location).save(); + + List newborns = maternalService.getNewbornsByMother(mother, location); + + assertThat(newborns.size(), equalTo(0)); + } + + @Test + public void shouldGetNewbornByMotherIfBabyVisitLocationDoesNotMatchQueriedVisitLocation() throws Exception { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + Location location = testDataManager.location().name("Test Location").save(); + Location anotherLocation = testDataManager.location().name("Another Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient baby = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(baby); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(mother).started(oneHourAgo).location(location).save(); + Visit babyVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).patient(baby).started(now).location(anotherLocation).save(); + + List newborns = maternalService.getNewbornsByMother(mother, location); + + assertThat(newborns.size(), equalTo(0)); + } +} diff --git a/api/src/test/resources/baseTestDataset.xml b/api/src/test/resources/baseTestDataset.xml index 5178d0e7..277b72b1 100644 --- a/api/src/test/resources/baseTestDataset.xml +++ b/api/src/test/resources/baseTestDataset.xml @@ -107,7 +107,7 @@ + uuid="2561da10-2229-11e2-9b1a-423440a3714c"/>emrapiProp + + + +