Skip to content

Commit 7e59956

Browse files
committed
Call Patient::TriageStatus#refresh!
This ensures that at each point where a triage record, vaccination records or consent record is created or updated, we update the triage status of the patient record. I've decided to avoid using callbacks as I find they can lead to suprising behaviour and this helps to keep the code explicit, however I'm not against using callbacks if that's preferred.
1 parent f65e798 commit 7e59956

File tree

8 files changed

+38
-19
lines changed

8 files changed

+38
-19
lines changed

app/controllers/consent_forms_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def set_consent_form
132132
def set_patient
133133
@patient =
134134
policy_scope(Patient).includes(
135+
:triages,
135136
consents: :parent,
136137
parent_relationships: :parent,
137138
sessions_for_current_academic_year: :programmes

app/controllers/consents_controller.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,19 @@ def set_consent
136136
end
137137

138138
def update_patient_status
139-
Patient
140-
.includes(consents: :parent)
141-
.find(@patient.id)
139+
patient = Patient.includes(:triages, consents: :parent).find(@patient.id)
140+
141+
patient
142142
.consent_statuses
143143
.find_or_initialize_by(programme: @consent.programme)
144144
.refresh!
145145

146-
@patient.triages.where(programme_id: @consent.programme_id).invalidate_all
146+
patient
147+
.triage_statuses
148+
.find_or_initialize_by(programme: @consent.programme)
149+
.refresh!
150+
151+
patient.triages.where(programme_id: @consent.programme_id).invalidate_all
147152
end
148153

149154
def ensure_can_withdraw

app/controllers/draft_consents_controller.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@ def handle_confirm
6060
@consent.parent&.save!
6161
@consent.save!
6262

63-
Patient
64-
.includes(consents: :parent)
65-
.find(@patient.id)
66-
.consent_statuses
67-
.find_or_initialize_by(programme: @consent.programme)
68-
.refresh!
63+
update_patient_status
6964
end
7065

7166
set_patient_session # reload with new consents
@@ -82,6 +77,20 @@ def handle_confirm
8277
}
8378
end
8479

80+
def update_patient_status
81+
patient = Patient.includes(:triages, consents: :parent).find(@patient.id)
82+
83+
patient
84+
.consent_statuses
85+
.find_or_initialize_by(programme: @programme)
86+
.refresh!
87+
88+
patient
89+
.triage_statuses
90+
.find_or_initialize_by(programme: @programme)
91+
.refresh!
92+
end
93+
8594
def handle_questions
8695
questions_attrs = update_params.except(:wizard_step).values
8796
@draft_consent.health_answers.each_with_index do |ha, index|

app/controllers/vaccinations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def set_session
7979

8080
def set_patient
8181
@patient =
82-
policy_scope(Patient).includes(consents: :parent).find(
82+
policy_scope(Patient).includes(:triages, consents: :parent).find(
8383
params.fetch(:patient_id) { params.fetch(:id) }
8484
)
8585
end

app/jobs/consent_form_matching_job.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def match_with_exact_nhs_number
5050
return false unless pds_patient
5151

5252
patient =
53-
Patient.includes(:school, consents: :parent).find_by(
53+
Patient.includes(:school, :triages, consents: :parent).find_by(
5454
nhs_number: pds_patient.nhs_number
5555
)
5656
return false unless patient
@@ -64,7 +64,7 @@ def session_patients
6464
@consent_form
6565
.original_session
6666
.patients
67-
.includes(:school, consents: :parent)
67+
.includes(:school, :triages, consents: :parent)
6868
.match_existing(nhs_number: nil, **query)
6969
end
7070

app/lib/patient_merger.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ def self.call(*args, **kwargs)
119119
attr_reader :patient_to_keep, :patient_to_destroy
120120

121121
def refresh_statuses
122-
Patient
123-
.includes(consents: :parent)
124-
.find(patient_to_keep.id)
125-
.consent_statuses
126-
.find_each(&:refresh!)
122+
patient =
123+
Patient.includes(:triages, consents: :parent).find(patient_to_keep.id)
124+
125+
patient.consent_statuses.find_each(&:refresh!)
126+
127+
patient.triage_statuses.find_each(&:refresh!)
127128
end
128129
end

app/models/consent.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def self.from_consent_form!(consent_form, patient:, current_user:)
165165
)
166166

167167
patient.consent_statuses.find_or_initialize_by(programme:).refresh!
168+
patient.triage_statuses.find_or_initialize_by(programme:).refresh!
168169

169170
consent
170171
end
@@ -186,6 +187,7 @@ def self.from_consent_form!(consent_form, patient:, current_user:)
186187
)
187188

188189
patient.consent_statuses.find_or_initialize_by(programme:).refresh!
190+
patient.triage_statuses.find_or_initialize_by(programme:).refresh!
189191

190192
consent
191193
end

app/models/patient.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ def self.from_consent_form(consent_form)
405405
organisation: consent_form.organisation,
406406
preferred_family_name: consent_form.preferred_family_name,
407407
preferred_given_name: consent_form.preferred_given_name,
408-
school: consent_form.school
408+
school: consent_form.school,
409+
triages: []
409410
)
410411
end
411412

0 commit comments

Comments
 (0)