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..586132d 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 @@ -4,7 +4,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @@ -17,7 +16,6 @@ import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.Task; import org.openmrs.Encounter; -import org.openmrs.EncounterProvider; import org.openmrs.Obs; import org.openmrs.Order; import org.openmrs.api.db.DAOException; @@ -92,23 +90,16 @@ public Task createOrder(Order order) throws OrderCreationException { Reference encounterRef = newReference(order.getEncounter().getUuid(), FhirConstants.ENCOUNTER); - Optional requesterProvider = order.getEncounter().getActiveEncounterProviders().stream() - .findFirst(); + Reference requesterRef = newReference(order.getEncounter().getLocation().getUuid(), FhirConstants.ORGANIZATION); - Reference requesterRef = requesterProvider.map( - encounterProvider -> newReference(encounterProvider.getUuid(), FhirConstants.PRACTITIONER)).orElse(null); - - Reference locationRef = null; - if (order.getEncounter().getLocation() != null) { - locationRef = newReference(order.getEncounter().getLocation().getUuid(), FhirConstants.LOCATION); - } + // Reference locationRef = newReference(order.getEncounter().getLocation().getUuid(), FhirConstants.LOCATION); // Create Task Resource for given Order - Task newTask = createTask(basedOnRefs, forReference, ownerRef, encounterRef, locationRef ,taskInputs); + Task newTask = createTask(basedOnRefs, forReference, null, encounterRef, null,requesterRef ,taskInputs); - if (order.getEncounter().getActiveEncounterProviders().isEmpty()) { - newTask.setRequester(requesterRef); - } +// if (order.getEncounter().getLocation() != null) { +// newTask.setRequester(requesterRef); +// } // Save the new Task Resource try { @@ -121,7 +112,7 @@ public Task createOrder(Order order) throws OrderCreationException { } private Task createTask(List basedOnRefs, Reference forReference, Reference ownerRef, - Reference encounterRef, Reference locationRef ,List taskInputs) { + Reference encounterRef, Reference locationRef, Reference organizationRef ,List taskInputs) { Task newTask = new Task(); newTask.setStatus(Task.TaskStatus.REQUESTED); newTask.setIntent(Task.TaskIntent.ORDER); @@ -130,6 +121,7 @@ private Task createTask(List basedOnRefs, Reference forReference, Ref newTask.setOwner(ownerRef); newTask.setEncounter(encounterRef); newTask.setLocation(locationRef); + newTask.setRequester(organizationRef); if (taskInputs != null) { newTask.setInput(taskInputs); } @@ -160,11 +152,7 @@ public Task createOrder(Encounter encounter) throws OrderCreationException { Reference locationRef = newReference(encounter.getLocation().getUuid(), FhirConstants.LOCATION); - Optional requesterProvider = encounter.getActiveEncounterProviders().stream().findFirst(); - - Reference requesterRef = requesterProvider.isPresent() ? - newReference(requesterProvider.get().getUuid(), FhirConstants.PRACTITIONER) : - null; + Reference requesterRef = newReference(encounter.getLocation().getUuid(), FhirConstants.ORGANIZATION); List taskInputs = null; if (config.addObsAsTaskInput()) { @@ -179,12 +167,20 @@ public Task createOrder(Encounter encounter) throws OrderCreationException { } // Create Task Resource for given Order - Task newTask = createTask(basedOnRefs, forReference, ownerRef, encounterRef, locationRef ,taskInputs); - newTask.setLocation(locationRef); - - if (!encounter.getActiveEncounterProviders().isEmpty()) { - newTask.setRequester(requesterRef); - } + // ========================================= + // Summary of Payload requirements in terms of location and organization: + // Payload coming from OpenMRS should include only the Location and Organization resources corresponding to the MFL-based location that represents the physical spot where a lab order is being collected. This MFL code will then be mapped to the proper IPMS location (in some cases based on the type of test being ordered as well) + // The incoming Location should reference the Organization as a managing organization, just like a normal Org/Location pair + // For the incoming task resource: + // `for` should reference the patient + // `requester` should reference the Organization + // `owner` and `location` can be left blank + // Once the task is processed, owner will reference the mapped IPMS Organization, and location the IPMS location. + // Leave the `location ref` and `owner ref` blank. Once the task is processed, owner will reference the mapped Lab Systems Organization, and location the Lab System's location. + Task newTask = createTask(basedOnRefs, forReference, null, encounterRef, null, requesterRef, taskInputs); + //newTask.setLocation(locationRef); + + //newTask.setRequester(requesterRef); // Save the new Task Resource try { 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 e0de003..5ebb004 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 @@ -13,7 +13,7 @@ import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.param.TokenParam; import org.hl7.fhir.r4.model.Location; - +import org.hl7.fhir.r4.model.Organization; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Identifier; @@ -101,6 +101,7 @@ public Bundle createLabBundle(Task task) { includes.add(new Include("Task:encounter")); includes.add(new Include("Task:based-on")); includes.add(new Include("Task:location")); + includes.add(new Include("Task:requester")); IBundleProvider labBundle = fhirTaskService.searchForTasks(new TaskSearchParams(null, null, null, uuid, null, null, includes)); @@ -111,14 +112,19 @@ public Bundle createLabBundle(Task task) { labResources.add(fhirLocationService.get(FhirUtils.referenceToId(task.getLocation().getReference()).get())); } + if (!task.getRequester().isEmpty()) { + labResources.add(fhirLocationService.get(FhirUtils.referenceToId(task.getRequester().getReference()).get())); + } + for (IBaseResource r : labResources) { Resource resource = (Resource) r; // if resource is location, add MFL Code as location identifier // TODO: Short term: Make the MFL Location attribute type to be a Global config variable - // TODO: Long term: Make the location translator pick enrich the MFL Code as appropriate + // TODO: Long term: Make the location translator pick and enrich the MFL Code as appropriate - if (resource instanceof Location) { + Bundle.BundleEntryComponent component = transactionBundle.addEntry(); + if (resource instanceof Location || resource instanceof Organization) { Location location = (Location) resource; if (location != null) { org.openmrs.Location openmrsLocation = locationService.getLocationByUuid(location.getId()); @@ -128,19 +134,20 @@ public Bundle createLabBundle(Task task) { if (!locationAttributeTypes.isEmpty()) { locationAttributeTypes.stream().filter(locationAttribute -> locationAttribute.getAttributeType().equals(mflLocationAttributeType)).findFirst().ifPresent(locationAttribute -> { String mflCode = (String) locationAttribute.getValue(); - + Identifier mflIdentifier = new Identifier(); mflIdentifier.setSystem(MFL_LOCATION_IDENTIFIER_URI); mflIdentifier.setValue(mflCode); location.addIdentifier(mflIdentifier); + component.setResource(location); }); } } } + } else { + component.setResource(resource); } - Bundle.BundleEntryComponent component = transactionBundle.addEntry(); - component.setResource(resource); component.getRequest().setUrl(resource.fhirType() + "/" + resource.getIdElement().getIdPart()) .setMethod(Bundle.HTTPVerb.PUT);