Skip to content

Commit

Permalink
Removing CR logic, represent state in the Communication
Browse files Browse the repository at this point in the history
  • Loading branch information
Filienko committed Feb 16, 2024
1 parent e7efff9 commit 4c593ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
36 changes: 12 additions & 24 deletions isacc_messaging/api/isacc_record_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def dispatch_cr(self, cr: CommunicationRequest):
patient = resolve_reference(cr.recipient[0].reference)
# Create a new Communication attempt
c = cr.create_communication_from_request(status="in-progress")
c = Communication(c)
comm = Communication(c)
try:
if not patient.generalPractitioner:
practitioner=None
Expand All @@ -75,7 +75,7 @@ def dispatch_cr(self, cr: CommunicationRequest):
content=cr.payload[0].contentString,
patient=patient,
practitioner=practitioner)
resulting_communication = HAPI_request('POST', 'Communication', resource=c.as_json())
resulting_communication = HAPI_request('POST', 'Communication', resource=comm.as_json())
audit_entry(
f"Created Communication resource for the outgoing text",
extra={"resource": resulting_communication},
Expand All @@ -87,23 +87,18 @@ def dispatch_cr(self, cr: CommunicationRequest):
if ex.code == 21610:
# In case of unsubcribed patient, mark as unsubscribed
patient.unsubcribe()
cr.status = "on-hold"
c.status = "not-done"
comm.status = "stopped"
else:
# For other causes of failed communication, mark the reason for failed request as unknown
cr.status = "unknown"
c.status = "entered-in-error"
c.persist()
audit_entry(
f"Updated Communication to status to {c.status}",
level='debug'
)
comm.status = "unknown"
comm.statusReason = str(ex)

audit_entry(
"Twilio exception",
extra={"resource": f"CommunicationResource/{cr.id}", "exception": ex},
level='exception'
)
comm.persist()
raise IsaccTwilioError(f"ERROR! {ex} raised attempting to send SMS")

if result.status != 'sent' and result.status != 'queued':
Expand Down Expand Up @@ -334,7 +329,6 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
"""
For all due CommunicationRequests, generate SMS, create Communication resource, and update CommunicationRequest
"""

successes = []
errors = []
skipped_crs = []
Expand All @@ -356,14 +350,14 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
# Should not occur in production.
try:
patient_unsubscribed = any(
telecom_entry.system.lower() == 'sms' and telecom_entry.period.end
telecom_entry.system.lower() == 'sms' and telecom_entry.period.end
for telecom_entry in patient.telecom
)
except Exception as e:
cr.status = "unknown"
skipped_crs.append(cr)
c = cr.create_communication_from_request(status="entered-in-error")
c = cr.create_communication_from_request(status="unknown")
c = Communication(c)
c.statusReason = str(e)
result = HAPI_request('POST', 'Communication', resource=c.as_json())
audit_entry(
f"Failed to send the message, {patient} does not have valid telecom",
Expand All @@ -378,26 +372,19 @@ 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"
c = cr.create_communication_from_request(status="not-done")
c = cr.create_communication_from_request(status="stopped")
c = Communication(c)
result = HAPI_request('POST', 'Communication', resource=c.as_json())
audit_entry(
f"Generated Communication for unsubscribed patient",
extra={"resource": f"{result}"},
level='debug'
)
else:
cr.status = "revoked"
if cr.occurrenceDateTime.date < now:
# Skip the messages scheduled to send if user unsubscribed
skipped_crs.append(cr)
# Do not cancel future sms
skipped_crs.append(cr)
continue
try:
self.process_cr(cr, successes)
Expand All @@ -411,6 +398,7 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
errors.append({'id': cr.id, 'error': str(e)})

for cr in skipped_crs:
cr.status = "revoked"
HAPI_request(
"PUT",
"CommunicationRequest",
Expand Down
3 changes: 2 additions & 1 deletion isacc_messaging/models/isacc_communicationrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def create_communication_from_request(self, status = "completed"):
code = "isacc-auto-sent-message"
return {
"resourceType": "Communication",
"id": str(self.id),
"basedOn": [{"reference": f"CommunicationRequest/{self.id}"}],
"partOf": [{"reference": f"{self.basedOn[0].reference}"}],
"category": [{
Expand All @@ -99,7 +100,7 @@ def create_communication_from_request(self, status = "completed"):
"note": [n.as_json() for n in self.note] if self.note else None,
"status": status
}

def persist(self):
"""Persist self state to FHIR store"""
response = HAPI_request('PUT', 'CommunicationRequest', resource_id=self.id, resource=self.as_json())
Expand Down

0 comments on commit 4c593ad

Please sign in to comment.