From 6a024d2f7855adf457dd5ee04aac886c61d215f1 Mon Sep 17 00:00:00 2001 From: Mutesasira Moses Date: Thu, 12 Sep 2024 18:15:00 +0300 Subject: [PATCH] Associate a default Practitioner to the Task when openelisuser is not defined (#51) * upgrade fhir2 version * Task should use a dynamic practitioner if OpenLIS user is not defined * fix NPE * fix preactitioner user --- .../module/labonfhir/api/LabOrderHandler.java | 34 ++++++++++-- .../api/event/LabCreationListener.java | 14 +++++ .../api/scheduler/FetchTaskUpdates.java | 52 ++++++++++++++----- 3 files changed, 84 insertions(+), 16 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/LabOrderHandler.java b/api/src/main/java/org/openmrs/module/labonfhir/api/LabOrderHandler.java index cfbd8a2..b968e4d 100644 --- a/api/src/main/java/org/openmrs/module/labonfhir/api/LabOrderHandler.java +++ b/api/src/main/java/org/openmrs/module/labonfhir/api/LabOrderHandler.java @@ -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 { @@ -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 @@ -95,8 +101,19 @@ public Task createOrder(Order order) throws OrderCreationException { Optional 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) { @@ -163,9 +180,18 @@ public Task createOrder(Encounter encounter) throws OrderCreationException { Optional 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 taskInputs = null; if (config.addObsAsTaskInput()) { taskInputs = new ArrayList<>(); diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java b/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java index fbcdc9f..598ff42 100644 --- a/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java +++ b/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java @@ -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; @@ -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; @@ -55,6 +58,9 @@ public abstract class LabCreationListener implements EventListener { @Autowired private LabOnFhirService labOnFhirService ; + @Autowired + private FhirPractitionerService practitionerService; + public DaemonToken getDaemonToken() { return daemonToken; } @@ -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(); diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java b/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java index 11542b6..da427ac 100644 --- a/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java +++ b/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java @@ -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; @@ -92,6 +93,9 @@ public class FetchTaskUpdates extends AbstractTask implements ApplicationContext @Autowired private LabOnFhirService labOnFhirService; + @Autowired + private FhirPractitionerService practitionerService; + @Override public void execute() { @@ -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 taskBundles = new ArrayList<>(); taskBundles.add(taskBundle); @@ -166,7 +193,7 @@ public void shutdown() { private Boolean updateTasksInBundle(List 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; @@ -201,20 +228,21 @@ private Boolean updateTasksInBundle(List 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; }