Skip to content

Commit

Permalink
resolving testing issue with missed transitions to resolved and cat…
Browse files Browse the repository at this point in the history
…ching any outstanding action states when new EMPRO is posted.
  • Loading branch information
pbugni committed Apr 16, 2024
1 parent 1318fb3 commit 46d63dd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
21 changes: 4 additions & 17 deletions portal/trigger_states/empro_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,6 @@ def evaluate_triggers(qnr):
current_app.logger.debug(
"persist-trigger_states-new record state change to 'processed' "
f"from evaluate_triggers() {ts}")

# a patient submission closes the window of availability for the
# post-intervention clinician follow up from any previous visits.
# mark state if one is found
previous = TriggerState.query.filter(
TriggerState.user_id == qnr.subject_id).filter(
TriggerState.state == 'resolved').order_by(
TriggerState.timestamp.desc()).first()
if previous and previous.triggers.get('action_state') not in (
'completed', 'missed', 'not applicable', 'withdrawn'):
triggers = copy.deepcopy(previous.triggers)
triggers['action_state'] = 'missed'
previous.triggers = triggers
current_app.logger.debug(
f"persist-trigger_states-change previous {previous}")
db.session.commit()

return ts

except TransitionNotAllowed as e:
Expand Down Expand Up @@ -585,6 +568,10 @@ def extract_observations(questionnaire_response_id, override_state=False):
"persist-trigger_states-new from"
f" enter_user_trigger_critical_section() {ts}")

# As we now have a new EMPRO to score, clean up any unfinished
# rows, as they can no longer be acted on.
ts.resolve_outstanding(ts.visit_month)

if not ts.state == 'inprocess':
raise ValueError(
f"invalid state; can't score: {qnr.subject_id}:{qnr.id}")
Expand Down
33 changes: 33 additions & 0 deletions portal/trigger_states/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,39 @@ def latest_for_visit(patient_id, visit_month):
TriggerState.visit_month == visit_month).order_by(
TriggerState.id.desc()).first()

def resolve_outstanding(self, visit_month):
"""resolve any visits prior to visit_month during transition
Once the next EMPRO is posted, the window closes for any outstanding
clinician follow-ups. Clean up state if any such rows are found
"""
from .empro_states import EMPRO_state

# a patient submission closes the window of availability for the
# post-intervention clinician follow up from any previous visits.
# mark state if one is found
outstanding = TriggerState.query.filter(
TriggerState.user_id == self.user_id).filter(
TriggerState.state.in_('triggered', 'resolved')).filter(
TriggerState.visit_month < visit_month)
for row in outstanding:
dirty = False
if row.state == 'triggered':
sm = EMPRO_state(row)
sm.resolve()
dirty = True

if row.triggers.get('action_state') not in (
'completed', 'missed', 'not applicable', 'withdrawn'):
triggers = deepcopy(row.triggers)
triggers['action_state'] = 'missed'
row.triggers = triggers
dirty = True
current_app.logger.debug(
f"persist-trigger_states-change outstanding {row}")
if dirty:
db.session.commit()


class TriggerStatesReporting:
"""Manage reporting details for a given patient"""
Expand Down

0 comments on commit 46d63dd

Please sign in to comment.