Skip to content

Commit e365202

Browse files
Trigger changes to notify_parents when consents are changed
This will cause an update on all related vaccination records if a relevant consent is: - invalidated - created (it is currently not possible to create a consent form after a vaccination record has been created, so this code is currently not used. The code has been included in case this becomes possible in the future)
1 parent 2934b4f commit e365202

File tree

5 files changed

+192
-3
lines changed

5 files changed

+192
-3
lines changed

app/controllers/draft_consents_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def handle_confirm
6868

6969
@consent.save!
7070

71+
@consent.update_vaccination_records_no_notify!
72+
7173
StatusUpdater.call(patient: @patient)
7274
end
7375

app/controllers/patient_sessions/consents_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def update_withdraw
6565
if @consent.valid?
6666
ActiveRecord::Base.transaction do
6767
@consent.save!
68+
6869
update_patient_status
6970
end
7071

@@ -84,6 +85,9 @@ def update_invalidate
8485
if @consent.valid?
8586
ActiveRecord::Base.transaction do
8687
@consent.save!
88+
89+
@consent.update_vaccination_records_no_notify!
90+
8791
update_patient_status
8892
end
8993

app/models/consent.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ def notes_required?
212212
)
213213
end
214214

215+
def update_vaccination_records_no_notify!
216+
vaccination_records = VaccinationRecord.where(patient:, programme:)
217+
218+
vaccination_records.find_each do |vaccination_record|
219+
vaccination_record.update!(
220+
notify_parents:
221+
VaccinationNotificationCriteria.call(vaccination_record:)
222+
)
223+
end
224+
end
225+
215226
class ConsentFormNotRecorded < StandardError
216227
end
217228
end

spec/features/invalidate_consent_spec.rb

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@
5050
and_i_am_not_able_to_record_a_vaccination
5151
end
5252

53+
scenario "Given self consent, and requested parents aren't notified, and vaccinated, then invalidate" do
54+
given_i_am_signed_in
55+
and_self_consent_has_been_given
56+
and_patient_has_been_vaccinated_with_no_parent_notification
57+
58+
when_i_go_to_the_patient
59+
then_i_see_the_self_consent
60+
61+
when_i_click_on_the_self_consent
62+
and_i_click_invalidate_consent
63+
64+
when_i_fill_in_the_notes
65+
and_i_click_invalidate_consent
66+
then_i_see_the_consent_has_been_invalidated
67+
and_the_vaccination_record_is_updated_to_notify_parents
68+
end
69+
5370
def given_i_am_signed_in
5471
@programme = create(:programme, :hpv)
5572
team = create(:team, :with_one_nurse, programmes: [@programme])
@@ -120,9 +137,15 @@ def when_i_fill_in_the_notes
120137

121138
def then_i_see_the_consent_has_been_invalidated
122139
expect(page).to have_content("Invalid")
123-
expect(page).to have_content(
124-
"Consent response from #{@parent.full_name} marked as invalid"
125-
)
140+
if @parent
141+
expect(page).to have_content(
142+
"Consent response from #{@parent.full_name} marked as invalid"
143+
)
144+
else
145+
expect(page).to have_content(
146+
"Consent response from #{@patient.full_name} marked as invalid"
147+
)
148+
end
126149
end
127150

128151
def and_i_cant_mark_as_invalid
@@ -137,4 +160,44 @@ def and_i_am_not_able_to_record_a_vaccination
137160
expect(page).to have_content("No response")
138161
expect(page).not_to have_content("ready for their HPV vaccination?")
139162
end
163+
164+
def and_self_consent_has_been_given
165+
@consent =
166+
create(
167+
:consent,
168+
:given,
169+
:self_consent,
170+
patient: @patient,
171+
programme: @programme
172+
)
173+
create(
174+
:patient_consent_status,
175+
:given,
176+
patient: @patient,
177+
programme: @programme
178+
)
179+
end
180+
181+
def and_patient_has_been_vaccinated_with_no_parent_notification
182+
@vaccination_record =
183+
create(
184+
:vaccination_record,
185+
patient: @patient,
186+
programme: @programme,
187+
session: @session,
188+
notify_parents: false
189+
)
190+
end
191+
192+
def then_i_see_the_self_consent
193+
expect(page).to have_content("Child (Gillick competent)")
194+
end
195+
196+
def when_i_click_on_the_self_consent
197+
click_on "Child (Gillick competent)"
198+
end
199+
200+
def and_the_vaccination_record_is_updated_to_notify_parents
201+
expect(@vaccination_record.reload.notify_parents).to be true
202+
end
140203
end

spec/models/consent_spec.rb

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,113 @@
345345
expect(consents).not_to include(consent_next_year)
346346
end
347347
end
348+
349+
describe "#update_vaccination_records_no_notify" do
350+
let(:patient) { create(:patient) }
351+
let(:programme) { create(:programme, :hpv) }
352+
let(:consent) { create(:consent, patient:, programme:) }
353+
354+
context "when vaccination records exist for the patient and programme" do
355+
let!(:first_vaccination_record) do
356+
create(:vaccination_record, patient:, programme:, notify_parents: false)
357+
end
358+
let!(:second_vaccination_record) do
359+
create(:vaccination_record, patient:, programme:, notify_parents: true)
360+
end
361+
let!(:other_patient_record) do
362+
create(
363+
:vaccination_record,
364+
patient: create(:patient),
365+
programme:,
366+
notify_parents: false
367+
)
368+
end
369+
let!(:other_programme_record) do
370+
create(
371+
:vaccination_record,
372+
patient:,
373+
programme: create(:programme, :flu),
374+
notify_parents: false
375+
)
376+
end
377+
378+
before do
379+
allow(VaccinationNotificationCriteria).to receive(:call).with(
380+
vaccination_record: first_vaccination_record
381+
).and_return(true)
382+
allow(VaccinationNotificationCriteria).to receive(:call).with(
383+
vaccination_record: second_vaccination_record
384+
).and_return(false)
385+
end
386+
387+
it "updates notify_parents for matching vaccination records" do
388+
expect { consent.update_vaccination_records_no_notify! }.to change {
389+
first_vaccination_record.reload.notify_parents
390+
}.from(false).to(true).and change {
391+
second_vaccination_record.reload.notify_parents
392+
}.from(true).to(false)
393+
end
394+
395+
it "does not update vaccination records for other patients" do
396+
expect { consent.update_vaccination_records_no_notify! }.not_to(
397+
change { other_patient_record.reload.notify_parents }
398+
)
399+
end
400+
401+
it "does not update vaccination records for other programmes" do
402+
expect { consent.update_vaccination_records_no_notify! }.not_to(
403+
change { other_programme_record.reload.notify_parents }
404+
)
405+
end
406+
407+
it "calls VaccinationNotificationCriteria for each matching record" do
408+
consent.update_vaccination_records_no_notify!
409+
410+
expect(VaccinationNotificationCriteria).to have_received(:call).with(
411+
vaccination_record: first_vaccination_record
412+
)
413+
expect(VaccinationNotificationCriteria).to have_received(:call).with(
414+
vaccination_record: second_vaccination_record
415+
)
416+
end
417+
end
418+
419+
context "with multiple vaccination records and mixed results" do
420+
let!(:vaccination_records) do
421+
Array.new(3) do
422+
create(
423+
:vaccination_record,
424+
patient:,
425+
programme:,
426+
notify_parents: false
427+
)
428+
end
429+
end
430+
431+
before do
432+
allow(VaccinationNotificationCriteria).to receive(:call).with(
433+
vaccination_record: vaccination_records[0]
434+
).and_return(true)
435+
allow(VaccinationNotificationCriteria).to receive(:call).with(
436+
vaccination_record: vaccination_records[1]
437+
).and_return(nil)
438+
allow(VaccinationNotificationCriteria).to receive(:call).with(
439+
vaccination_record: vaccination_records[2]
440+
).and_return(false)
441+
end
442+
443+
it "updates each vaccination record according to the criteria result" do
444+
expect { consent.update_vaccination_records_no_notify! }.to change {
445+
vaccination_records[0].reload.notify_parents
446+
}.from(false).to(true).and change {
447+
vaccination_records[1].reload.notify_parents
448+
}
449+
.from(false)
450+
.to(nil)
451+
.and(
452+
not_change { vaccination_records[2].reload.notify_parents }
453+
)
454+
end
455+
end
456+
end
348457
end

0 commit comments

Comments
 (0)