From 55bd1c8e9fa77da9f953f12d571c1507826bef32 Mon Sep 17 00:00:00 2001 From: pbugni Date: Thu, 12 Dec 2024 12:15:15 -0800 Subject: [PATCH] IRONN-242 update adherence cache on any patient demographics changes. (#4427) Updates to a user's study-id via a PUT to /demographics would leave old adherence data rows with the old study-id. We now force a recalculation of a user's adherence data when PUT /demographics is called. --- portal/models/reporting.py | 16 ++++++++++++++-- portal/views/demographics.py | 8 ++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/portal/models/reporting.py b/portal/models/reporting.py index 9db76b46b..d42f7319c 100644 --- a/portal/models/reporting.py +++ b/portal/models/reporting.py @@ -6,7 +6,6 @@ from flask import current_app from flask_babel import force_locale -from flask_login import login_manager from werkzeug.exceptions import Unauthorized from ..audit import auditable_event @@ -30,12 +29,25 @@ qnr_csv_column_headers, generate_qnr_csv, ) -from .research_study import BASE_RS_ID, EMPRO_RS_ID +from .research_study import BASE_RS_ID, EMPRO_RS_ID, ResearchStudy from .role import ROLE, Role from .user import User, UserRoles, patients_query from .user_consent import consent_withdrawal_dates +def update_patient_adherence_data(patient_id): + """Cache invalidation and force rebuild for given patient's adherence data + + NB - any timeline or questionnaire response data changes are invalidated and + updated as part of `invalidate_users_QBT()`. This function is for edge cases + such as changing a user's study-id. + """ + patient = User.query.get(patient_id) + AdherenceData.query.filter(AdherenceData.patient_id==patient_id).delete() + for rs_id in ResearchStudy.assigned_to(patient): + single_patient_adherence_data(patient_id=patient_id, research_study_id=rs_id) + + def single_patient_adherence_data(patient_id, research_study_id): """Update any missing (from cache) adherence data for patient diff --git a/portal/views/demographics.py b/portal/views/demographics.py index 962d52062..640a43f54 100644 --- a/portal/views/demographics.py +++ b/portal/views/demographics.py @@ -7,7 +7,9 @@ from ..database import db from ..extensions import oauth from ..models.reference import MissingReference +from ..models.reporting import update_patient_adherence_data from ..models.user import current_user, get_user +from ..models.role import ROLE from .crossdomain import crossdomain demographics_api = Blueprint('demographics_api', __name__, url_prefix='/api') @@ -176,7 +178,9 @@ def demographics_set(patient_id): patient_id, json.dumps(request.json)), user_id=current_user().id, subject_id=patient_id, context='user') - # update the patient_table cache with any change from above - patient_list_update_patient(patient_id) + # update the respective cache tables with any change from above + if patient.has_role(ROLE.PATIENT.value): + patient_list_update_patient(patient_id) + update_patient_adherence_data(patient_id) return jsonify(patient.as_fhir(include_empties=False))