Skip to content

Commit

Permalink
Chore: Fixes to set Task requester as Organization. and to add locati…
Browse files Browse the repository at this point in the history
…on identifier
  • Loading branch information
moshonk committed Jun 6, 2024
1 parent cde44ab commit 8c3c90d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -92,23 +90,16 @@ public Task createOrder(Order order) throws OrderCreationException {

Reference encounterRef = newReference(order.getEncounter().getUuid(), FhirConstants.ENCOUNTER);

Optional<EncounterProvider> 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 {
Expand All @@ -121,7 +112,7 @@ public Task createOrder(Order order) throws OrderCreationException {
}

private Task createTask(List<Reference> basedOnRefs, Reference forReference, Reference ownerRef,
Reference encounterRef, Reference locationRef ,List<Task.ParameterComponent> taskInputs) {
Reference encounterRef, Reference locationRef, Reference organizationRef ,List<Task.ParameterComponent> taskInputs) {
Task newTask = new Task();
newTask.setStatus(Task.TaskStatus.REQUESTED);
newTask.setIntent(Task.TaskIntent.ORDER);
Expand All @@ -130,6 +121,7 @@ private Task createTask(List<Reference> basedOnRefs, Reference forReference, Ref
newTask.setOwner(ownerRef);
newTask.setEncounter(encounterRef);
newTask.setLocation(locationRef);
newTask.setRequester(organizationRef);
if (taskInputs != null) {
newTask.setInput(taskInputs);
}
Expand Down Expand Up @@ -160,11 +152,7 @@ public Task createOrder(Encounter encounter) throws OrderCreationException {

Reference locationRef = newReference(encounter.getLocation().getUuid(), FhirConstants.LOCATION);

Optional<EncounterProvider> 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<Task.ParameterComponent> taskInputs = null;
if (config.addObsAsTaskInput()) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand All @@ -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());
Expand All @@ -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);

Expand Down

0 comments on commit 8c3c90d

Please sign in to comment.