@@ -64,7 +64,7 @@ def dispatch_cr(self, cr: CommunicationRequest):
64
64
patient = resolve_reference (cr .recipient [0 ].reference )
65
65
# Create a new Communication attempt
66
66
c = cr .create_communication_from_request (status = "in-progress" )
67
- c = Communication (c )
67
+ comm = Communication (c )
68
68
try :
69
69
if not patient .generalPractitioner :
70
70
practitioner = None
@@ -75,7 +75,7 @@ def dispatch_cr(self, cr: CommunicationRequest):
75
75
content = cr .payload [0 ].contentString ,
76
76
patient = patient ,
77
77
practitioner = practitioner )
78
- resulting_communication = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
78
+ resulting_communication = HAPI_request ('POST' , 'Communication' , resource = comm .as_json ())
79
79
audit_entry (
80
80
f"Created Communication resource for the outgoing text" ,
81
81
extra = {"resource" : resulting_communication },
@@ -87,23 +87,18 @@ def dispatch_cr(self, cr: CommunicationRequest):
87
87
if ex .code == 21610 :
88
88
# In case of unsubcribed patient, mark as unsubscribed
89
89
patient .unsubcribe ()
90
- cr .status = "on-hold"
91
- c .status = "not-done"
90
+ comm .status = "stopped"
92
91
else :
93
92
# For other causes of failed communication, mark the reason for failed request as unknown
94
- cr .status = "unknown"
95
- c .status = "entered-in-error"
96
- c .persist ()
97
- audit_entry (
98
- f"Updated Communication to status to { c .status } " ,
99
- level = 'debug'
100
- )
93
+ comm .status = "unknown"
94
+ comm .statusReason = str (ex )
101
95
102
96
audit_entry (
103
97
"Twilio exception" ,
104
98
extra = {"resource" : f"CommunicationResource/{ cr .id } " , "exception" : ex },
105
99
level = 'exception'
106
100
)
101
+ comm .persist ()
107
102
raise IsaccTwilioError (f"ERROR! { ex } raised attempting to send SMS" )
108
103
109
104
if result .status != 'sent' and result .status != 'queued' :
@@ -334,7 +329,6 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
334
329
"""
335
330
For all due CommunicationRequests, generate SMS, create Communication resource, and update CommunicationRequest
336
331
"""
337
-
338
332
successes = []
339
333
errors = []
340
334
skipped_crs = []
@@ -356,14 +350,14 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
356
350
# Should not occur in production.
357
351
try :
358
352
patient_unsubscribed = any (
359
- telecom_entry .system .lower () == 'sms' and telecom_entry .period .end
353
+ telecom_entry .system .lower () == 'sms' and telecom_entry .period .end
360
354
for telecom_entry in patient .telecom
361
355
)
362
356
except Exception as e :
363
- cr .status = "unknown"
364
357
skipped_crs .append (cr )
365
- c = cr .create_communication_from_request (status = "entered-in-error " )
358
+ c = cr .create_communication_from_request (status = "unknown " )
366
359
c = Communication (c )
360
+ c .statusReason = str (e )
367
361
result = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
368
362
audit_entry (
369
363
f"Failed to send the message, { patient } does not have valid telecom" ,
@@ -378,26 +372,19 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
378
372
# Anything older than cutoff will never be sent (#1861758)
379
373
# and needs a status adjustment lest it throws off other queries
380
374
# like next outgoing message time
381
- cr .status = "revoked"
382
375
skipped_crs .append (cr )
383
376
continue
384
377
if patient_unsubscribed or not patient .active :
385
378
if patient_unsubscribed :
386
- cr .status = "on-hold"
387
- c = cr .create_communication_from_request (status = "not-done" )
379
+ c = cr .create_communication_from_request (status = "stopped" )
388
380
c = Communication (c )
389
381
result = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
390
382
audit_entry (
391
383
f"Generated Communication for unsubscribed patient" ,
392
384
extra = {"resource" : f"{ result } " },
393
385
level = 'debug'
394
386
)
395
- else :
396
- cr .status = "revoked"
397
- if cr .occurrenceDateTime .date < now :
398
- # Skip the messages scheduled to send if user unsubscribed
399
- skipped_crs .append (cr )
400
- # Do not cancel future sms
387
+ skipped_crs .append (cr )
401
388
continue
402
389
try :
403
390
self .process_cr (cr , successes )
@@ -411,6 +398,7 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
411
398
errors .append ({'id' : cr .id , 'error' : str (e )})
412
399
413
400
for cr in skipped_crs :
401
+ cr .status = "revoked"
414
402
HAPI_request (
415
403
"PUT" ,
416
404
"CommunicationRequest" ,
0 commit comments