@@ -62,7 +62,7 @@ def dispatch_cr(self, cr: CommunicationRequest):
6262
6363 target_phone = resolve_reference (cr .recipient [0 ].reference ).get_phone_number ()
6464 patient = resolve_reference (cr .recipient [0 ].reference )
65- # Create a Communication attempt
65+ # Create a new Communication attempt
6666 c = cr .create_communication_from_request (status = "in-progress" )
6767 c = Communication (c )
6868 try :
@@ -75,10 +75,10 @@ def dispatch_cr(self, cr: CommunicationRequest):
7575 content = cr .payload [0 ].contentString ,
7676 patient = patient ,
7777 practitioner = practitioner )
78- c . persist ( )
78+ resulting_communication = HAPI_request ( 'POST' , 'Communication' , resource = c . as_json () )
7979 audit_entry (
8080 f"Created Communication resource for the outgoing text" ,
81- extra = {"resource" : c },
81+ extra = {"resource" : resulting_communication },
8282 level = 'debug'
8383 )
8484 result = self .send_twilio_sms (message = expanded_payload , to_phone = target_phone )
@@ -194,7 +194,7 @@ def generate_incoming_message(self, message, time: datetime = None, patient: Pat
194194 ]
195195 }
196196 c = Communication (m )
197- result = c . persist ( )
197+ result = HAPI_request ( 'POST' , 'Communication' , resource = c . as_json () )
198198 audit_entry (
199199 f"Created Communication resource for incoming text" ,
200200 extra = {"resource" : result },
@@ -246,10 +246,10 @@ def on_twilio_message_status_update(self, values):
246246 # Callback only occurs on completed Communications
247247 c = cr .create_communication_from_request (status = "completed" )
248248 c = Communication (c )
249- new_c = c . persist ( )
249+ result = HAPI_request ( 'POST' , 'Communication' , resource = c . as_json () )
250250 audit_entry (
251- f"Created Communication resource:" ,
252- extra = {"resource" : new_c },
251+ f"Created Communication resource on Twilio callback :" ,
252+ extra = {"resource" : result },
253253 level = 'debug'
254254 )
255255 # if this was a manual message, mark patient as having been followed up with
@@ -258,12 +258,12 @@ def on_twilio_message_status_update(self, values):
258258 else :
259259 # Update the status of the communication to completed
260260 comm = Communication (existing_comm )
261- comm .status = ' completed' if message_status == 'delivered' else 'in-progress'
262- updated_comm = comm .persist ()
261+ comm .status = " completed"
262+ result = comm .persist ()
263263 audit_entry (
264264 f"Received /MessageStatus callback with status { message_status } on existing Communication resource" ,
265- extra = {"resource" : updated_comm ,
266- "new status" : updated_comm .get ('status' ),
265+ extra = {"resource" : result ,
266+ "new status" : result .get ('status' ),
267267 "message status" : message_status },
268268 level = 'debug'
269269 )
@@ -362,13 +362,12 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
362362 except Exception as e :
363363 cr .status = "unknown"
364364 skipped_crs .append (cr )
365- c = cr .create_communication_from_request ()
365+ c = cr .create_communication_from_request (status = "entered-in-error" )
366366 c = Communication (c )
367- c .status = "entered-in-error"
368- c .persist ()
367+ result = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
369368 audit_entry (
370- f"Failed to send the message, { patient } does not have phone number " ,
371- extra = {"resource" : f"CommunicationResource/ { cr . id } " , "exception" : e },
369+ f"Failed to send the message, { patient } does not have valid telecom " ,
370+ extra = {"resource" : f"{ result } " , "exception" : e },
372371 level = 'exception'
373372 )
374373 # Display Twilio Error in a human readable form
@@ -385,13 +384,12 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
385384 if patient_unsubscribed or not patient .active :
386385 if patient_unsubscribed :
387386 cr .status = "on-hold"
388- c = cr .create_communication_from_request ()
387+ c = cr .create_communication_from_request (status = "not-done" )
389388 c = Communication (c )
390- c .status = "not-done"
391- new_c = c .persist ()
389+ result = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
392390 audit_entry (
393391 f"Generated Communication for unsubscribed patient" ,
394- extra = {"resource" : f"{ new_c } " },
392+ extra = {"resource" : f"{ result } " },
395393 level = 'debug'
396394 )
397395 else :
0 commit comments