diff --git a/api/src/main/java/org/openmrs/api/impl/PatientServiceImpl.java b/api/src/main/java/org/openmrs/api/impl/PatientServiceImpl.java index af1f5d203875..e0f2a0399f21 100644 --- a/api/src/main/java/org/openmrs/api/impl/PatientServiceImpl.java +++ b/api/src/main/java/org/openmrs/api/impl/PatientServiceImpl.java @@ -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()); } } } diff --git a/api/src/main/java/org/openmrs/person/PersonMergeLogData.java b/api/src/main/java/org/openmrs/person/PersonMergeLogData.java index f53efe1f5408..bacffb0cd08d 100644 --- a/api/src/main/java/org/openmrs/person/PersonMergeLogData.java +++ b/api/src/main/java/org/openmrs/person/PersonMergeLogData.java @@ -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 createdPrograms; + + /** + * List of UUIDs of patient programs moved from non-preferred to preferred + */ + private List movedPrograms; /** * List of UUIDs of voided relationships @@ -143,16 +150,29 @@ public void addMovedEncounter(String uuid) { movedEncounters.add(uuid); } + @Deprecated public List getCreatedPrograms() { return createdPrograms; } + @Deprecated public void addCreatedProgram(String uuid) { if (createdPrograms == null) { createdPrograms = new ArrayList<>(); } createdPrograms.add(uuid); } + + public List getMovedPrograms() { + return movedPrograms; + } + + public void addMovedProgram(String uuid) { + if (movedPrograms == null) { + movedPrograms = new ArrayList<>(); + } + movedPrograms.add(uuid); + } public List getVoidedRelationships() { return voidedRelationships; @@ -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(); } diff --git a/api/src/test/java/org/openmrs/api/PatientServiceTest.java b/api/src/test/java/org/openmrs/api/PatientServiceTest.java index ced6ec5e5e26..45ef5c0ee78e 100644 --- a/api/src/test/java/org/openmrs/api/PatientServiceTest.java +++ b/api/src/test/java/org/openmrs/api/PatientServiceTest.java @@ -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 nonPreferredPrograms = Context.getProgramWorkflowService() + .getPatientPrograms(notPreferred, null, null, null, null, null, false); + + //retrieve program for preferred patient + List 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 programUuids = nonPreferredPrograms.stream().map(PatientProgram::getUuid).collect(Collectors.toList()); + PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred); + + //retrieve updated program for notProferred patient + List updatedNonPreferredPrograms = Context.getProgramWorkflowService() + .getPatientPrograms(notPreferred, null, null, null, null, null, false); + + //retrieve program for preferred patient + List 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 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)); } /** diff --git a/api/src/test/java/org/openmrs/api/PersonServiceTest.java b/api/src/test/java/org/openmrs/api/PersonServiceTest.java index 7c5caa422fce..d0359500a066 100644 --- a/api/src/test/java/org/openmrs/api/PersonServiceTest.java +++ b/api/src/test/java/org/openmrs/api/PersonServiceTest.java @@ -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");