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) Set encounter and default role for encounter provider entity #542

Merged
merged 11 commits into from
Jul 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import lombok.Setter;
import org.hl7.fhir.r4.model.Encounter;
import org.openmrs.EncounterProvider;
import org.openmrs.EncounterRole;
import org.openmrs.api.EncounterService;
import org.openmrs.module.fhir2.api.FhirGlobalPropertyService;
import org.openmrs.module.fhir2.api.dao.FhirPractitionerDao;
import org.openmrs.module.fhir2.api.translators.EncounterParticipantTranslator;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,9 +29,17 @@
@Setter(AccessLevel.PACKAGE)
public class EncounterParticipantTranslatorImpl extends BaseReferenceHandlingTranslator implements EncounterParticipantTranslator {

private static final String DEFAULT_ENCOUNTER_ROLE_UUID_PROPERTY = "fhir2.encounterParticipantComponentUuid";

@Autowired
private FhirPractitionerDao practitionerDao;

@Autowired
private EncounterService encounterService;

@Autowired
private FhirGlobalPropertyService globalPropertyService;

@Override
public Encounter.EncounterParticipantComponent toFhirResource(@Nonnull EncounterProvider encounterProvider) {
if (encounterProvider == null || encounterProvider.getVoided()) {
Expand All @@ -49,6 +60,26 @@ public EncounterProvider toOpenmrsType(@Nonnull EncounterProvider encounterProvi
getReferenceId(encounterParticipantComponent.getIndividual())
.map(practitionerUuid -> practitionerDao.get(practitionerUuid)).ifPresent(encounterProvider::setProvider);

if (encounterProvider.getEncounterRole() == null) {
encounterProvider.setEncounterRole(getDefaultEncounterRole());
}

return encounterProvider;
}

protected EncounterRole getDefaultEncounterRole() {
String defaultEncounterRoleUuid = globalPropertyService.getGlobalProperty(DEFAULT_ENCOUNTER_ROLE_UUID_PROPERTY);

String encounterRoleUuid = (defaultEncounterRoleUuid != null && !defaultEncounterRoleUuid.isEmpty())
? defaultEncounterRoleUuid
: EncounterRole.UNKNOWN_ENCOUNTER_ROLE_UUID;

EncounterRole role = encounterService.getEncounterRoleByUuid(encounterRoleUuid);

if (role == null) {
throw new IllegalStateException("Default encounter role not found: " + encounterRoleUuid);
}

return role;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ public org.openmrs.Encounter toOpenmrsType(@Nonnull org.openmrs.Encounter existi
existingProviders = new LinkedHashSet<>(encounter.getParticipant().size());
}

existingProviders.addAll(encounter
.getParticipant().stream().map(encounterParticipantComponent -> participantTranslator
.toOpenmrsType(new EncounterProvider(), encounterParticipantComponent))
.collect(Collectors.toCollection(LinkedHashSet::new)));
existingProviders.addAll(encounter.getParticipant().stream()
.map(encounterParticipantComponent -> participantTranslator.toOpenmrsType(new EncounterProvider(),
encounterParticipantComponent))
.peek(ep -> ep.setEncounter(existingEncounter)).collect(Collectors.toCollection(LinkedHashSet::new)));

existingEncounter.setEncounterProviders(existingProviders);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

import org.hl7.fhir.r4.model.Encounter;
Expand All @@ -24,8 +25,11 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.EncounterProvider;
import org.openmrs.EncounterRole;
import org.openmrs.Provider;
import org.openmrs.api.EncounterService;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirGlobalPropertyService;
import org.openmrs.module.fhir2.api.dao.FhirPractitionerDao;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -38,6 +42,12 @@ public class EncounterParticipantTranslatorImplTest {
@Mock
private FhirPractitionerDao practitionerDao;

@Mock
private EncounterService encounterService;

@Mock
private FhirGlobalPropertyService globalPropertyService;

private EncounterParticipantTranslatorImpl participantTranslator;

private EncounterProvider encounterProvider;
Expand All @@ -50,11 +60,13 @@ public class EncounterParticipantTranslatorImplTest {
public void setUp() {
participantTranslator = new EncounterParticipantTranslatorImpl();
participantTranslator.setPractitionerDao(practitionerDao);
participantTranslator.setGlobalPropertyService(globalPropertyService);

encounterProvider = new EncounterProvider();
provider = new Provider();
provider.setUuid(PROVIDER_UUID);
encounterProvider.setProvider(provider);
participantTranslator.setEncounterService(encounterService);

encounterParticipantComponent = new Encounter.EncounterParticipantComponent();
Reference reference = new Reference(PROVIDER_URI);
Expand All @@ -81,6 +93,8 @@ public void shouldTranslateEncounterProviderToFhirTypeWithCorrectIndividualRefer

@Test
public void shouldTranslateEncounterParticipantToOpenMrsType() {
when(encounterService.getEncounterRoleByUuid(EncounterRole.UNKNOWN_ENCOUNTER_ROLE_UUID))
.thenReturn(new EncounterRole());
EncounterProvider encounterProvider = participantTranslator.toOpenmrsType(new EncounterProvider(),
encounterParticipantComponent);
assertThat(encounterProvider, notNullValue());
Expand All @@ -89,6 +103,8 @@ public void shouldTranslateEncounterParticipantToOpenMrsType() {
@Test
public void shouldTranslateEncounterParticipantToEncounterProviderWithCorrectProvider() {
when(practitionerDao.get(PROVIDER_UUID)).thenReturn(provider);
when(encounterService.getEncounterRoleByUuid(EncounterRole.UNKNOWN_ENCOUNTER_ROLE_UUID))
.thenReturn(new EncounterRole());

EncounterProvider encounterProvider = participantTranslator.toOpenmrsType(new EncounterProvider(),
encounterParticipantComponent);
Expand All @@ -102,4 +118,10 @@ public void shouldTranslateEncounterParticipantToEncounterProviderWithCorrectPro
public void shouldThrowExceptionWhenEncounterParticipantIsNull() {
participantTranslator.toOpenmrsType(new EncounterProvider(), null);
}

@Test
public void toFhirResource_shouldThrowExceptionWhenUnknownRoleIsNull() {
assertThrows(IllegalStateException.class,
() -> participantTranslator.toOpenmrsType(new EncounterProvider(), encounterParticipantComponent));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.EncounterProvider;
import org.openmrs.EncounterType;
import org.openmrs.Location;
import org.openmrs.Patient;
import org.openmrs.PatientIdentifier;
import org.openmrs.PatientIdentifierType;
import org.openmrs.PersonName;
import org.openmrs.Provider;
import org.openmrs.Visit;
import org.openmrs.*;
import org.openmrs.api.EncounterService;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.mappings.EncounterClassMap;
import org.openmrs.module.fhir2.api.translators.EncounterLocationTranslator;
Expand Down Expand Up @@ -110,6 +103,9 @@ public class EncounterTranslatorImplTest {

@Mock
private EncounterClassMap encounterClassMap;

@Mock
private EncounterService encounterService;

private Patient patient;

Expand Down Expand Up @@ -250,6 +246,7 @@ public void toOpenMrsType_shouldTranslateParticipantToEncounterProvider() {
Patient patient = new Patient();
patient.setUuid(PATIENT_UUID);
when(patientReferenceTranslator.toOpenmrsType(patientRef)).thenReturn(patient);
when(participantTranslator.toOpenmrsType(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(new EncounterProvider());
org.openmrs.Encounter result = encounterTranslator.toOpenmrsType(fhirEncounter);

assertThat(result.getEncounterProviders(), not(empty()));
Expand Down
Loading