Skip to content

Commit

Permalink
Finished refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Filienko committed Feb 15, 2024
1 parent b78cce8 commit e7efff9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
38 changes: 18 additions & 20 deletions isacc_messaging/api/isacc_record_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def dispatch_cr(self, cr: CommunicationRequest):

target_phone = resolve_reference(cr.recipient[0].reference).get_phone_number()
patient = resolve_reference(cr.recipient[0].reference)
# Create a Communication attempt
# Create a new Communication attempt
c = cr.create_communication_from_request(status="in-progress")
c = Communication(c)
try:
Expand All @@ -75,10 +75,10 @@ def dispatch_cr(self, cr: CommunicationRequest):
content=cr.payload[0].contentString,
patient=patient,
practitioner=practitioner)
c.persist()
resulting_communication = HAPI_request('POST', 'Communication', resource=c.as_json())
audit_entry(
f"Created Communication resource for the outgoing text",
extra={"resource": c},
extra={"resource": resulting_communication},
level='debug'
)
result = self.send_twilio_sms(message=expanded_payload, to_phone=target_phone)
Expand Down Expand Up @@ -194,7 +194,7 @@ def generate_incoming_message(self, message, time: datetime = None, patient: Pat
]
}
c = Communication(m)
result = c.persist()
result = HAPI_request('POST', 'Communication', resource=c.as_json())
audit_entry(
f"Created Communication resource for incoming text",
extra={"resource": result},
Expand Down Expand Up @@ -246,10 +246,10 @@ def on_twilio_message_status_update(self, values):
# Callback only occurs on completed Communications
c = cr.create_communication_from_request(status="completed")
c = Communication(c)
new_c = c.persist()
result = HAPI_request('POST', 'Communication', resource=c.as_json())
audit_entry(
f"Created Communication resource:",
extra={"resource": new_c},
f"Created Communication resource on Twilio callback:",
extra={"resource": result},
level='debug'
)
# if this was a manual message, mark patient as having been followed up with
Expand All @@ -258,12 +258,12 @@ def on_twilio_message_status_update(self, values):
else:
# Update the status of the communication to completed
comm = Communication(existing_comm)
comm.status = 'completed' if message_status == 'delivered' else 'in-progress'
updated_comm = comm.persist()
comm.status = "completed"
result = comm.persist()
audit_entry(
f"Received /MessageStatus callback with status {message_status} on existing Communication resource",
extra={"resource": updated_comm,
"new status": updated_comm.get('status'),
extra={"resource": result,
"new status": result.get('status'),
"message status": message_status},
level='debug'
)
Expand Down Expand Up @@ -362,13 +362,12 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
except Exception as e:
cr.status = "unknown"
skipped_crs.append(cr)
c = cr.create_communication_from_request()
c = cr.create_communication_from_request(status="entered-in-error")
c = Communication(c)
c.status = "entered-in-error"
c.persist()
result = HAPI_request('POST', 'Communication', resource=c.as_json())
audit_entry(
f"Failed to send the message, {patient} does not have phone number",
extra={"resource": f"CommunicationResource/{cr.id}", "exception": e},
f"Failed to send the message, {patient} does not have valid telecom",
extra={"resource": f"{result}", "exception": e},
level='exception'
)
# Display Twilio Error in a human readable form
Expand All @@ -385,13 +384,12 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
if patient_unsubscribed or not patient.active:
if patient_unsubscribed:
cr.status = "on-hold"
c = cr.create_communication_from_request()
c = cr.create_communication_from_request(status="not-done")
c = Communication(c)
c.status = "not-done"
new_c = c.persist()
result = HAPI_request('POST', 'Communication', resource=c.as_json())
audit_entry(
f"Generated Communication for unsubscribed patient",
extra={"resource": f"{new_c}"},
extra={"resource": f"{result}"},
level='debug'
)
else:
Expand Down
2 changes: 1 addition & 1 deletion isacc_messaging/models/isacc_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def is_manual_follow_up_message(self) -> bool:

def persist(self):
"""Persist self state to FHIR store"""
response = HAPI_request('POST', 'Communication', resource=self.as_json())
response = HAPI_request('PUT', 'Communication', resource_id=self.id, resource=self.as_json())
return response

@staticmethod
Expand Down

0 comments on commit e7efff9

Please sign in to comment.