Skip to content

Commit b78cce8

Browse files
committed
Handling communications for unsubscribed patients
1 parent 16d2794 commit b78cce8

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

isacc_messaging/api/isacc_record_creator.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ def dispatch_cr(self, cr: CommunicationRequest):
8888
# In case of unsubcribed patient, mark as unsubscribed
8989
patient.unsubcribe()
9090
cr.status = "on-hold"
91-
c.status = "suspended"
91+
c.status = "not-done"
9292
else:
9393
# For other causes of failed communication, mark the reason for failed request as unknown
9494
cr.status = "unknown"
95-
c.status = "on-hold"
95+
c.status = "entered-in-error"
9696
c.persist()
9797
audit_entry(
9898
f"Updated Communication to status to {c.status}",
@@ -246,7 +246,6 @@ 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-
250249
new_c = c.persist()
251250
audit_entry(
252251
f"Created Communication resource:",
@@ -259,7 +258,7 @@ def on_twilio_message_status_update(self, values):
259258
else:
260259
# Update the status of the communication to completed
261260
comm = Communication(existing_comm)
262-
comm.status = 'completed'
261+
comm.status = 'completed' if message_status == 'delivered' else 'in-progress'
263262
updated_comm = comm.persist()
264263
audit_entry(
265264
f"Received /MessageStatus callback with status {message_status} on existing Communication resource",
@@ -361,8 +360,12 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
361360
for telecom_entry in patient.telecom
362361
)
363362
except Exception as e:
364-
cr.status = "on-hold"
363+
cr.status = "unknown"
365364
skipped_crs.append(cr)
365+
c = cr.create_communication_from_request()
366+
c = Communication(c)
367+
c.status = "entered-in-error"
368+
c.persist()
366369
audit_entry(
367370
f"Failed to send the message, {patient} does not have phone number",
368371
extra={"resource": f"CommunicationResource/{cr.id}", "exception": e},
@@ -382,6 +385,15 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
382385
if patient_unsubscribed or not patient.active:
383386
if patient_unsubscribed:
384387
cr.status = "on-hold"
388+
c = cr.create_communication_from_request()
389+
c = Communication(c)
390+
c.status = "not-done"
391+
new_c = c.persist()
392+
audit_entry(
393+
f"Generated Communication for unsubscribed patient",
394+
extra={"resource": f"{new_c}"},
395+
level='debug'
396+
)
385397
else:
386398
cr.status = "revoked"
387399
if cr.occurrenceDateTime.date < now:

0 commit comments

Comments
 (0)