From 084137fa5812346ccaf5f5c83473d98ee5929297 Mon Sep 17 00:00:00 2001 From: aziz Date: Mon, 6 May 2024 11:12:49 +0000 Subject: [PATCH] Get Reject and cancel status form fhir --- api/pom.xml | 3 +- .../api/scheduler/FetchTaskUpdates.java | 80 ++++++++++++++++--- omod/pom.xml | 2 +- pom.xml | 4 +- 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index 49410d3..983cdcf 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -15,7 +15,7 @@ org.openmrs.module labonfhir - 1.4.0-SNAPSHOT + 1.5.1-SNAPSHOT labonfhir-api @@ -27,6 +27,7 @@ org.openmrs.api openmrs-api + 2.6.1 org.openmrs 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 dcd14b4..3667cc1 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 @@ -29,6 +29,7 @@ import org.hl7.fhir.r4.model.Task; import org.hl7.fhir.r4.model.codesystems.TaskStatus; import org.openmrs.Order; +import org.openmrs.Order.FulfillerStatus; import org.openmrs.api.OrderService; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.FhirDiagnosticReportService; @@ -125,7 +126,12 @@ public void execute() { // 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.STATUS.exactly().code(TaskStatus.COMPLETED.toCode())).lastUpdated(lastUpdated) + .where(Task.STATUS.exactly().codes( + TaskStatus.COMPLETED.toCode(), + TaskStatus.REQUESTED.toCode(), + TaskStatus.ACCEPTED.toCode(), + TaskStatus.REJECTED.toCode(), + TaskStatus.CANCELLED.toCode())).lastUpdated(lastUpdated) .returnBundle(Bundle.class).execute(); List taskBundles = new ArrayList<>(); @@ -158,6 +164,7 @@ public void shutdown() { private Boolean updateTasksInBundle(List taskBundles) { Boolean tasksUpdated = false; + String commentText = "Update Order with remote fhir status :)"; for (Bundle bundle : taskBundles) { for (Iterator tasks = bundle.getEntry().iterator(); tasks.hasNext();) { String openmrsTaskUuid = null; @@ -172,19 +179,42 @@ private Boolean updateTasksInBundle(List taskBundles) { // Only update if matching OpenMRS Task found if (openmrsTask != null) { // Handle status - openmrsTask.setStatus(openelisTask.getStatus()); + if (openelisTask.getStatus().toString().equals(TaskStatus.COMPLETED.toString())) { + + openmrsTask.setStatus(openelisTask.getStatus()); - Boolean taskOutPutUpdated = false; - if(openmrsTask.hasBasedOn()){ - setOrderNumberFromLIS(openmrsTask.getBasedOn()); + Boolean taskOutPutUpdated = false; + if(openmrsTask.hasBasedOn()){ + setOrderNumberFromLIS(openmrsTask.getBasedOn()); + } + if (openelisTask.hasOutput()) { + // openmrsTask.setOutput(openelisTask.getOutput()); + taskOutPutUpdated = updateOutput(openelisTask.getOutput(), openmrsTask); + } + if (taskOutPutUpdated) { + taskService.update(openmrsTaskUuid, openmrsTask); + tasksUpdated = taskOutPutUpdated; + } } - if (openelisTask.hasOutput()) { - // openmrsTask.setOutput(openelisTask.getOutput()); - taskOutPutUpdated = updateOutput(openelisTask.getOutput(), openmrsTask); + + if(openelisTask.getStatus().toString().equals(TaskStatus.REJECTED.toString()) ){ + openmrsTask.setStatus(openelisTask.getStatus()); + commentText = openelisTask.getStatusReason().getText().toString().isEmpty() ? commentText: openelisTask.getStatusReason().getText(); + setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.EXCEPTION, commentText); + tasksUpdated = true; } - if (taskOutPutUpdated) { - taskService.update(openmrsTaskUuid, openmrsTask); - tasksUpdated = taskOutPutUpdated; + + if(openelisTask.getStatus().toString().equals(TaskStatus.CANCELLED.toString()) ){ + openmrsTask.setStatus(openelisTask.getStatus()); + commentText = TaskStatus.CANCELLED.toString(); + setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.EXCEPTION, commentText); + tasksUpdated = true; + } + + if(openelisTask.getStatus().toString().equals(TaskStatus.REQUESTED.toString()) || openelisTask.getStatus().toString().equals(TaskStatus.ACCEPTED.toString()) ){ + openmrsTask.setStatus(openelisTask.getStatus()); + setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.RECEIVED, commentText); + tasksUpdated = true; } } } @@ -210,7 +240,7 @@ private void setOrderNumberFromLIS(List basedOn) { if (order != null) { String commentText = "Update Order with Accesion Number From LIS"; String accessionNumber = serviceRequest.getRequisition().getValue(); - orderService.updateOrderFulfillerStatus(order, Order.FulfillerStatus.IN_PROGRESS, + orderService.updateOrderFulfillerStatus(order, Order.FulfillerStatus.COMPLETED, commentText, accessionNumber); } } @@ -285,6 +315,32 @@ private Boolean updateOutput(List output, Task openmrs } return taskOutPutUpdated; } + private void setOrderStatus(List basedOn, String string, FulfillerStatus fulfillerStatus, String commentText) { + basedOn.forEach(ref -> { + if (ref.hasReferenceElement()) { + System.out.println("concidtion: 1"); + IIdType referenceElement = ref.getReferenceElement(); + if ("ServiceRequest".equals(referenceElement.getResourceType())) { + System.out.println("concidtion: 2"); + String serviceRequestUuid = referenceElement.getIdPart(); + try { + + Order order = orderService.getOrderByUuid(serviceRequestUuid); + if (order != null) { + String accessionNumber = ""; + orderService.updateOrderFulfillerStatus(order, fulfillerStatus, + commentText, accessionNumber); + } + + } + catch (ResourceNotFoundException e) { + log.error( + "Could not Fetch ServiceRequest/" + serviceRequestUuid + ":" + e.toString() + getStackTrace(e)); + } + } + } + }); + } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { diff --git a/omod/pom.xml b/omod/pom.xml index 750b938..da2ea08 100644 --- a/omod/pom.xml +++ b/omod/pom.xml @@ -3,7 +3,7 @@ org.openmrs.module labonfhir - 1.4.0-SNAPSHOT + 1.5.1-SNAPSHOT labonfhir-omod diff --git a/pom.xml b/pom.xml index e8519eb..450b7ab 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ org.openmrs.module labonfhir - 1.4.0-SNAPSHOT + 1.5.1-SNAPSHOT pom Lab on FHIR A module to support lab order communication between OpenMRS and an LIS like OpenELIS using FHIR-based @@ -44,7 +44,7 @@ org.openmrs.api openmrs-api - ${openmrsPlatformVersion} + 2.6.1 provided