Skip to content

Commit

Permalink
Introducing different CR statuses, according to the type of error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Filienko committed Feb 10, 2024
1 parent ceab3f8 commit 6784cd4
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions isacc_messaging/api/isacc_record_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ def convert_communicationrequest_to_communication(self, cr):
result = self.send_twilio_sms(message=expanded_payload, to_phone=target_phone)

except TwilioRestException as ex:
# In case of unsubcribed patient, mark as unsubscribed
if ex.code == 21610:
# In case of unsubcribed patient, mark as unsubscribed
sms_telecom_entry = next((entry for entry in patient.telecom if entry.system.lower() == 'sms'))
sms_telecom_entry.period.end = FHIRDate(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'))
patient.persist()

cr.status = "on-hold"
else:
# For other causes of failed communication, mark the reason for failed request as unknown
cr.status = "unknown"
audit_entry(
"Twilio exception",
extra={"resource": f"CommunicationResource/{cr.id}", "exception": ex},
Expand Down Expand Up @@ -366,8 +369,8 @@ def on_twilio_message_status_update(self, values):

def on_twilio_message_received(self, values):
pt = HAPI_request('GET', 'Patient', params={
'telecom': values.get('From', "+1").replace("+1", ""),
'active': 'true',
"telecom": values.get("From", "+1").replace("+1", ""),
"active": "true",
})
pt = first_in_bundle(pt)
if not pt:
Expand Down Expand Up @@ -444,6 +447,7 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
for telecom_entry in patient.telecom
)
except Exception as e:
cr.status = "on-hold"
skipped_crs.append(cr)
audit_entry(
f"Failed to send the message, {patient} does not have phone number",
Expand All @@ -458,9 +462,14 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
# Anything older than cutoff will never be sent (#1861758)
# and needs a status adjustment lest it throws off other queries
# like next outgoing message time
cr.status = "revoked"
skipped_crs.append(cr)
continue
if patient_unsubscribed or not patient.active:
if patient_unsubscribed:
cr.status = "on-hold"
else:
cr.status = "revoked"
if cr.occurrenceDateTime.date < now:
# Skip the messages scheduled to send if user unsubscribed
skipped_crs.append(cr)
Expand All @@ -474,18 +483,17 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
extra={"resource": f"CommunicationResource/{cr.id}", "exception": e},
level='exception'
)
# Display Twilio Error in a human readable form
skipped_crs.append(cr)
errors.append({'id': cr.id, 'error': str(e)})

for cr in skipped_crs:
cr.status = "revoked"
HAPI_request(
"PUT",
"CommunicationRequest",
resource_id=cr.id,
resource=cr.as_json())
audit_entry(
f"Skipped CommunicationRequest({cr.id}); status set to revoked",
f"Skipped CommunicationRequest({cr.id}); status set to {cr.status}",
extra={"CommunicationRequest": cr.as_json()})
# as that message was likely the next-outgoing for the patient,
# update the extension used to track next-outgoing-message time
Expand Down

0 comments on commit 6784cd4

Please sign in to comment.