Skip to content

Commit

Permalink
Don't send notifications to deceased patients
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomasleese committed Oct 17, 2024
1 parent 6ed18e8 commit 8b5b214
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/controllers/concerns/triage_mailer_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/concerns/vaccination_mailer_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions app/jobs/consent_reminders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
2 changes: 2 additions & 0 deletions app/jobs/consent_requests_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/jobs/session_reminders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 4 additions & 0 deletions app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/concerns/triage_mailer_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions spec/controllers/concerns/vaccination_mailer_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion spec/jobs/consent_reminders_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion spec/jobs/consent_requests_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/jobs/session_reminders_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8b5b214

Please sign in to comment.