Skip to content

Commit

Permalink
Add support for importing inventories
Browse files Browse the repository at this point in the history
  • Loading branch information
Wambere committed Mar 27, 2024
1 parent 02af958 commit 61e99a9
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 46 deletions.
8 changes: 7 additions & 1 deletion importer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@ The coverage report `coverage.html` will be at the working directory
- See example csv [here](/importer/csv/import/product.csv)
- This creates a Group resource for each product imported
- The first two columns __name__ and __active__ is the minimum required
- The last column __imageSourceUrl__ contains a url to the product image. If this source requires authentication, then you need to provide the `product_access_token` in the config file. The image is added as a binary resource and referenced in the product's Group resource
- The last column __imageSourceUrl__ contains a url to the product image. If this source requires authentication, then you need to provide the `product_access_token` in the config file. The image is added as a binary resource and referenced in the product's Group resource

### 11. Import inventories from openSRP 1
- Run `python3 main.py --csv_file csv/import/inventory.csv --setup inventories --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
2 changes: 2 additions & 0 deletions importer/csv/import/inventory.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,active,method,id,previousId,productId,type,deliveryDate,accountabilityEndDate,unicefSection,donor
sample1,true,create,8adfcfe0-41d0-4f0a-9a89-909c72fbf330,942098123,1d86d0e2-bac8-4424-90ae-e2298900ac3c,device,2021-01-09T00:00:00.000+0000,2025-01-09T00:00:00.000+0000,Health,Fund
106 changes: 106 additions & 0 deletions importer/json_payloads/inventory_group_payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"request": {
"method": "PUT",
"url": "Group/$unique_uuid",
"ifMatch": "$version"
},
"resource": {
"resourceType": "Group",
"id": "$unique_uuid",
"identifier": [
{
"use": "official",
"value": "$unique_uuid"
},
{
"use": "secondary",
"value": "$previous_id"
}
],
"active": "$active",
"type": "$type",
"code": {
"coding": [
{
"system": "http://smartregister.org/supply-inventory",
"code": "78991122",
"display": "Supply Inventory"
}
]
},
"name": "$name",
"characteristic": [
{
"code": {
"coding": [
{
"system": "http://smartregister.org/delivery-and-accountability",
"code": "09887657",
"display": "Delivery and Accountability"
}
]
},
"valuePeriod": {
"start": "$delivery_date",
"end": "$accountability_date"
}
},
{
"code": {
"coding": [
{
"system": "http://smartregister.org/unicef-section",
"code": "98734231",
"display": "Unicef Section"
}
]
},
"valueCodeableConcept": {
"coding": [
{
"system": "http://smartregister.org/unicef-section",
"code": "98734231-1",
"display": "Value entered on the unicef section"
}
],
"text": "$unicef_section"
}
},
{
"code": {
"coding": [
{
"system": "http://smartregister.org/donor",
"code": "45647484",
"display": "Donor"
}
]
},
"valueCodeableConcept": {
"coding": [
{
"system": "http://smartregister.org/donor",
"code": "45647484-1",
"display": "Value entered on the donor"
}
],
"text": "$donor"
}
},
{
"code": {
"coding": [
{
"system": "http://smartregister.org/product-reference",
"code": "33467722",
"display": "Product reference"
}
]
},
"valueReference": {
"reference": "Group/$product_id"
}
}
]
}
}
160 changes: 115 additions & 45 deletions importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,63 +484,120 @@ def delete_missing_obj_from_payload(payload_obj, position, tracker):


# custom extras for product import
def product_extras(resource, payload_string):
def group_extras(resource, payload_string, group_type):
payload_obj = json.loads(payload_string)
product_name = resource[0]
item_name = resource[0]
tracker = 0

try:
(_, active, *_, previous_id, is_attractive_item, availability, condition, appropriate_usage,
accountability_period, image_source_url) = resource
except ValueError:
logging.error("Skipping: " + product_name + " : Because of missing columns ")
active = previous_id = is_attractive_item = availability = condition = appropriate_usage = \
accountability_period = image_source_url = "missing_column"

if active and active != "missing_column":
payload_obj["resource"]["active"] = active
else:
del payload_obj["resource"]["active"]
if group_type == "product":
try:
(_, active, *_, previous_id, is_attractive_item, availability, condition, appropriate_usage,
accountability_period, image_source_url) = resource
except ValueError:
logging.error("Skipping: " + item_name + " : Because of missing columns ")
active = previous_id = is_attractive_item = availability = condition = appropriate_usage = \
accountability_period = image_source_url = "missing_column"

if active and active != "missing_column":
payload_obj["resource"]["active"] = active
else:
del payload_obj["resource"]["active"]

if previous_id and previous_id != "missing_column":
payload_obj["resource"]["identifier"][1]["value"] = previous_id
else:
del payload_obj["resource"]["identifier"][1]
if previous_id and previous_id != "missing_column":
payload_obj["resource"]["identifier"][1]["value"] = previous_id
else:
del payload_obj["resource"]["identifier"][1]

if is_attractive_item and is_attractive_item != "missing_column":
payload_obj["resource"]["characteristic"][0+tracker]["valueBoolean"] = is_attractive_item
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 0+tracker, tracker)
if is_attractive_item and is_attractive_item != "missing_column":
payload_obj["resource"]["characteristic"][0+tracker]["valueBoolean"] = is_attractive_item
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 0+tracker, tracker)

if availability and availability != "missing_column":
payload_obj["resource"]["characteristic"][1+tracker]["valueCodeableConcept"]["text"] = availability
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 1+tracker, tracker)
if availability and availability != "missing_column":
payload_obj["resource"]["characteristic"][1+tracker]["valueCodeableConcept"]["text"] = availability
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 1+tracker, tracker)

if condition and condition != "missing_column":
payload_obj["resource"]["characteristic"][2+tracker]["valueCodeableConcept"]["text"] = condition
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 2+tracker, tracker)
if condition and condition != "missing_column":
payload_obj["resource"]["characteristic"][2+tracker]["valueCodeableConcept"]["text"] = condition
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 2+tracker, tracker)

if appropriate_usage and appropriate_usage != "missing_column":
payload_obj["resource"]["characteristic"][3+tracker]["valueCodeableConcept"]["text"] = appropriate_usage
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 3+tracker, tracker)
if appropriate_usage and appropriate_usage != "missing_column":
payload_obj["resource"]["characteristic"][3+tracker]["valueCodeableConcept"]["text"] = appropriate_usage
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 3+tracker, tracker)

if accountability_period and accountability_period != "missing_column":
payload_obj["resource"]["characteristic"][4+tracker]["valueQuantity"]["value"] = accountability_period
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 4+tracker, tracker)
if accountability_period and accountability_period != "missing_column":
payload_obj["resource"]["characteristic"][4+tracker]["valueQuantity"]["value"] = accountability_period
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 4+tracker, tracker)

if image_source_url and image_source_url != "missing_column":
image_binary = save_image(image_source_url)
if image_binary != 0:
payload_obj["resource"]["characteristic"][5+tracker]["valueReference"]["reference"] = "Binary/" + image_binary
if image_source_url and image_source_url != "missing_column":
image_binary = save_image(image_source_url)
if image_binary != 0:
payload_obj["resource"]["characteristic"][5+tracker]["valueReference"]["reference"] = "Binary/" + image_binary
else:
logging.error("Unable to link the image Binary resource for product " + item_name)
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 5+tracker, tracker)
else:
logging.error("Unable to link the image Binary resource for product " + product_name)
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 5+tracker, tracker)

elif group_type == "inventory":
try:
(_, active, *_, previous_id, product_id, inventory_type, delivery_date, accountability_end_date,
unicef_section, donor) = resource
except ValueError:
logging.error("Skipping: " + item_name + " : Because of missing columns ")
active = previous_id = product_id = inventory_type = delivery_date = accountability_end_date = \
unicef_section = donor = "missing_column"

if active and active != "missing_column":
payload_obj["resource"]["active"] = active
else:
del payload_obj["resource"]["active"]

if previous_id and previous_id != "missing_column":
payload_obj["resource"]["identifier"][1]["value"] = previous_id
else:
del payload_obj["resource"]["identifier"][1]

if inventory_type and inventory_type != "missing_column":
payload_obj["resource"]["type"] = inventory_type
else:
del payload_obj["resource"]["type"]

if delivery_date and delivery_date != "missing_column":
payload_obj["resource"]["characteristic"][0+tracker]["valuePeriod"]["start"] = delivery_date
else:
payload_obj["resource"]["characteristic"][0+tracker]["valuePeriod"]["start"] = ""

if accountability_end_date and accountability_end_date != "missing_column":
payload_obj["resource"]["characteristic"][0+tracker]["valuePeriod"]["end"] = accountability_end_date
else:
payload_obj["resource"]["characteristic"][0+tracker]["valuePeriod"]["end"] = ""

if (not delivery_date and not accountability_end_date) or (
delivery_date == "missing_column" and accountability_end_date == "missing_column"):
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 0+tracker, tracker)

if unicef_section and unicef_section != "missing_column":
payload_obj["resource"]["characteristic"][1+tracker]["valueCodeableConcept"]["text"] = unicef_section
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 1+tracker, tracker)

if donor and donor != "missing_column":
payload_obj["resource"]["characteristic"][2+tracker]["valueCodeableConcept"]["text"] = donor
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 2+tracker, tracker)

if product_id and product_id != "missing_column":
payload_obj["resource"]["characteristic"][3+tracker]["valueReference"]["reference"] = "Group/" + product_id
else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 3+tracker, tracker)

else:
payload_obj, tracker = delete_missing_obj_from_payload(payload_obj, 5+tracker, tracker)
logging.info("Group type not defined")

payload_string = json.dumps(payload_obj, indent=4)
return payload_string
Expand Down Expand Up @@ -770,7 +827,13 @@ def build_payload(resource_type, resources, resource_payload_file):
elif resource_type == "careTeams":
ps = care_team_extras(resource, ps, "orgs & users")
elif resource_type == "Group":
ps = product_extras(resource, ps)
if "inventory" in resource_payload_file:
group_type = "inventory"
elif "product" in resource_payload_file:
group_type = "product"
else:
logging.error("Undefined group type")
ps = group_extras(resource, ps, group_type)

final_string = final_string + ps + ","

Expand Down Expand Up @@ -1386,7 +1449,14 @@ def main(
"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(final_response)
logging.info("Inventory importing process complete")
else:
logging.error("Unsupported request!")
else:
Expand Down

0 comments on commit 61e99a9

Please sign in to comment.