Skip to content

Commit

Permalink
TRUNK-6247: Merging Patients should not copy Patient Programs on "los…
Browse files Browse the repository at this point in the history
…ing" patients (#4703)
  • Loading branch information
mogoodrich authored Aug 15, 2024
1 parent 3e7df3d commit fe8ebe1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,10 @@ private void mergeProgramEnrolments(Patient preferred, Patient notPreferred, Per
ProgramWorkflowService programService = Context.getProgramWorkflowService();
for (PatientProgram pp : programService.getPatientPrograms(notPreferred, null, null, null, null, null, false)) {
if (!pp.getVoided()) {
PatientProgram enroll = pp.copy();
enroll.setPatient(preferred);
log.debug("Copying patientProgram {} to {}", pp.getPatientProgramId(), preferred.getPatientId());
PatientProgram persisted = programService.savePatientProgram(enroll);
mergedData.addCreatedProgram(persisted.getUuid());
pp.setPatient(preferred);
log.debug("Moving patientProgram {} to {}", pp.getPatientProgramId(), preferred.getPatientId());
PatientProgram persisted = programService.savePatientProgram(pp);
mergedData.addMovedProgram(persisted.getUuid());
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions api/src/main/java/org/openmrs/person/PersonMergeLogData.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ public class PersonMergeLogData {

/**
* List of UUIDs of patient programs copied from non-preferred to preferred
* (Deprecated in 2.6.8 and 2.7.0+, as we now move programs)
*/
@Deprecated
private List<String> createdPrograms;

/**
* List of UUIDs of patient programs moved from non-preferred to preferred
*/
private List<String> movedPrograms;

/**
* List of UUIDs of voided relationships
Expand Down Expand Up @@ -143,16 +150,29 @@ public void addMovedEncounter(String uuid) {
movedEncounters.add(uuid);
}

@Deprecated
public List<String> getCreatedPrograms() {
return createdPrograms;
}

@Deprecated
public void addCreatedProgram(String uuid) {
if (createdPrograms == null) {
createdPrograms = new ArrayList<>();
}
createdPrograms.add(uuid);
}

public List<String> getMovedPrograms() {
return movedPrograms;
}

public void addMovedProgram(String uuid) {
if (movedPrograms == null) {
movedPrograms = new ArrayList<>();
}
movedPrograms.add(uuid);
}

public List<String> getVoidedRelationships() {
return voidedRelationships;
Expand Down Expand Up @@ -326,6 +346,9 @@ public int computeHashValue() {
if (getCreatedPrograms() != null) {
str += getCreatedPrograms().toString();
}
if (getMovedPrograms() != null) {
str += getMovedPrograms().toString();
}
if (getCreatedRelationships() != null) {
str += getCreatedRelationships().toString();
}
Expand Down
46 changes: 29 additions & 17 deletions api/src/test/java/org/openmrs/api/PatientServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2350,32 +2350,44 @@ public void mergePatients_shouldAuditCreatedNames() throws Exception {
/**
* @see PatientService#mergePatients(Patient,Patient)
*/

@Test
public void mergePatients_shouldAuditCreatedPatientPrograms() throws Exception {
public void mergePatients_shouldCopyProgramsFromNonPreferredPatient() throws Exception {
//retrieve preferred and notPreferredPatient patient
Patient preferred = patientService.getPatient(999);
Patient notPreferred = patientService.getPatient(2);
voidOrders(Collections.singleton(notPreferred));

//retrieve program for notProferred patient
PatientProgram program = Context.getProgramWorkflowService()
.getPatientPrograms(notPreferred, null, null, null, null, null, false).get(0);
List<PatientProgram> nonPreferredPrograms = Context.getProgramWorkflowService()
.getPatientPrograms(notPreferred, null, null, null, null, null, false);

//retrieve program for preferred patient
List<PatientProgram> preferredPrograms = Context.getProgramWorkflowService()
.getPatientPrograms(preferred, null, null, null, null, null, false);


//merge the two patients and retrieve the audit object
assertEquals(2, nonPreferredPrograms.size()); // sanity check, non preferred patient had 2 programs at time of writing test
assertEquals(0, preferredPrograms.size()); // sanity check, preferred patient had 0 programs at time of writing test

List<String> programUuids = nonPreferredPrograms.stream().map(PatientProgram::getUuid).collect(Collectors.toList());

PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred);

//retrieve updated program for notProferred patient
List<PatientProgram> updatedNonPreferredPrograms = Context.getProgramWorkflowService()
.getPatientPrograms(notPreferred, null, null, null, null, null, false);

//retrieve program for preferred patient
List<PatientProgram> updatedPreferredPrograms = Context.getProgramWorkflowService()
.getPatientPrograms(preferred, null, null, null, null, null, false);

//find the UUID of the program to which the preferred patient was enrolled as a result of the merge
String enrolledProgramUuid = null;
List<PatientProgram> programs = Context.getProgramWorkflowService().getPatientPrograms(preferred, null, null, null,
null, null, false);
for (PatientProgram p : programs) {
if (p.getDateCreated().equals(program.getDateCreated())) {
enrolledProgramUuid = p.getUuid();
}
}
assertNotNull("expected enrolled program was not found for the preferred patient after the merge",
enrolledProgramUuid);
assertTrue(isValueInList(enrolledProgramUuid, audit.getPersonMergeLogData().getCreatedPrograms()), "program enrollment not audited");
// programs should be copied from nonPreferred to preferred
assertEquals(0, updatedNonPreferredPrograms.size());
assertEquals(2, updatedPreferredPrograms.size());

assertTrue(updatedPreferredPrograms.stream().map(PatientProgram::getUuid).allMatch(programUuids::contains));
assertTrue(audit.getPersonMergeLogData().getMovedPrograms().stream().allMatch(programUuids::contains));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/java/org/openmrs/api/PersonServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ private PersonMergeLog getTestPersonMergeLog() {
data.addCreatedIdentifier("3");
data.addCreatedName("4");
data.addCreatedOrder("5");
data.addCreatedProgram("6");
data.addMovedProgram("6");
data.addCreatedRelationship("7");
data.addMovedEncounter("8");
data.addMovedIndependentObservation("9");
Expand Down

0 comments on commit fe8ebe1

Please sign in to comment.