Skip to content

Commit

Permalink
Modify List creation to work for inventories too
Browse files Browse the repository at this point in the history
  • Loading branch information
Wambere committed Jul 25, 2024
1 parent 2311132 commit e1f00f6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
4 changes: 3 additions & 1 deletion importer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ The coverage report `coverage.html` will be at the working directory
- You can pass in a `list_resource_id` to be used as the identifier for the List resource, or you can leave it empty and a random uuid will be generated

### 11. Import inventories from openSRP 1
- Run `python3 main.py --csv_file csv/import/inventory.csv --setup inventories --log_level info`
- Run `python3 main.py --csv_file csv/import/inventory.csv --setup inventories --list_resource_id 123 --log_level info`
- See example csv [here](/importer/csv/import/inventory.csv)
- This creates a Group resource for each inventory imported
- The first two columns __name__ and __active__ is the minimum required
- Adding a value to the Location column will create a separate List resource (or update) that links the inventory to the provided location resource
- A separate List resource with references to all the Group and List resources generated is also created
- You can pass in a `list_resource_id` to be used as the identifier for the (reference) List resource, or you can leave it empty and a random uuid will be generated

### 12. Import JSON resources from file
- Run `python3 main.py --bulk_import True --json_file tests/fhir_sample.json --chunk_size 500000 --sync sort --resources_count 100 --log_level info`
Expand Down
42 changes: 30 additions & 12 deletions importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,17 @@ def build_payload(resource_type, resources, resource_payload_file, created_resou
return final_string


def build_group_list_resource(list_resource_id: str, csv_file: str, full_list_created_resources: list, title: str):
if not list_resource_id:
list_resource_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, csv_file))
current_version = get_resource(list_resource_id, "List")
method = "create" if current_version == str(0) else "update"
resource = [[title, "current", method, list_resource_id]]
result_payload = build_payload(
"List", resource, "json_payloads/group_list_payload.json")
return process_resources_list(result_payload, full_list_created_resources)


# This function takes a 'created_resources' array and a response string
# It converts the response string to a json object, then loops through the entry array
# extracting all the referenced resources and adds them to the created_resources array
Expand All @@ -1107,7 +1118,8 @@ def extract_resources(created_resources, response_string):
entry = json_response["entry"]
for item in entry:
resource = item["response"]["location"]
created_resources.append(resource[0:42])
index = resource.find("/", resource.find("/") + 1)
created_resources.append(resource[0:index])
return created_resources


Expand Down Expand Up @@ -1988,16 +2000,8 @@ def main(

if product_creation_response.status_code == 200:
full_list_created_resources = extract_resources(created_resources, product_creation_response.text)
if not list_resource_id:
list_resource_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, csv_file))

current_version = get_resource(list_resource_id, "List")
method = "create" if current_version == str(0) else "update"
resource = [["Supply Inventory List", "current", method, list_resource_id]]
result_payload = build_payload(
"List", resource, "json_payloads/group_list_payload.json")

list_payload = process_resources_list(result_payload, full_list_created_resources)
list_payload = build_group_list_resource(
list_resource_id, csv_file, full_list_created_resources, "Supply Inventory List")
final_response = handle_request("POST", "", config.fhir_base_url, list_payload)
logging.info("Processing complete!")
else:
Expand All @@ -2007,13 +2011,27 @@ def main(
json_payload = build_payload(
"Group", resource_list, "json_payloads/inventory_group_payload.json"
)
final_response = handle_request("POST", json_payload, config.fhir_base_url)
inventory_creation_response = handle_request("POST", json_payload, config.fhir_base_url)
groups_created = []
if inventory_creation_response.status_code == 200:
groups_created = extract_resources(groups_created, inventory_creation_response.text)

lists_created = []
link_payload = link_to_location(resource_list)
if len(link_payload) > 0:
link_response = handle_request(
"POST", link_payload, config.fhir_base_url
)
if link_response.status_code == 200:
lists_created = extract_resources(lists_created, link_response.text)
logging.info(link_response.text)

full_list_created_resources = groups_created + lists_created
if len(full_list_created_resources) > 0:
list_payload = build_group_list_resource(
list_resource_id, csv_file, full_list_created_resources, "Supply Chain commodities")
final_response = handle_request("POST", "", config.fhir_base_url, list_payload)
logging.info("Processing complete!")
else:
logging.error("Unsupported request!")
else:
Expand Down

0 comments on commit e1f00f6

Please sign in to comment.