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/Child.java b/api/src/main/java/org/openmrs/module/emrapi/maternal/Child.java new file mode 100644 index 00000000..5ae0f25f --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/Child.java @@ -0,0 +1,12 @@ +package org.openmrs.module.emrapi.maternal; + +import lombok.Data; +import org.openmrs.Patient; +import org.openmrs.module.emrapi.adt.InpatientAdmission; + +@Data +public class Child { + private Patient child; + private Patient mother; + private InpatientAdmission childAdmission; +} diff --git a/api/src/main/java/org/openmrs/module/emrapi/maternal/ChildrenByMothersSearchCriteria.java b/api/src/main/java/org/openmrs/module/emrapi/maternal/ChildrenByMothersSearchCriteria.java new file mode 100644 index 00000000..fb746430 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/ChildrenByMothersSearchCriteria.java @@ -0,0 +1,20 @@ +package org.openmrs.module.emrapi.maternal; + +import java.util.List; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ChildrenByMothersSearchCriteria { + private List motherUuids; // restrict to children of these mothers + @Accessors(fluent = true) private boolean requireMotherHasActiveVisit = false; // restrict to children of mothers who have an active visit + @Accessors(fluent = true) private boolean requireChildHasActiveVisit = false; // restrict to children who have an active visit + @Accessors(fluent = true) private boolean requireChildBornDuringMothersActiveVisit = false; // restrict to children who were born during their mother's active visit +} + + 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..92f90826 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalService.java @@ -0,0 +1,22 @@ +package org.openmrs.module.emrapi.maternal; + +import java.util.List; + +import org.openmrs.api.OpenmrsService; + +public interface MaternalService extends OpenmrsService { + + /** + * Fetches patients who are "children" (personB) of a Mother-Child relationship + * @param criteria search criteria (see class for details) + * @return a list of children, with their linked mothers (note this returns a "Child" per mother-child pair, so a child with multiple mothers will appear multiple times) + */ + List getChildrenByMothers(ChildrenByMothersSearchCriteria criteria); + + /** + * Fetches patients who are "mothers" (personA) of a Mother-Child relationship + * @param criteria search criteria (see class for details) + * @return a list of mothers, with their linked children (note this returns a "Mother" per mother-child pair, so a mother with multiple children will appear multiple times) + */ + List getMothersByChildren(MothersByChildrenSearchCriteria criteria); +} 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..18003453 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java @@ -0,0 +1,124 @@ +package org.openmrs.module.emrapi.maternal; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.openmrs.Patient; +import org.openmrs.RelationshipType; +import org.openmrs.api.APIException; +import org.openmrs.api.impl.BaseOpenmrsService; +import org.openmrs.module.emrapi.EmrApiProperties; +import org.openmrs.module.emrapi.adt.AdtService; +import org.openmrs.module.emrapi.adt.InpatientAdmission; +import org.openmrs.module.emrapi.adt.InpatientAdmissionSearchCriteria; +import org.openmrs.module.emrapi.db.EmrApiDAO; + +public class MaternalServiceImpl extends BaseOpenmrsService implements MaternalService { + + private EmrApiProperties emrApiProperties; + + private AdtService adtService; + + private EmrApiDAO emrApiDAO; + + public void setEmrApiProperties(EmrApiProperties emrApiProperties) { + this.emrApiProperties = emrApiProperties; + } + + public void setEmrApiDAO(EmrApiDAO emrApiDAO) { + this.emrApiDAO = emrApiDAO; + } + + public void setAdtService(AdtService adtService) { + this.adtService = adtService; + } + + public List getChildrenByMothers(ChildrenByMothersSearchCriteria criteria) { + + RelationshipType motherChildRelationshipType = emrApiProperties.getMotherChildRelationshipType(); + + if (motherChildRelationshipType == null) { + throw new APIException("Mother-Child relationship type has not been configured"); + } + + Map parameters = new HashMap<>(); + parameters.put("motherUuids", criteria.getMotherUuids()); + parameters.put("childUuids", null); + parameters.put("motherChildRelationshipType", motherChildRelationshipType); + parameters.put("requireMotherHasActiveVisit", criteria.requireMotherHasActiveVisit()); + parameters.put("requireChildHasActiveVisit", criteria.requireChildHasActiveVisit()); + parameters.put("requireChildBornDuringMothersActiveVisit", criteria.requireChildBornDuringMothersActiveVisit()); + + List l = emrApiDAO.executeHqlFromResource("hql/mother_child.hql", parameters, List.class); + + List ret = new ArrayList<>(); + + for (Object req : l) { + Object[] row = (Object[]) req; + Child child = new Child(); + child.setMother((Patient) row[0]); + child.setChild((Patient) row[1]); + ret.add(child); + } + + // now fetch all the admissions for children in the result set + InpatientAdmissionSearchCriteria inpatientAdmissionSearchCriteria = new InpatientAdmissionSearchCriteria(); + inpatientAdmissionSearchCriteria.setPatientIds(new ArrayList<>(ret.stream().map(Child::getChild).map(Patient::getId).collect(Collectors.toSet()))); + List admissions = adtService.getInpatientAdmissions(inpatientAdmissionSearchCriteria); + Map admissionsByPatient = new HashMap<>(); + for (InpatientAdmission admission : admissions) { + admissionsByPatient.put(admission.getVisit().getPatient(), admission); + } + for (Child child : ret) { + child.setChildAdmission(admissionsByPatient.get(child.getChild())); + } + + return ret; + } + + public List getMothersByChildren(MothersByChildrenSearchCriteria criteria) { + RelationshipType motherChildRelationshipType = emrApiProperties.getMotherChildRelationshipType(); + + if (motherChildRelationshipType == null) { + throw new APIException("Mother-Child relationship type has not been configured"); + } + + Map parameters = new HashMap<>(); + parameters.put("motherUuids", null); + parameters.put("childUuids", criteria.getChildUuids()); + parameters.put("motherChildRelationshipType", motherChildRelationshipType); + parameters.put("requireMotherHasActiveVisit", criteria.requireMotherHasActiveVisit()); + parameters.put("requireChildHasActiveVisit", criteria.requireChildHasActiveVisit()); + parameters.put("requireChildBornDuringMothersActiveVisit", criteria.requireChildBornDuringMothersActiveVisit()); + + List l = emrApiDAO.executeHqlFromResource("hql/mother_child.hql", parameters, List.class); + + List ret = new ArrayList<>(); + + for (Object req : l) { + Object[] row = (Object[]) req; + Mother mother = new Mother(); + mother.setMother((Patient) row[0]); + mother.setChild((Patient) row[1]); + ret.add(mother); + } + + // now fetch all the admissions for mothers in the result set + InpatientAdmissionSearchCriteria inpatientAdmissionSearchCriteria = new InpatientAdmissionSearchCriteria(); + inpatientAdmissionSearchCriteria.setPatientIds(new ArrayList<>(ret.stream().map(Mother::getMother).map(Patient::getId).collect(Collectors.toSet()))); + List admissions = adtService.getInpatientAdmissions(inpatientAdmissionSearchCriteria); + Map admissionsByPatient = new HashMap<>(); + for (InpatientAdmission admission : admissions) { + admissionsByPatient.put(admission.getVisit().getPatient(), admission); + } + for (Mother mother : ret) { + mother.setMotherAdmission(admissionsByPatient.get(mother.getMother())); + } + + + return ret; + } +} diff --git a/api/src/main/java/org/openmrs/module/emrapi/maternal/Mother.java b/api/src/main/java/org/openmrs/module/emrapi/maternal/Mother.java new file mode 100644 index 00000000..689bcabb --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/Mother.java @@ -0,0 +1,13 @@ +package org.openmrs.module.emrapi.maternal; + +import lombok.Data; +import org.openmrs.Patient; +import org.openmrs.module.emrapi.adt.InpatientAdmission; + + +@Data +public class Mother { + private Patient mother; + private Patient child; + private InpatientAdmission motherAdmission; +} diff --git a/api/src/main/java/org/openmrs/module/emrapi/maternal/MothersByChildrenSearchCriteria.java b/api/src/main/java/org/openmrs/module/emrapi/maternal/MothersByChildrenSearchCriteria.java new file mode 100644 index 00000000..210f600f --- /dev/null +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/MothersByChildrenSearchCriteria.java @@ -0,0 +1,19 @@ +package org.openmrs.module.emrapi.maternal; + +import java.util.List; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class MothersByChildrenSearchCriteria { + private List childUuids; // restrict to mothers of these children + @Accessors(fluent = true) private Boolean requireMotherHasActiveVisit = false; // restrict to mothers who have an active visit + @Accessors(fluent = true) private Boolean requireChildHasActiveVisit = false; // restrict to mothers of children who have an active visit + @Accessors(fluent = true) private boolean requireChildBornDuringMothersActiveVisit = false; // restrict to mothers who had a child born during their active visit + +} diff --git a/api/src/main/resources/hql/mother_child.hql b/api/src/main/resources/hql/mother_child.hql new file mode 100644 index 00000000..05871a2a --- /dev/null +++ b/api/src/main/resources/hql/mother_child.hql @@ -0,0 +1,18 @@ +select + mother, + child +from + Relationship as motherChildRelationship + inner join motherChildRelationship.personA as mother + inner join motherChildRelationship.personB as child +where + ((:motherUuids) is null or mother.uuid in (:motherUuids)) + and ((:childUuids) is null or child.uuid in (:childUuids)) + and motherChildRelationship.relationshipType = :motherChildRelationshipType + and (:requireMotherHasActiveVisit = false or (select count(motherVisit) from Visit as motherVisit where motherVisit.patient = mother and motherVisit.stopDatetime is null and motherVisit.voided = false) > 0) + and (:requireChildHasActiveVisit = false or (select count(childVisit) from Visit as childVisit where childVisit.patient = child and childVisit.stopDatetime is null and childVisit.voided = false) > 0) + and (:requireChildBornDuringMothersActiveVisit = false or (select count(motherVisit) from Visit as motherVisit where motherVisit.patient = mother and motherVisit.stopDatetime is null and motherVisit.voided = false + and year(child.birthdate) >= year(motherVisit.startDatetime) + and month(child.birthdate) >= month(motherVisit.startDatetime) + and day(child.birthdate) >= day(motherVisit.startDatetime)) > 0) + and mother.voided = false and child.voided = false and motherChildRelationship.voided = false diff --git a/api/src/main/resources/moduleApplicationContext.xml b/api/src/main/resources/moduleApplicationContext.xml index 835c0e07..e26bf59d 100644 --- a/api/src/main/resources/moduleApplicationContext.xml +++ b/api/src/main/resources/moduleApplicationContext.xml @@ -71,6 +71,34 @@ + + + + + + + + + + + + + + + + + + + + + + + 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..035599d5 --- /dev/null +++ b/api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java @@ -0,0 +1,788 @@ +package org.openmrs.module.emrapi.maternal; + +import static org.hamcrest.CoreMatchers.equalTo; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +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.Encounter; +import org.openmrs.Location; +import org.openmrs.Patient; +import org.openmrs.Relationship; +import org.openmrs.Visit; +import org.openmrs.api.ConceptService; +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.openmrs.module.emrapi.disposition.DispositionService; +import org.openmrs.module.emrapi.test.ContextSensitiveMetadataTestUtils; +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 ConceptService conceptService; + + @Autowired + private DispositionService dispositionService; + + @Autowired + private EmrApiProperties emrApiProperties; + + @Autowired + private TestDataManager testDataManager; + + + @Before + public void setUp() { + executeDataSet("baseTestDataset.xml"); + ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService); + ContextSensitiveMetadataTestUtils.setupAdmissionDecisionConcept(conceptService, emrApiProperties); + } + + + @Test + public void shouldGetChild() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(null, false, false, false)); + + assertThat(children.size(), equalTo(1)); + assertThat(children.get(0).getChild(), equalTo(child)); + assertThat(children.get(0).getMother(), equalTo(mother)); + assertNull(children.get(0).getChildAdmission()); + } + @Test + public void shouldGetChildByMother() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, false)); + + assertThat(children.size(), equalTo(1)); + assertThat(children.get(0).getChild(), equalTo(child)); + assertThat(children.get(0).getMother(), equalTo(mother)); + assertNull(children.get(0).getChildAdmission()); + } + + @Test + public void shouldNotGetChildByMotherIfMotherDoesNotHaveActiveVisitAndMotherHasActiveVisitSetTrue() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).stopped(now).save(); + Visit childVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).save(); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), true, false, false)); + + assertThat(children.size(), equalTo(0)); + } + + @Test + public void shouldNotGetChildByMotherIfChildDoesNotHaveActiveVisitAndChildHasActiveVisitSetTrue() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).save(); + Visit childVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).stopped(now).save(); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, true, false)); + + assertThat(children.size(), equalTo(0)); + } + + @Test + public void shouldNotGetChildByMotherIfChildNotLinkedByMotherChildRelationship() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, false)); + + assertThat(children.size(), equalTo(0)); + } + + @Test + public void shouldNotGetChildByMotherIfChildBornADayOrMoreBeforeVisitAndChildBornDuringMothersActiveVisitSetTrue() { + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + Date oneDayAgo = new DateTime().minusDays(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(oneDayAgo).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).save(); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, true)); + + assertThat(children.size(), equalTo(0)); + } + + // this test will fail when run *exactly* at midnight, on the second + @Test + public void shouldGetChildByMotherIfChildBornBeforeVisitStartButSameDayAndChildBornDuringMothersActiveVisitSetTrue() { + Date now = new Date(); + Date oneSecondAgo = new DateTime().minusSeconds(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(oneSecondAgo).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(now).save(); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, true)); + + assertThat(children.size(),equalTo(1)); + assertThat(children.get(0).getChild(),equalTo(child)); + assertThat(children.get(0).getMother(), equalTo(mother)); + assertNull(children.get(0).getChildAdmission()); + } + + @Test + public void shouldGetMultipleChildByMother() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child1 = testDataManager.randomPatient().birthdate(now).save(); + Patient child2= testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship1 = new Relationship(); + motherChildRelationship1.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship1.setPersonA(mother); + motherChildRelationship1.setPersonB(child1); + personService.saveRelationship(motherChildRelationship1); + + Relationship motherChildRelationship2 = new Relationship(); + motherChildRelationship2.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship2.setPersonA(mother); + motherChildRelationship2.setPersonB(child2); + personService.saveRelationship(motherChildRelationship2); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, false)); + + assertThat(children.size(), equalTo(2)); + List childList = children.stream().map(Child::getChild).collect(Collectors.toList()); + assertTrue(childList.contains(child1)); + assertTrue(childList.contains(child2)); + + assertThat(children.get(0).getMother(), equalTo(mother)); + assertThat(children.get(1).getMother(), equalTo(mother)); + + assertNull(children.get(0).getChildAdmission()); + assertNull(children.get(1).getChildAdmission()); + } + + @Test + public void shouldNotGetChildByMotherIfMotherVoided() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + patientService.voidPatient(mother, "test"); + + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, false)); + + assertThat(children.size(), equalTo(0)); + } + + @Test + public void shouldNotGetChildByMotherIfChildVoided() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + patientService.voidPatient(child, "test"); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, false)); + + assertThat(children.size(), equalTo(0)); + } + + @Test + public void shouldNotGetChildByMotherIfRelationshipVoided() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + personService.voidRelationship(motherChildRelationship, "test"); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, false)); + + assertThat(children.size(), equalTo(0)); + } + + @Test + public void shouldNotGetChildByMotherIfMotherVisitVoidedAndMotherHasActiveVisitSetTrue() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).save(); + visitService.voidVisit(motherVisit, "test"); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), true, false, false)); + + assertThat(children.size(), equalTo(0)); + } + + @Test + public void shouldNotGetChildByMotherIfChildVisitVoidedAndChildHasActiveVisitSetTrue() { + Date now = new Date(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit childVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).save(); + visitService.voidVisit(childVisit, "test"); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, true, false)); + + assertThat(children.size(), equalTo(0)); + } + + @Test + public void shouldGetChildrenForMultipleMothers() { + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate("2000-01-01").save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Patient otherMother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient otherChild = testDataManager.randomPatient().birthdate("2000-01-01").save(); + + Relationship otherMotherChildRelationship = new Relationship(); + otherMotherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + otherMotherChildRelationship.setPersonA(otherMother); + otherMotherChildRelationship.setPersonB(otherChild); + personService.saveRelationship(otherMotherChildRelationship); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Arrays.asList(mother.getUuid(),otherMother.getUuid()), false, false, false)); + + assertThat(children.size(), equalTo(2)); + List childList = children.stream().map(Child::getChild).collect(Collectors.toList()); + assertTrue(childList.contains(child)); + assertTrue(childList.contains(otherChild)); + + List motherList = children.stream().map(Child::getMother).collect(Collectors.toList()); + assertTrue(motherList.contains(mother)); + assertTrue(motherList.contains(otherMother)); + } + + @Test + public void shouldGetChildrenForMultipleMothersMatchingByActiveVisitsAndChildBornDuringActiveVisit() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).save(); + Visit childVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).save(); + + Patient otherMother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient otherChild = testDataManager.randomPatient().birthdate(now).save(); + + Relationship otherMotherChildRelationship = new Relationship(); + otherMotherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + otherMotherChildRelationship.setPersonA(otherMother); + otherMotherChildRelationship.setPersonB(otherChild); + personService.saveRelationship(otherMotherChildRelationship); + + Visit otherMotherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(otherMother).started(oneHourAgo).save(); + Visit otherChildVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(otherChild).started(now).save(); + + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Arrays.asList(mother.getUuid(),otherMother.getUuid()), true, true, true)); + + assertThat(children.size(), equalTo(2)); + List childList = children.stream().map(Child::getChild).collect(Collectors.toList()); + assertTrue(childList.contains(child)); + assertTrue(childList.contains(otherChild)); + + List motherList = children.stream().map(Child::getMother).collect(Collectors.toList()); + assertTrue(motherList.contains(mother)); + assertTrue(motherList.contains(otherMother)); + } + + @Test + public void shouldGetChildByMotherWithInpatientAdmission() { + Date now = new Date(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + // admit the child + Encounter admission = testDataManager.encounter().encounterType(emrApiProperties.getAdmissionEncounterType()).encounterDatetime(now).patient(child).save(); + Visit childVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).encounter(admission).save(); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), false, false, false)); + + assertThat(children.size(), equalTo(1)); + assertThat(children.get(0).getChildAdmission().getVisit(), equalTo(childVisit)); + assertThat(children.get(0).getChildAdmission().getFirstAdmissionOrTransferEncounter(), equalTo(admission)); + } + + @Test + public void getChildByMotherDoesNotFailWhenChildHaveTwoActiveVisits() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).save(); + Visit childVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).save(); + Visit anotherChildVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).save(); + + List children = maternalService.getChildrenByMothers(new ChildrenByMothersSearchCriteria(Collections.singletonList(mother.getUuid()), true, true, true)); + + assertThat(children.size(), equalTo(1)); + } + + @Test + public void shouldGetMother() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(null, false, false, false)); + + assertThat(mothers.size(), equalTo(1)); + assertThat(mothers.get(0).getChild(), equalTo(child)); + assertThat(mothers.get(0).getMother(), equalTo(mother)); + assertNull(mothers.get(0).getMotherAdmission()); + } + + @Test + public void shouldGetMotherByChild() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), false, false, false)); + + assertThat(mothers.size(), equalTo(1)); + assertThat(mothers.get(0).getChild(), equalTo(child)); + assertThat(mothers.get(0).getMother(), equalTo(mother)); + assertNull(mothers.get(0).getMotherAdmission()); + } + + @Test + public void shouldNotGetMotherByChildIfMotherDoesNotHaveActiveVisitAndMotherHasActiveVisitSetTrue() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).stopped(now).save(); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), true, false, false)); + + assertThat(mothers.size(), equalTo(0)); + } + + @Test + public void shouldNotGetMotherByChildIfChildDoesNotHaveActiveVisitAndChildHasActiveVisitSetTrue() { + Date now = new Date(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit childVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).stopped(now).save(); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), false, true,false)); + + assertThat(mothers.size(), equalTo(0)); + } + + @Test + public void shouldNotGetMotherByChildIfChildNotLinkedByMotherChildRelationship() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), false, false, false)); + + assertThat(mothers.size(), equalTo(0)); + } + + @Test + public void shouldNotGetMotherByChildIfMotherVoided() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + patientService.voidPatient(mother, "test"); + + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), false, false, false)); + + assertThat(mothers.size(), equalTo(0)); + } + + @Test + public void shouldNotGetMotherByChildIfChildVoided() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + patientService.voidPatient(child, "test"); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), false, false, false)); + + assertThat(mothers.size(), equalTo(0)); + } + + @Test + public void shouldNotGetMotherByChildIfRelationshipVoided() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + personService.voidRelationship(motherChildRelationship, "test"); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), false, false,false)); + + assertThat(mothers.size(), equalTo(0)); + } + + @Test + public void shouldNotGetMotherByChildIfMotherVisitVoidedAndMotherHasActiveVisitSetTrue() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).save(); + visitService.voidVisit(motherVisit, "test"); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), true, false, false)); + + assertThat(mothers.size(), equalTo(0)); + } + + @Test + public void shouldNotGetMotherByChildIfChildVisitVoidedAndChildHasActiveVisitSetTrue() { + Date now = new Date(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit childVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(child).started(now).save(); + visitService.voidVisit(childVisit, "test"); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), false, true, false)); + + assertThat(mothers.size(), equalTo(0)); + } + + @Test + public void shouldGetMothersForMultipleChildren() { + Date now = new Date(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Patient otherMother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient otherChild = testDataManager.randomPatient().birthdate(now).save(); + + Relationship otherMotherChildRelationship = new Relationship(); + otherMotherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + otherMotherChildRelationship.setPersonA(otherMother); + otherMotherChildRelationship.setPersonB(otherChild); + personService.saveRelationship(otherMotherChildRelationship); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Arrays.asList(child.getUuid(),otherChild.getUuid()), false, false, false)); + + assertThat(mothers.size(), equalTo(2)); + List childList = mothers.stream().map(Mother::getChild).collect(Collectors.toList()); + assertTrue(childList.contains(child)); + assertTrue(childList.contains(otherChild)); + + List motherList = mothers.stream().map(Mother::getMother).collect(Collectors.toList()); + assertTrue(motherList.contains(mother)); + assertTrue(motherList.contains(otherMother)); + } + + @Test + public void shouldGetMotherByChildWithInpatientAdmission() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + // admit the mother + Encounter admission = testDataManager.encounter().encounterType(emrApiProperties.getAdmissionEncounterType()).encounterDatetime(now).patient(mother).save(); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).encounter(admission).save(); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(Collections.singletonList(child.getUuid()), false, false, false)); + + assertThat(mothers.size(), equalTo(1)); + assertThat(mothers.get(0).getMotherAdmission().getVisit(), equalTo(motherVisit)); + assertThat(mothers.get(0).getMotherAdmission().getFirstAdmissionOrTransferEncounter(), equalTo(admission)); + } + + @Test + public void shouldOnlyGetMothersWithChildrenBornDuringActiveVisit() { + Date now = new Date(); + Date oneHourAgo = new DateTime().minusHours(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(now).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Patient otherMother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient otherChild = testDataManager.randomPatient().birthdate("2020-01-01").save(); // not during active visit + + Relationship otherMotherChildRelationship = new Relationship(); + otherMotherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + otherMotherChildRelationship.setPersonA(otherMother); + otherMotherChildRelationship.setPersonB(otherChild); + personService.saveRelationship(otherMotherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(oneHourAgo).save(); + Visit otherMotherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(otherMother).started(oneHourAgo).save(); + + List mothers = maternalService.getMothersByChildren(new MothersByChildrenSearchCriteria(null, false, false, true)); + + assertThat(mothers.size(), equalTo(1)); + assertThat(mothers.get(0).getChild(), equalTo(child)); + assertThat(mothers.get(0).getMother(), equalTo(mother)); + assertNull(mothers.get(0).getMotherAdmission()); + } + +} diff --git a/api/src/test/resources/baseTestDataset.xml b/api/src/test/resources/baseTestDataset.xml index 5178d0e7..a4ea690b 100644 --- a/api/src/test/resources/baseTestDataset.xml +++ b/api/src/test/resources/baseTestDataset.xml @@ -137,4 +137,11 @@ + + + + diff --git a/omod/src/test/java/org/openmrs/module/emrapi/web/controller/EmrApiConfigurationControllerTest.java b/omod/src/test/java/org/openmrs/module/emrapi/web/controller/EmrApiConfigurationControllerTest.java index 03d90a9f..d3d6f916 100644 --- a/omod/src/test/java/org/openmrs/module/emrapi/web/controller/EmrApiConfigurationControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/emrapi/web/controller/EmrApiConfigurationControllerTest.java @@ -66,7 +66,7 @@ public void shouldGetAsJson() throws Exception { @Test public void shouldGetDefaultRepresentation() { SimpleObject config = emrApiConfigurationController.getEmrApiConfiguration(request, response); - assertEquals(47, config.keySet().size()); + assertEquals(48, config.keySet().size()); assertEquals("org.openmrs.module.emrapi", config.get("metadataSourceName")); assertEquals("50", config.get("lastViewedPatientSizeLimit").toString()); Map unknownLocation = mapNode(config, "unknownLocation");