Permalink
Browse files

Balaji | merging conflicts

  • Loading branch information...
balaji committed Apr 30, 2012
2 parents 95ecae8 + f0ea24a commit 38003fb90dc75468868c0b827ff526af4f46c2ee
@@ -102,4 +102,9 @@ public EnrollmentRecord enrollment(EnrollmentRequest enrollmentRequest) {
public EnrollmentRecord getActiveEnrollment(String externalId, String scheduleName) {
return scheduleTrackingService.getEnrollment(externalId, scheduleName);
}
+
+ public List<DateTime> getDueWindowAlertTimings(EnrollmentRequest enrollmentRequest){
+ return scheduleTrackingService.getAlertTimings(enrollmentRequest).getDueWindowAlertTimings();
+ }
+
}
@@ -2,6 +2,7 @@
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
+import org.joda.time.Period;
import org.motechproject.ghana.national.domain.*;
import org.motechproject.ghana.national.domain.care.*;
import org.motechproject.ghana.national.mapper.ScheduleEnrollmentMapper;
@@ -10,10 +11,12 @@
import org.motechproject.ghana.national.repository.AllPatients;
import org.motechproject.ghana.national.repository.AllSchedules;
import org.motechproject.ghana.national.vo.*;
+import org.motechproject.model.Time;
import org.motechproject.mrs.exception.ObservationNotFoundException;
import org.motechproject.mrs.model.MRSConcept;
import org.motechproject.mrs.model.MRSEncounter;
import org.motechproject.mrs.model.MRSObservation;
+import org.motechproject.scheduletracking.api.service.EnrollmentRequest;
import org.motechproject.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -23,8 +26,7 @@
import static org.motechproject.ghana.national.configuration.ScheduleNames.*;
import static org.motechproject.ghana.national.domain.Concept.*;
import static org.motechproject.ghana.national.domain.EncounterType.*;
-import static org.motechproject.ghana.national.tools.Utility.safeParseDouble;
-import static org.motechproject.ghana.national.tools.Utility.safeToString;
+import static org.motechproject.ghana.national.tools.Utility.*;
import static org.motechproject.util.DateUtil.newDate;
@Service
@@ -58,11 +60,12 @@ void enrollToCWCCarePrograms(CwcVO cwcVO, Patient patient) {
final List<CwcCareHistory> mergedHistories = mergeNewHistoriesWithExisting(existingHistories, cwcVO.getCWCCareHistoryVO().getCwcCareHistories());
List<PatientCare> patientCares = patient.cwcCareProgramToEnrollOnRegistration(newDate(cwcVO.getRegistrationDate()),
- mergedHistories, cwcVO.getCWCCareHistoryVO(), activeCareSchedules(patient, Arrays.asList(CWC_PENTA,CWC_IPT_VACCINE,CWC_OPV_OTHERS)));
+ mergedHistories, cwcVO.getCWCCareHistoryVO(), activeCareSchedules(patient, Arrays.asList(CWC_PENTA, CWC_IPT_VACCINE, CWC_OPV_OTHERS)));
for (PatientCare patientCare : patientCares) {
allSchedules.enroll(new ScheduleEnrollmentMapper().map(patient, patientCare));
}
+ enrollChildForPNC(patient);
}
List<CwcCareHistory> mergeNewHistoriesWithExisting(List<MRSObservation> existingHistory, List<CwcCareHistory> newHistory) {
@@ -82,8 +85,7 @@ void enrollToCWCCarePrograms(CwcVO cwcVO, Patient patient) {
String conceptName = null;
if (mrsObservation.getValue() instanceof MRSConcept) {
conceptName = ((MRSConcept) mrsObservation.getValue()).getName();
- }
- else{
+ } else {
conceptName = mrsObservation.getConceptName();
}
if (conceptToCareHistory.containsKey(conceptName)) {
@@ -103,15 +105,38 @@ public void enroll(ANCVO ancVO) throws ObservationNotFoundException {
allEncounters.persistEncounter(patient.getMrsPatient(), ancVO.getStaffId(), ancVO.getFacilityId(), ANC_REG_VISIT.value(), ancVO.getRegistrationDate(), prepareObservations(ancVO));
allEncounters.persistEncounter(patient.getMrsPatient(), ancVO.getStaffId(), ancVO.getFacilityId(), PREG_REG_VISIT.value(), ancVO.getRegistrationDate(), pregnancyObservations);
+
ActiveCareSchedules activeCareSchedules = activeCareSchedules(patient, Arrays.asList(TT_VACCINATION, ANC_IPT_VACCINE));
- TTVaccineCare ttVaccineCare = new TTVaccineCare(patient, expectedDeliveryDate, newDate(ancVO.getRegistrationDate()), activeCareSchedules.hasActiveTTSchedule(), ancCareHistoryVO.getLastTT(), ancCareHistoryVO.getLastTTDate());
- IPTVaccineCare iptVaccineCare = new IPTVaccineCare(patient, expectedDeliveryDate, activeCareSchedules.hasActiveIPTSchedule(), ancCareHistoryVO.getLastIPT(), ancCareHistoryVO.getLastIPTDate());
+ Date lastTTDate = getLastTTDate(ancCareHistoryVO,expectedDeliveryDate);
+ TTVaccineCare ttVaccineCare = new TTVaccineCare(patient, expectedDeliveryDate, newDate(ancVO.getRegistrationDate()),
+ activeCareSchedules.hasActiveTTSchedule(), ancCareHistoryVO.getLastTT(), lastTTDate);
+
+ Date lastIPTDate = getLastIPTDate(ancCareHistoryVO,expectedDeliveryDate);
+ IPTVaccineCare iptVaccineCare = new IPTVaccineCare(patient, expectedDeliveryDate, activeCareSchedules.hasActiveIPTSchedule(), ancCareHistoryVO.getLastIPT(), lastIPTDate);
List<PatientCare> patientCares = new ANCCareRegistration(ttVaccineCare, iptVaccineCare, patient, expectedDeliveryDate).allCares();
enrollPatientCares(patientCares, patient);
}
+ private Date getLastIPTDate(ANCCareHistoryVO ancCareHistoryVO,LocalDate edd) {
+ IPTDose nextIPTMilestone = ancCareHistoryVO.getLastIPT()!=null ? getNextOf(IPTDose.byValue(ancCareHistoryVO.getLastIPT())) : null;
+ Date lastIPTDate = ancCareHistoryVO.getLastIPTDate();
+ if(lastIPTDate!=null && nextIPTMilestone!=null) {
+ lastIPTDate = getEnrollmentDate(ANC_IPT_VACCINE, DateUtil.newDate(lastIPTDate), nextIPTMilestone.milestone(),edd).toDate();
+ }
+ return lastIPTDate;
+ }
+
+ private Date getLastTTDate(ANCCareHistoryVO ancCareHistoryVO,LocalDate edd) {
+ Date lastTTDate = ancCareHistoryVO.getLastTTDate();
+ TTVaccineDosage nextTTMilestone = (ancCareHistoryVO.getLastTT()!=null) ? getNextOf(TTVaccineDosage.byValue(Integer.parseInt(ancCareHistoryVO.getLastTT()))) : null;
+ if(lastTTDate!=null && nextTTMilestone!=null) {
+ lastTTDate= getEnrollmentDate(TT_VACCINATION, DateUtil.newDate(lastTTDate), nextTTMilestone.getScheduleMilestoneName(),edd).toDate();
+ }
+ return lastTTDate;
+ }
+
ActiveCareSchedules activeCareSchedules(Patient patient, List<String> scheduleNames) {
ActiveCareSchedules activeCareSchedules = new ActiveCareSchedules();
for (String scheduleName : scheduleNames)
@@ -129,7 +154,7 @@ void enrollPatientCares(List<PatientCare> patientCares, Patient patient) {
}
}
- public void enrollChildForPNCOnDelivery(Patient child) {
+ public void enrollChildForPNC(Patient child) {
enrollPatientCares(child.pncBabyProgramsToEnrollOnRegistration(), child);
}
@@ -275,11 +300,14 @@ private void processANCHistories(CareHistoryVO careHistoryVO, Patient patient) {
if (activePregnancyObservation != null) {
Date edd = getEDD(patient.getMotechId());
+ LocalDate expectedDeliveryDate = newDate(edd);
ActiveCareSchedules activeCareSchedules = activeCareSchedules(patient, Arrays.asList(TT_VACCINATION, ANC_IPT_VACCINE));
+ Date lastTTDate = getLastTTDate(ancCareHistoryVO,expectedDeliveryDate);
+ TTVaccineCare ttVaccineCare = new TTVaccineCare(patient, expectedDeliveryDate, newDate(careHistoryVO.getDate()), activeCareSchedules.hasActiveTTSchedule(), ancCareHistoryVO.getLastTT(), lastTTDate);
- TTVaccineCare ttVaccineCare = new TTVaccineCare(patient, newDate(edd), newDate(careHistoryVO.getDate()), activeCareSchedules.hasActiveTTSchedule(), ancCareHistoryVO.getLastTT(), ancCareHistoryVO.getLastTTDate());
- IPTVaccineCare iptVaccineCare = new IPTVaccineCare(patient, newDate(edd), activeCareSchedules.hasActiveIPTSchedule(), ancCareHistoryVO.getLastIPT(), ancCareHistoryVO.getLastIPTDate());
+ Date lastIPTDate = getLastIPTDate(ancCareHistoryVO,expectedDeliveryDate);
+ IPTVaccineCare iptVaccineCare = new IPTVaccineCare(patient, expectedDeliveryDate, activeCareSchedules.hasActiveIPTSchedule(), ancCareHistoryVO.getLastIPT(), lastIPTDate);
final List<PatientCare> caresApplicableToTheCurrentPregnancy = CareHistory.forPregnancy(ttVaccineCare, iptVaccineCare).cares();
@@ -297,8 +325,23 @@ private void processANCHistories(CareHistoryVO careHistoryVO, Patient patient) {
}
}
+ public LocalDate getEnrollmentDate(String scheduleName, LocalDate lastCareTakenDate, String startMilestoneName,LocalDate edd) {
+ EnrollmentRequest enrollmentRequest = new EnrollmentRequest(null, scheduleName, null, null, null, lastCareTakenDate, new Time(0,0), startMilestoneName, null);
+ List<DateTime> dueWindowAlertTimings = allSchedules.getDueWindowAlertTimings(enrollmentRequest);
+ Pregnancy pregnancy = Pregnancy.basedOnDeliveryDate(edd);
+ if (!dueWindowAlertTimings.isEmpty() && dueWindowAlertTimings.get(0).isBeforeNow() && lastCareTakenDate.toDate().after(pregnancy.dateOfConception().toDate()))
+ return getDifferenceOfDates(dueWindowAlertTimings.get(0), DateUtil.newDateTime(lastCareTakenDate)).toLocalDate();
+ return lastCareTakenDate;
+ }
+
+ private DateTime getDifferenceOfDates(DateTime dueDate, DateTime lastCareTakenDate) {
+ Period period = new Period(lastCareTakenDate,dueDate);
+ return DateUtil.now().minus(period);
+ }
+
+
Date getEDD(String motechId) {
MRSObservation eddObservation = allObservations.findObservation(motechId, EDD.getName());
- return eddObservation!=null?(Date) eddObservation.getValue():null;
+ return eddObservation != null ? (Date) eddObservation.getValue() : null;
}
}
@@ -103,7 +103,7 @@ public void handleDelivery(PregnancyDeliveryRequest request) {
allEncounters.persistEncounter(encounterFactory.createBirthEncounter(childRequest, savedChild.getMrsPatient(), staff, facility, birthDate));
careService.enroll(new CwcVO(staff.getSystemId(), facility.mrsFacilityId(), birthDate, savedChild.getMotechId(),
Collections.<CwcCareHistory>emptyList(), null, null, null, null, null, null, null, null, null, null, savedChild.getMotechId(), false));
- careService.enrollChildForPNCOnDelivery(savedChild);
+ careService.enrollChildForPNC(savedChild);
smsForEachChild.add(new SMSTemplate().fillPatientDetails(savedChild));
}
}
@@ -82,6 +82,18 @@ public void shouldVerifyTTVaccinationScheduleOnANCRegistration() throws Schedule
alert(late, onDate("2-Apr-2012"))));
}
+// @Test
+// public void shouldMoveDueDateAccordingToHistoryInput() throws SchedulerException {
+// mockToday(newDate("19-Apr-2012"));
+// EnrollmentRequest enrollmentRequest = new EnrollmentRequest(PATIENT_ID, scheduleName, preferredAlertTime, null, null, newDate("01-Mar-2012"), null, TTVaccineDosage.TT2.getScheduleMilestoneName(), null);
+// enrollmentId = scheduleTrackingService.enroll(enrollmentRequest);
+//
+// assertTestAlerts(captureAlertsForNextMilestone(enrollmentId), asList(
+// alert(late, onDate("19-Apr-2012")),
+// alert(late, onDate("26-Apr-2012")),
+// alert(late, onDate("3-May-2012"))));
+// }
+
private String scheduleAlertForTTVaccination(LocalDate firstDosageDate) {
EnrollmentRequest enrollmentRequest = new EnrollmentRequest(PATIENT_ID, scheduleName, preferredAlertTime, firstDosageDate, null, null, null, null, null);
return scheduleTrackingService.enroll(enrollmentRequest);
@@ -7,10 +7,7 @@
import org.mockito.Mock;
import org.motechproject.model.Time;
import org.motechproject.scheduletracking.api.domain.exception.InvalidEnrollmentException;
-import org.motechproject.scheduletracking.api.service.EnrollmentRecord;
-import org.motechproject.scheduletracking.api.service.EnrollmentRequest;
-import org.motechproject.scheduletracking.api.service.EnrollmentsQuery;
-import org.motechproject.scheduletracking.api.service.ScheduleTrackingService;
+import org.motechproject.scheduletracking.api.service.*;
import java.util.Arrays;
import java.util.List;
@@ -135,4 +132,13 @@ public void shouldReturnActiveEnrollmentToAScheduleGivenAnEnrollmentIdAndSchedul
when(mockScheduleTrackingService.getEnrollment(externalId, scheduleName)).thenReturn(enrollmentRecord);
assertThat(enrollmentRecord, is(equalTo(allSchedules.getActiveEnrollment(externalId, scheduleName))));
}
+
+ @Test
+ public void shouldReturnDueAlertsGivenAnEnrollmentRequest() {
+ EnrollmentRequest request = new EnrollmentRequest("123", "scheduleName", new Time(12, 0), new LocalDate(), null, null, null, null, null);
+ MilestoneAlerts mockMilestoneAlerts = mock(MilestoneAlerts.class);
+ when(mockScheduleTrackingService.getAlertTimings(request)).thenReturn(mockMilestoneAlerts);
+ allSchedules.getDueWindowAlertTimings(request);
+ verify(mockMilestoneAlerts).getDueWindowAlertTimings();
+ }
}
Oops, something went wrong.

0 comments on commit 38003fb

Please sign in to comment.