Skip to content

Commit

Permalink
FM2-650: Replace the existing name instead of creating a new name (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
icrc-jofrancisco authored Dec 3, 2024
1 parent 10a5c39 commit c6ea1b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@ public org.openmrs.Patient toOpenmrsType(@Nonnull org.openmrs.Patient currentPat
}

for (HumanName name : patient.getName()) {
currentPatient.addName(nameTranslator.toOpenmrsType(name));
PersonName existingName = null;
if (name.hasId()) {
existingName = currentPatient.getNames().stream().filter(n -> n.getUuid().equals(name.getId())).findFirst()
.orElse(null);
}

PersonName pn = nameTranslator.toOpenmrsType(existingName != null ? existingName : new PersonName(), name);
currentPatient.addName(pn);
}

if (patient.hasGender()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class PatientTranslatorImplTest {

private static final String PATIENT_IDENTIFIER_UUID = "654321-fedcba-654321";

private static final String PATIENT_NAME_UUID = "1fdb5469-57c7-435f-b009-3dcceb23b0a2";

private static final String PATIENT_GIVEN_NAME = "Jean Claude";

private static final String PATIENT_FAMILY_NAME = "van Damme";
Expand Down Expand Up @@ -304,17 +306,38 @@ public void shouldTranslateFhirPatientNameToOpenmrsPatientName() {
PersonName personName = new PersonName();
personName.setGivenName(PATIENT_GIVEN_NAME);
personName.setFamilyName(PATIENT_FAMILY_NAME);
when(nameTranslator.toOpenmrsType(any())).thenReturn(personName);
when(nameTranslator.toOpenmrsType(any(), any())).thenReturn(personName);

Patient patient = new Patient();
HumanName name = new HumanName();
name.addGiven(PATIENT_GIVEN_NAME);
name.setFamily(PATIENT_FAMILY_NAME);
patient.addName(name);

org.openmrs.Patient result = patientTranslator.toOpenmrsType(patient);
assertThat(result.getGivenName(), equalTo(PATIENT_GIVEN_NAME));
assertThat(result.getFamilyName(), equalTo(PATIENT_FAMILY_NAME));
}

@Test
public void shouldTranslateFhirPatientNameToExistingOpenmrsPatientName() {
PersonName personName = new PersonName();
personName.setUuid(PATIENT_NAME_UUID);
personName.setGivenName(PATIENT_GIVEN_NAME);
personName.setFamilyName(PATIENT_FAMILY_NAME);
when(nameTranslator.toOpenmrsType(any(), any())).thenReturn(personName);

Patient patient = new Patient();
HumanName name = new HumanName();
name.setId(PATIENT_NAME_UUID);
name.addGiven(PATIENT_GIVEN_NAME);
name.setFamily(PATIENT_FAMILY_NAME);
patient.addName(name);

org.openmrs.Patient result = patientTranslator.toOpenmrsType(patient);
assertThat(result.getGivenName(), equalTo(PATIENT_GIVEN_NAME));
assertThat(result.getFamilyName(), equalTo(PATIENT_FAMILY_NAME));
assertThat(result.getPersonName().getUuid(), equalTo(PATIENT_NAME_UUID));
}

@Test
Expand Down

0 comments on commit c6ea1b3

Please sign in to comment.