Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) Replace the existing name instead of creating a new name #546

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,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
Loading