From 8b5b214ed2dd61aa13de478adfe69a2c5be10333 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Thu, 17 Oct 2024 16:25:58 +0100 Subject: [PATCH] Don't send notifications to deceased patients This updates everywhere that we send notifications to ensure that we're not sending inapproriate notifications for any deceased patients. This shouldn't happen as the patients are automatically removed from sessions, but as a precaution we can put checks here too. --- .../concerns/triage_mailer_concern.rb | 3 +++ .../concerns/vaccination_mailer_concern.rb | 3 +++ app/jobs/consent_reminders_job.rb | 2 ++ app/jobs/consent_requests_job.rb | 2 ++ app/jobs/session_reminders_job.rb | 7 ++++++- app/models/patient.rb | 4 ++++ .../concerns/triage_mailer_concern_spec.rb | 13 +++++++++++++ .../vaccination_mailer_concern_spec.rb | 12 ++++++++++++ spec/jobs/consent_reminders_job_spec.rb | 4 +++- spec/jobs/consent_requests_job_spec.rb | 8 +++++++- spec/jobs/session_reminders_job_spec.rb | 19 +++++++++++++++++++ 11 files changed, 74 insertions(+), 3 deletions(-) diff --git a/app/controllers/concerns/triage_mailer_concern.rb b/app/controllers/concerns/triage_mailer_concern.rb index f44054309..818b20baf 100644 --- a/app/controllers/concerns/triage_mailer_concern.rb +++ b/app/controllers/concerns/triage_mailer_concern.rb @@ -5,6 +5,9 @@ module TriageMailerConcern def send_triage_confirmation(patient_session, consent) session = patient_session.session + patient = patient_session.patient + + return if patient.deceased? if vaccination_will_happen?(patient_session, consent) TriageMailer diff --git a/app/controllers/concerns/vaccination_mailer_concern.rb b/app/controllers/concerns/vaccination_mailer_concern.rb index 3605e1a3a..fd63a9296 100644 --- a/app/controllers/concerns/vaccination_mailer_concern.rb +++ b/app/controllers/concerns/vaccination_mailer_concern.rb @@ -5,6 +5,9 @@ module VaccinationMailerConcern def send_vaccination_confirmation(vaccination_record) patient_session = vaccination_record.patient_session + patient = vaccination_record.patient + + return if patient.deceased? mailer_action = if vaccination_record.administered? diff --git a/app/jobs/consent_reminders_job.rb b/app/jobs/consent_reminders_job.rb index 129e0e344..ab53fe3ea 100644 --- a/app/jobs/consent_reminders_job.rb +++ b/app/jobs/consent_reminders_job.rb @@ -37,6 +37,8 @@ def perform end def should_send_notification?(patient:, programme:, session:) + return false if patient.deceased? + return false if patient.has_consent?(programme) return false if patient.consent_notifications.none?(&:request?) diff --git a/app/jobs/consent_requests_job.rb b/app/jobs/consent_requests_job.rb index 3fc76cc22..e376bec84 100644 --- a/app/jobs/consent_requests_job.rb +++ b/app/jobs/consent_requests_job.rb @@ -29,6 +29,8 @@ def perform end def should_send_notification?(patient:, programme:) + return false if patient.deceased? + return false if patient.has_consent?(programme) patient.consent_notifications.none? do diff --git a/app/jobs/session_reminders_job.rb b/app/jobs/session_reminders_job.rb index 437e862d2..e4bd3c16c 100644 --- a/app/jobs/session_reminders_job.rb +++ b/app/jobs/session_reminders_job.rb @@ -16,7 +16,7 @@ def perform .reminder_not_sent(date) patient_sessions.each do |patient_session| - next if patient_session.vaccination_administered? + next unless should_send_notification?(patient_session) # We create a record in the database first to avoid sending duplicate emails/texts. # If a problem occurs while the emails/texts are sent, they will be in the job @@ -39,4 +39,9 @@ def perform end end end + + def should_send_notification?(patient_session) + !patient_session.patient.deceased? && + !patient_session.vaccination_administered? + end end diff --git a/app/models/patient.rb b/app/models/patient.rb index aa67facb2..61dfceef9 100644 --- a/app/models/patient.rb +++ b/app/models/patient.rb @@ -149,6 +149,10 @@ def vaccinated?(programme) end end + def deceased? + date_of_death != nil + end + def update_from_pds!(pds_patient) if nhs_number.nil? || nhs_number != pds_patient["id"] raise NHSNumberMismatch diff --git a/spec/controllers/concerns/triage_mailer_concern_spec.rb b/spec/controllers/concerns/triage_mailer_concern_spec.rb index 94045f78b..aded0077f 100644 --- a/spec/controllers/concerns/triage_mailer_concern_spec.rb +++ b/spec/controllers/concerns/triage_mailer_concern_spec.rb @@ -97,5 +97,18 @@ ).with(consent:, session:) end end + + context "if the patient is deceased" do + let(:patient) { create(:patient, :deceased) } + let(:patient_session) { create(:patient_session, patient:) } + + it "doesn't send an email" do + expect { send_triage_confirmation }.not_to have_enqueued_email + end + + it "doesn't send a text message" do + expect { send_triage_confirmation }.not_to have_enqueued_text + end + end end end diff --git a/spec/controllers/concerns/vaccination_mailer_concern_spec.rb b/spec/controllers/concerns/vaccination_mailer_concern_spec.rb index e396a57dc..96e5a7d6f 100644 --- a/spec/controllers/concerns/vaccination_mailer_concern_spec.rb +++ b/spec/controllers/concerns/vaccination_mailer_concern_spec.rb @@ -71,5 +71,17 @@ expect { send_vaccination_confirmation }.not_to have_enqueued_text end end + + context "if the patient is deceased" do + let(:patient) { create(:patient, :deceased) } + + it "doesn't send an email" do + expect { send_vaccination_confirmation }.not_to have_enqueued_email + end + + it "doesn't send a text message" do + expect { send_vaccination_confirmation }.not_to have_enqueued_text + end + end end end diff --git a/spec/jobs/consent_reminders_job_spec.rb b/spec/jobs/consent_reminders_job_spec.rb index 9ea133323..48b5c7ac1 100644 --- a/spec/jobs/consent_reminders_job_spec.rb +++ b/spec/jobs/consent_reminders_job_spec.rb @@ -25,13 +25,15 @@ let(:patient_with_consent) do create(:patient, :consent_given_triage_not_needed, programme:) end + let(:deceased_patient) { create(:patient, :deceased) } let!(:patients) do [ patient_with_initial_reminder_sent, patient_not_sent_reminder, patient_not_sent_request, - patient_with_consent + patient_with_consent, + deceased_patient ] end diff --git a/spec/jobs/consent_requests_job_spec.rb b/spec/jobs/consent_requests_job_spec.rb index 83f2a0611..f23d5a380 100644 --- a/spec/jobs/consent_requests_job_spec.rb +++ b/spec/jobs/consent_requests_job_spec.rb @@ -17,9 +17,15 @@ let(:patient_with_consent) do create(:patient, :consent_given_triage_not_needed, programme:) end + let(:deceased_patient) { create(:patient, :deceased) } let!(:patients) do - [patient_with_request_sent, patient_not_sent_request, patient_with_consent] + [ + patient_with_request_sent, + patient_not_sent_request, + patient_with_consent, + deceased_patient + ] end context "when session is unscheduled" do diff --git a/spec/jobs/session_reminders_job_spec.rb b/spec/jobs/session_reminders_job_spec.rb index 84ec278fb..011c275c4 100644 --- a/spec/jobs/session_reminders_job_spec.rb +++ b/spec/jobs/session_reminders_job_spec.rb @@ -85,6 +85,25 @@ expect { perform_now }.not_to change(SessionNotification, :count) end end + + context "if the patient is deceased" do + let(:patient) { create(:patient, :deceased, parents:) } + + it "doesn't send a reminder email" do + expect { perform_now }.not_to have_enqueued_mail( + SessionMailer, + :reminder + ) + end + + it "doesn't sent a reminder text" do + expect { perform_now }.not_to have_enqueued_text(:session_reminder) + end + + it "doesn't record a notification" do + expect { perform_now }.not_to change(SessionNotification, :count) + end + end end context "for a session today" do