From 99fa446f14fa5dc5f664656ef57e33d2bb541389 Mon Sep 17 00:00:00 2001 From: Wambere Date: Mon, 22 Apr 2024 11:35:00 +0300 Subject: [PATCH] Update inventory csv and code --- importer/csv/import/inventory.csv | 2 +- importer/main.py | 56 +++++++++++++++++++------------ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/importer/csv/import/inventory.csv b/importer/csv/import/inventory.csv index 5e11ed01..7e5c08af 100644 --- a/importer/csv/import/inventory.csv +++ b/importer/csv/import/inventory.csv @@ -1,2 +1,2 @@ name,active,method,id,poNumber,serialNumber,usualId,actual,productId,deliveryDate,accountabilityDate,quantity,unicefSection,donor -Bishop Magua - Bed nets,true,create,8adfcfe0-41d0-4f0a-9a89-909c72fbf330,123523,989682,a065c211-cf3e-4b5b-972f-fdac0e45fef7,false,1d86d0e2-bac8-4424-90ae-e2298900ac3c,2021-01-09T00:00:00.000+0000,2025-01-09T00:00:00.000+0000,34,Health,GAVI +Bishop Magua - Bed nets,true,create,8adfcfe0-41d0-4f0a-9a89-909c72fbf330,123523,989682,a065c211-cf3e-4b5b-972f-fdac0e45fef7,false,1d86d0e2-bac8-4424-90ae-e2298900ac3c,2024-02-01T00:00:00.00Z,2025-02-01T00:00:00.00Z,34,Health,GAVI \ No newline at end of file diff --git a/importer/main.py b/importer/main.py index f13d0709..39767926 100644 --- a/importer/main.py +++ b/importer/main.py @@ -534,35 +534,52 @@ def group_extras(resource, payload_string, group_type): del payload_obj["resource"]["characteristic"][x] elif group_type == "inventory": - (_, active, *_, previous_id, product_id, inventory_type, delivery_date, accountability_end_date, - unicef_section, donor) = resource + (_, active, *_, po_number, serial_number, usual_id, actual, product_id, delivery_date, + accountability_date, quantity, unicef_section, donor) = resource if active: - payload_obj["resource"]["active"] = active + payload_obj["resource"]["active"] = bool(active) else: del payload_obj["resource"]["active"] - if previous_id: - payload_obj["resource"]["identifier"][1]["value"] = previous_id + if serial_number: + payload_obj["resource"]["identifier"][0]["value"] = serial_number + else: + del payload_obj["resource"]["identifier"][0] + + if po_number: + payload_obj["resource"]["identifier"][1]["value"] = po_number else: del payload_obj["resource"]["identifier"][1] - if inventory_type: - payload_obj["resource"]["type"] = inventory_type + if usual_id: + payload_obj["resource"]["identifier"][2]["value"] = usual_id + else: + del payload_obj["resource"]["identifier"][2] + + if actual: + payload_obj["resource"]["actual"] = bool(actual) + else: + del payload_obj["resource"]["actual"] + + if product_id: + payload_obj["resource"]["member"][0]["entity"]["reference"] = "Group/" + product_id else: - del payload_obj["resource"]["type"] + payload_obj["resource"]["member"][0]["entity"]["reference"] = "Group/" if delivery_date: - payload_obj["resource"]["characteristic"][0]["valuePeriod"]["start"] = delivery_date + payload_obj["resource"]["member"][0]["period"]["start"] = delivery_date else: - payload_obj["resource"]["characteristic"][0]["valuePeriod"]["start"] = "" + payload_obj["resource"]["member"][0]["period"]["start"] = "" - if accountability_end_date: - payload_obj["resource"]["characteristic"][0]["valuePeriod"]["end"] = accountability_end_date + if accountability_date: + payload_obj["resource"]["member"][0]["period"]["end"] = accountability_date else: - payload_obj["resource"]["characteristic"][0]["valuePeriod"]["end"] = "" + payload_obj["resource"]["member"][0]["period"]["end"] = "" - if not delivery_date and not accountability_end_date: + if quantity: + payload_obj["resource"]["characteristic"][0]["valueQuantity"]["value"] = int(quantity) + else: del_indexes.append(0) if unicef_section: @@ -575,11 +592,6 @@ def group_extras(resource, payload_string, group_type): else: del_indexes.append(2) - if product_id: - payload_obj["resource"]["characteristic"][3]["valueReference"]["reference"] = "Group/" + product_id - else: - del_indexes.append(3) - for x in reversed(del_indexes): del payload_obj["resource"]["characteristic"][x] @@ -733,7 +745,8 @@ def get_valid_resource_type(resource_type): # This function gets the current resource version from the API def get_resource(resource_id, resource_type): - resource_type = get_valid_resource_type(resource_type) + if resource_type != "Group": + resource_type = get_valid_resource_type(resource_type) resource_url = "/".join([config.fhir_base_url, resource_type, resource_id]) response = handle_request("GET", "", resource_url) return json.loads(response[0])["meta"]["versionId"] if response[1] == 200 else "0" @@ -781,6 +794,7 @@ def build_payload(resource_type, resources, resource_payload_file): if method == "update": if id: version = get_resource(id, resource_type) + if version != "0": unique_uuid = identifier_uuid = id else: @@ -1431,14 +1445,12 @@ def main( json_payload = build_payload( "Group", resource_list, "json_payloads/product_group_payload.json") final_response = handle_request("POST", json_payload, config.fhir_base_url) - logging.info("Product importing process complete") elif setup == "inventories": logging.info("Importing inventories as FHIR Group resources") json_payload = build_payload( "Group", resource_list, "json_payloads/inventory_group_payload.json" ) final_response = handle_request("POST", json_payload, config.fhir_base_url) - logging.info("Inventory importing process complete") else: logging.error("Unsupported request!") else: