@@ -62,7 +62,7 @@ def dispatch_cr(self, cr: CommunicationRequest):
62
62
63
63
target_phone = resolve_reference (cr .recipient [0 ].reference ).get_phone_number ()
64
64
patient = resolve_reference (cr .recipient [0 ].reference )
65
- # Create a Communication attempt
65
+ # Create a new Communication attempt
66
66
c = cr .create_communication_from_request (status = "in-progress" )
67
67
c = Communication (c )
68
68
try :
@@ -75,10 +75,10 @@ def dispatch_cr(self, cr: CommunicationRequest):
75
75
content = cr .payload [0 ].contentString ,
76
76
patient = patient ,
77
77
practitioner = practitioner )
78
- c . persist ( )
78
+ resulting_communication = HAPI_request ( 'POST' , 'Communication' , resource = c . as_json () )
79
79
audit_entry (
80
80
f"Created Communication resource for the outgoing text" ,
81
- extra = {"resource" : c },
81
+ extra = {"resource" : resulting_communication },
82
82
level = 'debug'
83
83
)
84
84
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
194
194
]
195
195
}
196
196
c = Communication (m )
197
- result = c . persist ( )
197
+ result = HAPI_request ( 'POST' , 'Communication' , resource = c . as_json () )
198
198
audit_entry (
199
199
f"Created Communication resource for incoming text" ,
200
200
extra = {"resource" : result },
@@ -246,10 +246,10 @@ def on_twilio_message_status_update(self, values):
246
246
# Callback only occurs on completed Communications
247
247
c = cr .create_communication_from_request (status = "completed" )
248
248
c = Communication (c )
249
- new_c = c . persist ( )
249
+ result = HAPI_request ( 'POST' , 'Communication' , resource = c . as_json () )
250
250
audit_entry (
251
- f"Created Communication resource:" ,
252
- extra = {"resource" : new_c },
251
+ f"Created Communication resource on Twilio callback :" ,
252
+ extra = {"resource" : result },
253
253
level = 'debug'
254
254
)
255
255
# 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):
258
258
else :
259
259
# Update the status of the communication to completed
260
260
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 ()
263
263
audit_entry (
264
264
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' ),
267
267
"message status" : message_status },
268
268
level = 'debug'
269
269
)
@@ -362,13 +362,12 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
362
362
except Exception as e :
363
363
cr .status = "unknown"
364
364
skipped_crs .append (cr )
365
- c = cr .create_communication_from_request ()
365
+ c = cr .create_communication_from_request (status = "entered-in-error" )
366
366
c = Communication (c )
367
- c .status = "entered-in-error"
368
- c .persist ()
367
+ result = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
369
368
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 },
372
371
level = 'exception'
373
372
)
374
373
# Display Twilio Error in a human readable form
@@ -385,13 +384,12 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
385
384
if patient_unsubscribed or not patient .active :
386
385
if patient_unsubscribed :
387
386
cr .status = "on-hold"
388
- c = cr .create_communication_from_request ()
387
+ c = cr .create_communication_from_request (status = "not-done" )
389
388
c = Communication (c )
390
- c .status = "not-done"
391
- new_c = c .persist ()
389
+ result = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
392
390
audit_entry (
393
391
f"Generated Communication for unsubscribed patient" ,
394
- extra = {"resource" : f"{ new_c } " },
392
+ extra = {"resource" : f"{ result } " },
395
393
level = 'debug'
396
394
)
397
395
else :
0 commit comments