ChildService { public void pncOAChildRegistration(FormSubmission submission) { List<Mother> mothers = allMothers.findByEcCaseId(submission.entityId()); if (mothers.size() <= 0) { logger.warn("Failed to handle PNC OA children registration as there is no mother registered with id: " + submission.entityId()); return; } Mother mother = mothers.get(0); allMothers.update(mother.withAnm(submission.anmId())); SubFormData subFormData = submission.getSubFormByName(AllConstants.Form.PNC_REGISTRATION_OA_SUB_FORM_NAME); if (handleStillBirth(submission, subFormData)) return; String referenceDate = submission.getField(REFERENCE_DATE); for (Map<String, String> childFields : subFormData.instances()) { Child child = allChildren.findByCaseId(childFields.get(ID)); List<String> immunizationsGiven = getNamesOfImmunizationsGiven(childFields.get(IMMUNIZATIONS_GIVEN_FIELD_NAME)); child = child.withAnm(submission.anmId()) .withDateOfBirth(referenceDate) .withThayiCard(mother.thayiCardNumber()) .setIsClosed(false) .withImmunizations(getChildImmunizationDetails(submission, child, immunizationsGiven)); allChildren.update(child); SafeMap reportingData = new SafeMap(); reportingData.put(ChildReportingService.CHILD_ID_FIELD, child.caseId()); reportingData.put(DELIVERY_PLACE, submission.getField(DELIVERY_PLACE)); reportingData.put(BF_POSTBIRTH, childFields.get(DID_BREAST_FEEDING_START)); reportingData.put(REGISTRATION_DATE, referenceDate); reportingData.put(SUBMISSION_DATE_FIELD_NAME, submission.getField(SUBMISSION_DATE_FIELD_NAME)); childReportingService.registerChild(reportingData); childSchedulesService.enrollChild(child); } } @Autowired  ChildService(ChildSchedulesService childSchedulesService,
                        AllMothers allMothers,
                        AllChildren allChildren,
                        ChildReportingService childReportingService, ActionService actionService, ReportFieldsDefinition reportFieldsDefinition); void registerChildren(FormSubmission submission); void registerChildrenForEC(FormSubmission submission); void registerChildrenForOA(FormSubmission submission); void updateChildImmunization(FormSubmission submission); void vitaminAProvided(FormSubmission submission); void closeChild(FormSubmission submission); void pncOAChildRegistration(FormSubmission submission); void pncVisitHappened(FormSubmission submission); void sickVisitHappened(FormSubmission submission); static final String IMMUNIZATIONS_SEPARATOR; }
@Test public void shouldUpdateANMIdOnMotherWhenOAPNCIsRegistered() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); Mother mother = new Mother("mother id 1", "ec id 1", "TC1"); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(mother)); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .withSubForm(new SubFormData("child_registration_oa", Collections.<Map<String, String>>emptyList())) .build(); service.pncOAChildRegistration(submission); verify(allMothers).update(mother.withAnm("anm id 1")); }
@Test public void shouldEnrollEveryChildIntoSchedulesDuringPNCRegistration() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "ec id 1", "TC1"))); Child firstChild = new Child("child id 1", "mother id 1", "opv", "2", "female"); Child secondChild = new Child("child id 2", "mother id 1", "opv", "2", "male"); when(allChildren.findByCaseId("child id 1")).thenReturn(firstChild); when(allChildren.findByCaseId("child id 2")).thenReturn(secondChild); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("deliveryPlace", "phc") .addFormField("registrationDate", "2012-01-02") .addFormField("submissionDate", "2012-01-02") .withSubForm(new SubFormData("child_registration_oa", asList(EasyMap.create("id", "child id 1").put("didBreastfeedingStart", "no").map(), EasyMap.create("id", "child id 2").put("didBreastfeedingStart", "yes").map())) ) .build(); service.pncOAChildRegistration(submission); InOrder inOrder = inOrder(childReportingService, childSchedulesService); inOrder.verify(childReportingService).registerChild(new SafeMap(create("didBreastfeedingStart", "no") .put("childId", "child id 1") .put("deliveryPlace", "phc") .put("registrationDate", "2012-01-01") .put("submissionDate", "2012-01-02") .map())); inOrder.verify(childSchedulesService).enrollChild(firstChild.withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1")); inOrder.verify(childReportingService).registerChild(new SafeMap(create("didBreastfeedingStart", "yes") .put("childId", "child id 2") .put("deliveryPlace", "phc") .put("registrationDate", "2012-01-01") .put("submissionDate", "2012-01-02") .map())); inOrder.verify(childSchedulesService).enrollChild(secondChild.withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1")); }
@Test public void shouldUpdateEveryChildWithMotherInfoDuringPNCgRegistration() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "ec id 1", "TC1"))); Child firstChild = new Child("child id 1", "mother id 1", "opv", "2", "female"); Child secondChild = new Child("child id 2", "mother id 1", "opv", "2", "male"); when(allChildren.findByCaseId("child id 1")).thenReturn(firstChild); when(allChildren.findByCaseId("child id 2")).thenReturn(secondChild); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .withSubForm(new SubFormData("child_registration_oa", asList(mapOf("id", "child id 1"), mapOf("id", "child id 2")))) .build(); service.pncOAChildRegistration(submission); verify(allChildren).update(firstChild.withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1")); verify(allChildren).update(secondChild.withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1")); assertFalse(firstChild.isClosed()); assertFalse(secondChild.isClosed()); }
@Test public void shouldUpdateEveryChildWithImmunizationInfoDuringPNCOARegistration() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "ec id 1", "TC1"))); Child firstChild = new Child("child id 1", "mother id 1", "opv", "2", "female"); Child secondChild = new Child("child id 2", "mother id 1", "opv", "2", "male"); when(allChildren.findByCaseId("child id 1")).thenReturn(firstChild); when(allChildren.findByCaseId("child id 2")).thenReturn(secondChild); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("submissionDate", "2012-01-01") .withSubForm(new SubFormData("child_registration_oa", asList(create("id", "child id 1").put("immunizationsGiven", "bcg").map(), mapOf("id", "child id 2")))) .build(); service.pncOAChildRegistration(submission); verify(allChildren).update(new Child("child id 1", "mother id 1", "opv", "2", "female").withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1") .withImmunizations(mapOf("bcg", "2012-01-01"))); }
@Test public void shouldNotHandlePNCChildRegistrationWhenMotherIsNotFound() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(Collections.EMPTY_LIST); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .build(); service.pncOAChildRegistration(submission); verifyZeroInteractions(allChildren); verifyZeroInteractions(childSchedulesService); }
@Test public void shouldDeleteChildRegisteredThroughPNCRegistrationFormOAWhenDeliveryOutcomeIsStillBirth() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("deliveryOutcome", "still_birth") .withSubForm(new SubFormData("child_registration_oa", asList(mapOf("id", "child id 1")))) .build(); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "EC-CASE-1", "TC1"))); service.pncOAChildRegistration(submission); verify(allChildren).remove("child id 1"); verifyZeroInteractions(childReportingService); verifyZeroInteractions(childSchedulesService); }
@Test public void shouldNotFailTryingToDeleteChildWhenPNCRegistrationOAIsHandledAndDeliveryOutcomeIsStillBirthAndThereIsNoChild() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("deliveryOutcome", "still_birth") .withSubForm(new SubFormData("child_registration_oa", Collections.<Map<String, String>>emptyList())) .build(); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "EC-CASE-1", "TC1"))); service.pncOAChildRegistration(submission); verifyZeroInteractions(allChildren); verifyZeroInteractions(childReportingService); verifyZeroInteractions(childSchedulesService); }