Skip to content

Commit

Permalink
EA-168: Add methods to ExitFromCare service to support to marking a p… (
Browse files Browse the repository at this point in the history
#204)

* EA-168: Add methods to ExitFromCare service to support to marking a patient as "not dead

* EA-168: Add methods to ExitFromCare service to support to marking a patient as "not dead"
  • Loading branch information
mogoodrich authored Jul 1, 2021
1 parent 44b3e5d commit 25d32b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ public interface ExitFromCareService extends OpenmrsService {

/**
* Reopens (ie, sets outcome and completion date to null) any programs that have an outcome equal to
* the "outcome", and a completion date equal to the "completionDate"
* the "outcome"
*
* @param patient
* @param outcome
* @param completionDate
*/
void reopenPatientPrograms(Patient patient, Concept outcome, Date completionDate);
void reopenPatientPrograms(Patient patient, Concept outcome);


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openmrs.module.emrapi.exitfromcare;

import org.joda.time.DateTime;
import org.openmrs.Concept;
import org.openmrs.Patient;
import org.openmrs.PatientProgram;
Expand Down Expand Up @@ -52,12 +51,10 @@ public void closePatientPrograms(Patient patient, Concept outcome, Date completi
}

@Override
public void reopenPatientPrograms(Patient patient, Concept outcome, Date completionDate) {
// iterate through all programs and close those with outcome that matches outcome and completion date that matches completion date
public void reopenPatientPrograms(Patient patient, Concept outcome) {
// iterate through all programs and reopen those with outcome that matches outcome
for (PatientProgram patientProgram : programWorkflowService.getPatientPrograms(patient, null, null, null, null, null, false)) {
if (patientProgram.getDateCompleted() != null && completionDate != null
&& new DateTime(patientProgram.getDateCompleted()).withTimeAtStartOfDay().equals(new DateTime(completionDate).withTimeAtStartOfDay()) // just match date, not datetime
&& outcome.equals(patientProgram.getOutcome())) {
if (outcome.equals(patientProgram.getOutcome())) {
patientProgram.setOutcome(null);
patientProgram.setDateCompleted(null);
programWorkflowService.savePatientProgram(patientProgram);
Expand Down Expand Up @@ -130,7 +127,7 @@ public void markPatientNotDead(Patient patient) {

Concept patientDied = emrApiProperties.getPatientDiedConcept();
if (patientDied != null && deathDate != null) {
reopenPatientPrograms(patient, patientDied, deathDate);
reopenPatientPrograms(patient, patientDied);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void closePatientPrograms_shouldThrowExceptionIfDateInFuture() {
}

@Test
public void closePatientPrograms_shouldCloseActivePatientProgramIfOnStartDateIfCloseDateBeforeStartDate() {
public void closePatientPrograms_shouldCloseActivePatientProgramOnStartDateIfCloseDateBeforeStartDate() {

Patient patient = new Patient();

Expand Down Expand Up @@ -429,7 +429,7 @@ public void markPatientDead_shouldFailIfDeathDateBeforeBirthDate() {


@Test
public void reopenPatientPrograms_shouldReopenClosedProgramWithMatchingOutcomeAndDate() {
public void reopenPatientPrograms_shouldReopenClosedProgramWithMatchingOutcome() {

Patient patient = new Patient();

Expand All @@ -451,45 +451,13 @@ public void reopenPatientPrograms_shouldReopenClosedProgramWithMatchingOutcomeAn
when(mockProgramWorkflowService.getPatientPrograms(patient, null, null, null, null, null,false))
.thenReturn(Arrays.asList(pp1));

exitFromCareService.reopenPatientPrograms(patient, outcome, new DateTime(2019, 10, 10, 10, 10).toDate()); // same date as completion date, but different time component

exitFromCareService.reopenPatientPrograms(patient, outcome);
assertNull(pp1.getDateCompleted());
assertNull(pp1.getOutcome());

verify(mockProgramWorkflowService,times(1)).savePatientProgram(pp1);
}

@Test
public void reopenPatientPrograms_shouldNotReopenClosedProgramIfCompletionDateDoesNotMatch() {

Patient patient = new Patient();

Concept outcome = new Concept(2);

Date enrollmentDate = new DateTime(2018, 11, 11, 10, 10).toDate();
Date completionDate = new DateTime(2019, 10, 10,5, 5).toDate();

Program program = new Program();

PatientProgram pp1 = new PatientProgram();
pp1.setProgram(program);
pp1.setDateEnrolled(enrollmentDate);
pp1.setDateCompleted(completionDate);
pp1.setOutcome(outcome);

pp1.setPatient(patient);

when(mockProgramWorkflowService.getPatientPrograms(patient, null, null, null, null, null,false))
.thenReturn(Arrays.asList(pp1));

exitFromCareService.reopenPatientPrograms(patient, outcome, new DateTime(2019, 11, 10, 10, 10).toDate()); // different date from completion date

assertThat(pp1.getDateCompleted(), is(completionDate));
assertThat(pp1.getOutcome(), is(outcome));

verify(mockProgramWorkflowService,times(0)).savePatientProgram(pp1);
}

@Test
public void reopenPatientPrograms_shouldNotReopenClosedProgramIfOutcomeDoesNotMatch() {

Expand All @@ -514,7 +482,7 @@ public void reopenPatientPrograms_shouldNotReopenClosedProgramIfOutcomeDoesNotMa
when(mockProgramWorkflowService.getPatientPrograms(patient, null, null, null, null, null,false))
.thenReturn(Arrays.asList(pp1));

exitFromCareService.reopenPatientPrograms(patient, differentOutcome, completionDate); // different date from completion date
exitFromCareService.reopenPatientPrograms(patient, differentOutcome);

assertThat(pp1.getDateCompleted(), is(completionDate));
assertThat(pp1.getOutcome(), is(outcome));
Expand All @@ -541,7 +509,7 @@ public void markPatientNotDead_shouldUnmarkPatientDead() {
assertNull(patient.getDeathDate());

verify(mockPatientService, times(1)).savePatient(patient);
verify(exitFromCareService, times(1)).reopenPatientPrograms(patient, patientDied, now);
verify(exitFromCareService, times(1)).reopenPatientPrograms(patient, patientDied);
}

}

0 comments on commit 25d32b8

Please sign in to comment.