Skip to content

Commit

Permalink
Associate a default Practitioner to the Task when openelisuser is not…
Browse files Browse the repository at this point in the history
… defined (#51)

* upgrade fhir2 version

* Task should use a dynamic practitioner if OpenLIS user is not defined

* fix NPE

* fix preactitioner user
  • Loading branch information
mozzy11 authored Sep 12, 2024
1 parent 5f25237 commit 6a024d2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import org.openmrs.api.db.DAOException;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirObservationService;
import org.openmrs.module.fhir2.api.FhirPractitionerService;
import org.openmrs.module.fhir2.api.FhirTaskService;
import org.openmrs.module.labonfhir.LabOnFhirConfig;
import org.openmrs.module.labonfhir.api.fhir.OrderCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;

@Component
public class LabOrderHandler {

Expand All @@ -41,6 +44,9 @@ public class LabOrderHandler {
@Autowired
private FhirObservationService observationService;

@Autowired
private FhirPractitionerService practitionerService;

public Task createOrder(Order order) throws OrderCreationException {
//TDO: MAKE THIS A GLOBAL CONFIG
final String REQUIRED_TESTS_UUIDS = config.getOrderTestUuids(); // GeneXpert
Expand Down Expand Up @@ -95,8 +101,19 @@ public Task createOrder(Order order) throws OrderCreationException {
Optional<EncounterProvider> requesterProvider = order.getEncounter().getActiveEncounterProviders().stream()
.findFirst();

Reference requesterRef = requesterProvider.map(
encounterProvider -> newReference(encounterProvider.getUuid(), FhirConstants.PRACTITIONER)).orElse(null);
Reference requesterRef = requesterProvider.isPresent()? newReference(requesterProvider.get().getProvider().getUuid(), FhirConstants.PRACTITIONER) : null;

try {
practitionerService.get(config.getLisUserUuid());
} catch (ResourceNotFoundException e) {
if (requesterRef != null) {
ownerRef = requesterRef;
} else {
ownerRef = newReference(order.getEncounter().getCreator().getUuid(), FhirConstants.PRACTITIONER);
}

}


Reference locationRef = null;
if (order.getEncounter().getLocation() != null) {
Expand Down Expand Up @@ -163,9 +180,18 @@ public Task createOrder(Encounter encounter) throws OrderCreationException {
Optional<EncounterProvider> requesterProvider = encounter.getActiveEncounterProviders().stream().findFirst();

Reference requesterRef = requesterProvider.isPresent() ?
newReference(requesterProvider.get().getUuid(), FhirConstants.PRACTITIONER) :
newReference(requesterProvider.get().getProvider().getUuid(), FhirConstants.PRACTITIONER) :
null;

try {
practitionerService.get(config.getLisUserUuid());
} catch (ResourceNotFoundException e) {
if (requesterRef != null) {
ownerRef = requesterRef;
} else {
ownerRef = newReference(encounter.getCreator().getUuid(), FhirConstants.PRACTITIONER);
}
}

List<Task.ParameterComponent> taskInputs = null;
if (config.addObsAsTaskInput()) {
taskInputs = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenParam;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;

import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Resource;
Expand All @@ -18,6 +20,7 @@
import org.openmrs.event.EventListener;
import org.openmrs.module.DaemonToken;
import org.openmrs.module.fhir2.api.FhirLocationService;
import org.openmrs.module.fhir2.api.FhirPractitionerService;
import org.openmrs.module.fhir2.api.FhirTaskService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
import org.openmrs.module.labonfhir.LabOnFhirConfig;
Expand Down Expand Up @@ -55,6 +58,9 @@ public abstract class LabCreationListener implements EventListener {
@Autowired
private LabOnFhirService labOnFhirService ;

@Autowired
private FhirPractitionerService practitionerService;

public DaemonToken getDaemonToken() {
return daemonToken;
}
Expand Down Expand Up @@ -96,6 +102,14 @@ public Bundle createLabBundle(Task task) {
if (!task.getLocation().isEmpty()) {
labResources.add(fhirLocationService.get(FhirUtils.referenceToId(task.getLocation().getReference()).get()));
}
if (!task.getOwner().isEmpty()) {
try {
practitionerService.get(config.getLisUserUuid());
} catch (ResourceNotFoundException e) {
labResources
.add(practitionerService.get(FhirUtils.referenceToId(task.getOwner().getReference()).get()).setActive(true));
}
}
for (IBaseResource r : labResources) {
Resource resource = (Resource) r;
Bundle.BundleEntryComponent component = transactionBundle.addEntry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirDiagnosticReportService;
import org.openmrs.module.fhir2.api.FhirObservationService;
import org.openmrs.module.fhir2.api.FhirPractitionerService;
import org.openmrs.module.fhir2.api.FhirTaskService;
import org.openmrs.module.fhir2.api.dao.FhirObservationDao;
import org.openmrs.module.fhir2.api.translators.ObservationReferenceTranslator;
Expand Down Expand Up @@ -92,6 +93,9 @@ public class FetchTaskUpdates extends AbstractTask implements ApplicationContext
@Autowired
private LabOnFhirService labOnFhirService;

@Autowired
private FhirPractitionerService practitionerService;

@Override
public void execute() {

Expand Down Expand Up @@ -126,15 +130,38 @@ public void execute() {
DateRangeParam lastUpdated = new DateRangeParam().setLowerBound(lastRequstDate).setUpperBound(currentTime);

// Get List of Tasks that belong to this instance and update them
Bundle taskBundle = client.search().forResource(Task.class)
.where(Task.IDENTIFIER.hasSystemWithAnyCode(FhirConstants.OPENMRS_FHIR_EXT_TASK_IDENTIFIER))
.where(Task.OWNER.hasId(practitionerId))
.where(Task.STATUS.exactly().codes(
TaskStatus.COMPLETED.toCode(),
TaskStatus.ACCEPTED.toCode(),
TaskStatus.REJECTED.toCode(),
TaskStatus.CANCELLED.toCode())).lastUpdated(lastUpdated)
.returnBundle(Bundle.class).execute();
Bundle taskBundle = new Bundle();
Boolean userExists = true;
try {
practitionerService.get(config.getLisUserUuid());
userExists = true;
} catch (ResourceNotFoundException e) {
userExists = false;
}
if (userExists) {
taskBundle = client.search().forResource(Task.class)
.where(Task.IDENTIFIER.hasSystemWithAnyCode(FhirConstants.OPENMRS_FHIR_EXT_TASK_IDENTIFIER))
.where(Task.OWNER.hasId(practitionerId))
.where(Task.STATUS.exactly().codes(
TaskStatus.COMPLETED.toCode(),
TaskStatus.ACCEPTED.toCode(),
TaskStatus.REJECTED.toCode(),
TaskStatus.CANCELLED.toCode()))
.lastUpdated(lastUpdated)
.returnBundle(Bundle.class).execute();

} else {
taskBundle = client.search().forResource(Task.class)
.where(Task.IDENTIFIER.hasSystemWithAnyCode(FhirConstants.OPENMRS_FHIR_EXT_TASK_IDENTIFIER))
.where(Task.STATUS.exactly().codes(
TaskStatus.COMPLETED.toCode(),
TaskStatus.ACCEPTED.toCode(),
TaskStatus.REJECTED.toCode(),
TaskStatus.CANCELLED.toCode()))
.lastUpdated(lastUpdated)
.returnBundle(Bundle.class).execute();

}

List<Bundle> taskBundles = new ArrayList<>();
taskBundles.add(taskBundle);
Expand Down Expand Up @@ -166,7 +193,7 @@ public void shutdown() {

private Boolean updateTasksInBundle(List<Bundle> taskBundles) {
Boolean tasksUpdated = false;
String commentText = "Update Order with remote fhir status :)";
String commentText = "Update Order with remote fhir status : ";
for (Bundle bundle : taskBundles) {
for (Iterator tasks = bundle.getEntry().iterator(); tasks.hasNext();) {
String openmrsTaskUuid = null;
Expand Down Expand Up @@ -201,20 +228,21 @@ private Boolean updateTasksInBundle(List<Bundle> taskBundles) {

if(openelisTask.getStatus().toString().equals(TaskStatus.REJECTED.toString()) ){
openmrsTask.setStatus(openelisTask.getStatus());
commentText = openelisTask.getStatusReason().getText().toString().isEmpty() ? commentText: openelisTask.getStatusReason().getText();
commentText = commentText + TaskStatus.REJECTED.toString();
setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.EXCEPTION, commentText);
tasksUpdated = true;
}

if(openelisTask.getStatus().toString().equals(TaskStatus.CANCELLED.toString()) ){
openmrsTask.setStatus(openelisTask.getStatus());
commentText = TaskStatus.CANCELLED.toString();
commentText = commentText + TaskStatus.CANCELLED.toString();
setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.EXCEPTION, commentText);
tasksUpdated = true;
}

if( openelisTask.getStatus().toString().equals(TaskStatus.ACCEPTED.toString()) ){
openmrsTask.setStatus(openelisTask.getStatus());
commentText = commentText + TaskStatus.ACCEPTED.toString();
setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.IN_PROGRESS, commentText);
tasksUpdated = true;
}
Expand Down

0 comments on commit 6a024d2

Please sign in to comment.