@@ -64,7 +64,7 @@ def dispatch_cr(self, cr: CommunicationRequest):
6464 patient = resolve_reference (cr .recipient [0 ].reference )
6565 # Create a new Communication attempt
6666 c = cr .create_communication_from_request (status = "in-progress" )
67- c = Communication (c )
67+ comm = Communication (c )
6868 try :
6969 if not patient .generalPractitioner :
7070 practitioner = None
@@ -75,7 +75,7 @@ def dispatch_cr(self, cr: CommunicationRequest):
7575 content = cr .payload [0 ].contentString ,
7676 patient = patient ,
7777 practitioner = practitioner )
78- resulting_communication = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
78+ resulting_communication = HAPI_request ('POST' , 'Communication' , resource = comm .as_json ())
7979 audit_entry (
8080 f"Created Communication resource for the outgoing text" ,
8181 extra = {"resource" : resulting_communication },
@@ -87,23 +87,18 @@ def dispatch_cr(self, cr: CommunicationRequest):
8787 if ex .code == 21610 :
8888 # In case of unsubcribed patient, mark as unsubscribed
8989 patient .unsubcribe ()
90- cr .status = "on-hold"
91- c .status = "not-done"
90+ comm .status = "stopped"
9291 else :
9392 # 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 )
10195
10296 audit_entry (
10397 "Twilio exception" ,
10498 extra = {"resource" : f"CommunicationResource/{ cr .id } " , "exception" : ex },
10599 level = 'exception'
106100 )
101+ comm .persist ()
107102 raise IsaccTwilioError (f"ERROR! { ex } raised attempting to send SMS" )
108103
109104 if result .status != 'sent' and result .status != 'queued' :
@@ -334,7 +329,6 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
334329 """
335330 For all due CommunicationRequests, generate SMS, create Communication resource, and update CommunicationRequest
336331 """
337-
338332 successes = []
339333 errors = []
340334 skipped_crs = []
@@ -356,14 +350,14 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
356350 # Should not occur in production.
357351 try :
358352 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
360354 for telecom_entry in patient .telecom
361355 )
362356 except Exception as e :
363- cr .status = "unknown"
364357 skipped_crs .append (cr )
365- c = cr .create_communication_from_request (status = "entered-in-error " )
358+ c = cr .create_communication_from_request (status = "unknown " )
366359 c = Communication (c )
360+ c .statusReason = str (e )
367361 result = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
368362 audit_entry (
369363 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]]:
378372 # Anything older than cutoff will never be sent (#1861758)
379373 # and needs a status adjustment lest it throws off other queries
380374 # like next outgoing message time
381- cr .status = "revoked"
382375 skipped_crs .append (cr )
383376 continue
384377 if patient_unsubscribed or not patient .active :
385378 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" )
388380 c = Communication (c )
389381 result = HAPI_request ('POST' , 'Communication' , resource = c .as_json ())
390382 audit_entry (
391383 f"Generated Communication for unsubscribed patient" ,
392384 extra = {"resource" : f"{ result } " },
393385 level = 'debug'
394386 )
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 )
401388 continue
402389 try :
403390 self .process_cr (cr , successes )
@@ -411,6 +398,7 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
411398 errors .append ({'id' : cr .id , 'error' : str (e )})
412399
413400 for cr in skipped_crs :
401+ cr .status = "revoked"
414402 HAPI_request (
415403 "PUT" ,
416404 "CommunicationRequest" ,
0 commit comments