From 96d68c7191d5ee060358968757936c070d0436cd Mon Sep 17 00:00:00 2001 From: azizdiakite Date: Wed, 11 Sep 2024 10:39:31 +0300 Subject: [PATCH 1/4] Track progress of fhir task status --- .../api/scheduler/FetchTaskUpdates.java | 84 ++++++++++++++++--- 1 file changed, 72 insertions(+), 12 deletions(-) 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..718a32e 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,8 @@ 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.AdministrationService; import org.openmrs.api.OrderService; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.FhirDiagnosticReportService; @@ -119,13 +121,21 @@ public void execute() { lastRequstDate = dateFormat.format(lastRequest.getRequestDate()); } + String practitionerId = config.getLisUserUuid(); + String currentTime = dateFormat.format(newDate); 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.STATUS.exactly().code(TaskStatus.COMPLETED.toCode())).lastUpdated(lastUpdated) + .where(Task.OWNER.hasId(practitionerId)) + .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 +168,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 +183,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.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 (openelisTask.hasOutput()) { - // openmrsTask.setOutput(openelisTask.getOutput()); - taskOutPutUpdated = updateOutput(openelisTask.getOutput(), openmrsTask); + + 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 (taskOutPutUpdated) { - taskService.update(openmrsTaskUuid, openmrsTask); - tasksUpdated = taskOutPutUpdated; + + 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 +244,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 +319,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 { From df57324771197242f407c1cd9bfd1e595719240e Mon Sep 17 00:00:00 2001 From: azizdiakite Date: Wed, 11 Sep 2024 10:42:34 +0300 Subject: [PATCH 2/4] Remove unused lines --- .../module/labonfhir/api/scheduler/FetchTaskUpdates.java | 3 --- 1 file changed, 3 deletions(-) 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 718a32e..2710058 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 @@ -30,7 +30,6 @@ import org.hl7.fhir.r4.model.codesystems.TaskStatus; import org.openmrs.Order; import org.openmrs.Order.FulfillerStatus; -import org.openmrs.api.AdministrationService; import org.openmrs.api.OrderService; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.FhirDiagnosticReportService; @@ -322,10 +321,8 @@ private Boolean updateOutput(List output, Task openmrs 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 { From 5a850b3d76a46503e01a3e9bf22995c4cd550c01 Mon Sep 17 00:00:00 2001 From: azizdiakite Date: Wed, 11 Sep 2024 11:44:42 +0300 Subject: [PATCH 3/4] Remove REQUESTED status from the list --- .../openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java | 1 - 1 file changed, 1 deletion(-) 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 2710058..32c03d8 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 @@ -131,7 +131,6 @@ public void execute() { .where(Task.OWNER.hasId(practitionerId)) .where(Task.STATUS.exactly().codes( TaskStatus.COMPLETED.toCode(), - TaskStatus.REQUESTED.toCode(), TaskStatus.ACCEPTED.toCode(), TaskStatus.REJECTED.toCode(), TaskStatus.CANCELLED.toCode())).lastUpdated(lastUpdated) From 959fe4d5edb2b6cb0e5fb68f49cd539f861303b7 Mon Sep 17 00:00:00 2001 From: azizdiakite Date: Wed, 11 Sep 2024 12:12:57 +0300 Subject: [PATCH 4/4] Change RECEIVED to IN_PROGRESS while updating the status --- .../module/labonfhir/api/scheduler/FetchTaskUpdates.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 32c03d8..11542b6 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 @@ -213,9 +213,9 @@ private Boolean updateTasksInBundle(List taskBundles) { tasksUpdated = true; } - if(openelisTask.getStatus().toString().equals(TaskStatus.REQUESTED.toString()) || openelisTask.getStatus().toString().equals(TaskStatus.ACCEPTED.toString()) ){ + if( openelisTask.getStatus().toString().equals(TaskStatus.ACCEPTED.toString()) ){ openmrsTask.setStatus(openelisTask.getStatus()); - setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.RECEIVED, commentText); + setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.IN_PROGRESS, commentText); tasksUpdated = true; } }