@@ -679,7 +679,7 @@ def get_valid_resource_type(resource_type):
679
679
680
680
# This function gets the current resource version from the API
681
681
def get_resource (resource_id , resource_type ):
682
- if resource_type not in ["List" , "Group" ]:
682
+ if resource_type not in ["List" , "Group" , "Encounter" , "Location" ]:
683
683
resource_type = get_valid_resource_type (resource_type )
684
684
resource_url = "/" .join ([fhir_base_url , resource_type , resource_id ])
685
685
response = handle_request ("GET" , "" , resource_url )
@@ -1150,3 +1150,192 @@ def build_report(csv_file, response, error_details, fail_count, fail_all):
1150
1150
logging .info (string_report )
1151
1151
logging .info ("============================================================" )
1152
1152
logging .info ("============================================================" )
1153
+
1154
+
1155
+ def build_single_resource (
1156
+ resource_type , resource_id , practitioner_id , period_start , location_id , form_encounter ,
1157
+ visit_encounter , subject , value_string = "" , note = "" ,
1158
+ ):
1159
+ template_map = {
1160
+ "visit" : "visit_encounter_payload.json" ,
1161
+ "flag" : "flag_payload.json" ,
1162
+ "encounter" : "flag_encounter_payload.json" ,
1163
+ "observation" : "flag_observation_payload.json" ,
1164
+ }
1165
+ json_template = next (
1166
+ (template for key , template in template_map .items () if key in resource_type ),
1167
+ None ,
1168
+ )
1169
+
1170
+ boolean_code = boolean_value = ""
1171
+ if "product" in resource_type :
1172
+ code = "PRODCHECK"
1173
+ display = text = "Product Check"
1174
+ c_code = "issue_details"
1175
+ c_display = c_text = value_string
1176
+ elif "service_point_check" in resource_type :
1177
+ code = "SPCHECK"
1178
+ display = text = "Service Point Check"
1179
+ c_code = "34657579"
1180
+ c_display = c_text = "Service Point Good Order Check"
1181
+ boolean_code = "373067005"
1182
+ boolean_value = "No (qualifier value)"
1183
+ elif "consult_beneficiaries" in resource_type :
1184
+ code = "CNBEN"
1185
+ display = text = "Consult Beneficiaries Visit"
1186
+ c_code = "77223346"
1187
+ c_display = c_text = "Consult Beneficiaries"
1188
+ boolean_code = "373066001"
1189
+ boolean_value = "Yes (qualifier value)"
1190
+ elif "warehouse_check" in resource_type :
1191
+ code = "WHCHECK"
1192
+ display = text = "Warehouse check Visit"
1193
+ c_code = "68561322"
1194
+ c_display = c_text = "Required action"
1195
+ boolean_code = "373066001"
1196
+ boolean_value = "Yes (qualifier value)"
1197
+ else :
1198
+ code = display = text = c_code = c_display = c_text = ""
1199
+
1200
+ with open (json_path + json_template ) as json_file :
1201
+ resource_payload = json_file .read ()
1202
+
1203
+ visit_encounter_vars = {
1204
+ "$id" : resource_id ,
1205
+ "$version" : "1" ,
1206
+ "$category_code" : code ,
1207
+ "$category_display" : display ,
1208
+ "$category_text" : text ,
1209
+ "$code_code" : c_code ,
1210
+ "$code_display" : c_display ,
1211
+ "$code_text" : c_text ,
1212
+ "$practitioner_id" : practitioner_id ,
1213
+ "$start" : period_start .replace (" " , "T" ),
1214
+ "$end" : period_start .replace (" " , "T" ),
1215
+ "$subject" : subject ,
1216
+ "$location" : location_id ,
1217
+ "$form_encounter" : form_encounter ,
1218
+ "$visit_encounter" : visit_encounter ,
1219
+ "$value_string" : value_string ,
1220
+ "$boolean_code" : boolean_code ,
1221
+ "$boolean_value" : boolean_value ,
1222
+ "$note" : note ,
1223
+ }
1224
+ for var , value in visit_encounter_vars .items ():
1225
+ resource_payload = resource_payload .replace (var , value )
1226
+
1227
+ obj = json .loads (resource_payload )
1228
+ if "product_observation" in resource_type :
1229
+ del obj ["resource" ]["valueCodeableConcept" ]
1230
+ del obj ["resource" ]["note" ]
1231
+ if (
1232
+ "service_point_check_encounter" in resource_type
1233
+ or "consult_beneficiaries_encounter" in resource_type
1234
+ ):
1235
+ del obj ["resource" ]["subject" ]
1236
+ if (
1237
+ "service_point_check_observation" in resource_type
1238
+ or "consult_beneficiaries_observation" in resource_type
1239
+ ):
1240
+ del obj ["resource" ]["focus" ]
1241
+ del obj ["resource" ]["valueString" ]
1242
+ resource_payload = json .dumps (obj , indent = 4 )
1243
+
1244
+ return resource_payload
1245
+
1246
+
1247
+ def build_resources (
1248
+ resource_type , encounter_id , flag_id , observation_id , practitioner_id , period , location ,
1249
+ visit_encounter , subject , value_string = "" , note = "" ,
1250
+ ):
1251
+ encounter = build_single_resource (
1252
+ resource_type + "_encounter" , encounter_id , practitioner_id , period , location , "" ,
1253
+ visit_encounter , subject ,
1254
+ )
1255
+ flag = build_single_resource (
1256
+ resource_type + "_flag" , flag_id , practitioner_id , period , location , encounter_id ,
1257
+ "" , subject ,
1258
+ )
1259
+ observation = build_single_resource (
1260
+ resource_type + "_observation" , observation_id , practitioner_id , period , location , encounter_id ,
1261
+ "" , subject , value_string , note ,
1262
+ )
1263
+
1264
+ resources = encounter + "," + flag + "," + observation + ","
1265
+ return resources
1266
+
1267
+
1268
+ def check_location (location_id , locations_list ):
1269
+ if location_id in locations_list :
1270
+ return locations_list [location_id ]
1271
+
1272
+ check = get_resource (location_id , "Location" )
1273
+ if check != "0" :
1274
+ locations_list [location_id ] = True
1275
+ return True
1276
+ else :
1277
+ locations_list [location_id ] = False
1278
+ logging .info ("-- Skipping location, it does NOT EXIST " + location_id )
1279
+ return False
1280
+
1281
+
1282
+ def build_flag_payload (resources , practitioner_id , visit_encounter ):
1283
+ initial_string = """{"resourceType": "Bundle","type": "transaction","entry": [ """
1284
+ final_string = ""
1285
+ locations = {}
1286
+ for resource in resources :
1287
+ flag_id = str (uuid .uuid5 (uuid .NAMESPACE_DNS , "flag" + resource [2 ]))
1288
+ observation_id = str (uuid .uuid5 (uuid .NAMESPACE_DNS , "observation" + resource [2 ]))
1289
+
1290
+ sub_list = []
1291
+ valid_location = check_location (resource [4 ], locations )
1292
+ if valid_location :
1293
+ if resource [3 ] == "service_point" :
1294
+ if "no" in resource [15 ]:
1295
+ note = (
1296
+ resource [16 ].replace ('"' , "" ).replace ("[" , "" ).replace ("]" , "" )
1297
+ )
1298
+ sub_list = build_resources (
1299
+ "service_point_check" , resource [2 ], flag_id , observation_id , practitioner_id ,
1300
+ resource [1 ], resource [4 ], visit_encounter , "Location/" + resource [4 ], "" , note ,
1301
+ )
1302
+ if "yes" in resource [17 ]:
1303
+ note = (
1304
+ resource [18 ].replace ('"' , "" ).replace ("[" , "" ).replace ("]" , "" )
1305
+ )
1306
+ sub_list = build_resources (
1307
+ "consult_beneficiaries" , resource [2 ], flag_id , observation_id , practitioner_id ,
1308
+ resource [1 ], resource [4 ], visit_encounter , "Location/" + resource [4 ], "" , note ,
1309
+ )
1310
+ if "yes" in resource [19 ]:
1311
+ note = (
1312
+ resource [20 ].replace ('"' , "" ).replace ("[" , "" ).replace ("]" , "" )
1313
+ )
1314
+ sub_list = build_resources (
1315
+ "warehouse" , resource [2 ], flag_id , observation_id , practitioner_id , resource [1 ],
1316
+ resource [4 ], visit_encounter , "Location/" + resource [4 ], "" , note ,
1317
+ )
1318
+
1319
+ elif resource [3 ] == "product" :
1320
+ if resource [8 ]:
1321
+ product_info = [
1322
+ resource [8 ], resource [9 ], resource [10 ], resource [11 ], resource [12 ], resource [13 ], resource [14 ],
1323
+ ]
1324
+ value_string = " | " .join (filter (None , product_info ))
1325
+ value_string = value_string .replace ('"' , "" )
1326
+ if resource [6 ]:
1327
+ sub_list = build_resources (
1328
+ "product" , resource [2 ], flag_id , observation_id , practitioner_id ,
1329
+ resource [1 ], resource [4 ], visit_encounter , "Group/" + resource [6 ], value_string ,
1330
+ )
1331
+ else :
1332
+ logging .info ("-- Missing Group, skipping resource: " + str (resource ))
1333
+ else :
1334
+ logging .info ("-- This entityType is not supported" )
1335
+
1336
+ if len (sub_list ) < 1 :
1337
+ sub_list = ""
1338
+
1339
+ final_string = final_string + sub_list
1340
+ final_string = initial_string + final_string [:- 1 ] + " ] } "
1341
+ return final_string
0 commit comments