From abaaba7ca7ff291041baaf47f3c023bff7f5292c Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Wed, 8 Jan 2025 12:37:23 +0000 Subject: [PATCH] Always send text messages for consent requests/reminders This updates the `ConsentNotification` model to always send a text message to a parent when sending consent requests and reminders, not taking in to account whether the parent has opted-in to receive updates on the vaccination. Instead, opting-in relates to other updates like consent confirmation, rejected, or vaccination given/not given updates. To make this change I've had to update the `TextDeliveryJob` to not take this value in to account and instead everywhere that the job is enqueued considers whether to send the text or not based on the context and whether the parent has opted-in. --- .../concerns/consent_form_mailer_concern.rb | 19 ++++++-- .../concerns/triage_mailer_concern.rb | 10 +++- .../concerns/vaccination_mailer_concern.rb | 4 +- app/jobs/text_delivery_job.rb | 6 --- app/models/consent_notification.rb | 2 + app/models/session_notification.rb | 7 +++ spec/jobs/text_delivery_job_spec.rb | 41 ---------------- spec/models/consent_notification_spec.rb | 48 +++++++++++++++++++ spec/models/session_notification_spec.rb | 32 +++++++++++++ 9 files changed, 114 insertions(+), 55 deletions(-) diff --git a/app/controllers/concerns/consent_form_mailer_concern.rb b/app/controllers/concerns/consent_form_mailer_concern.rb index fdc0b3ae7..5b2324226 100644 --- a/app/controllers/concerns/consent_form_mailer_concern.rb +++ b/app/controllers/concerns/consent_form_mailer_concern.rb @@ -10,10 +10,13 @@ def send_consent_form_confirmation(consent_form) mailer.confirmation_injection.deliver_later elsif consent_form.consent_refused? mailer.confirmation_refused.deliver_later - TextDeliveryJob.perform_later( - :consent_confirmation_refused, - consent_form: - ) + + if consent_form.parent_phone_receive_updates + TextDeliveryJob.perform_later( + :consent_confirmation_refused, + consent_form: + ) + end elsif consent_form.needs_triage? mailer.confirmation_triage.deliver_later elsif consent_form.actual_upcoming_session == @@ -22,7 +25,13 @@ def send_consent_form_confirmation(consent_form) mailer.confirmation_clinic.deliver_later else mailer.confirmation_given.deliver_later - TextDeliveryJob.perform_later(:consent_confirmation_given, consent_form:) + + if consent_form.parent_phone_receive_updates + TextDeliveryJob.perform_later( + :consent_confirmation_given, + consent_form: + ) + end end end end diff --git a/app/controllers/concerns/triage_mailer_concern.rb b/app/controllers/concerns/triage_mailer_concern.rb index 3338f9b8f..1369c087d 100644 --- a/app/controllers/concerns/triage_mailer_concern.rb +++ b/app/controllers/concerns/triage_mailer_concern.rb @@ -22,10 +22,16 @@ def send_triage_confirmation(patient_session, consent) ConsentMailer.with(params).confirmation_triage.deliver_later elsif consent.response_refused? ConsentMailer.with(params).confirmation_refused.deliver_later - TextDeliveryJob.perform_later(:consent_confirmation_refused, **params) + + if consent.parent.phone_receive_updates + TextDeliveryJob.perform_later(:consent_confirmation_refused, **params) + end elsif consent.response_given? ConsentMailer.with(params).confirmation_given.deliver_later - TextDeliveryJob.perform_later(:consent_confirmation_given, **params) + + if consent.parent.phone_receive_updates + TextDeliveryJob.perform_later(:consent_confirmation_given, **params) + end end end diff --git a/app/controllers/concerns/vaccination_mailer_concern.rb b/app/controllers/concerns/vaccination_mailer_concern.rb index 99e8405e2..f7ff37208 100644 --- a/app/controllers/concerns/vaccination_mailer_concern.rb +++ b/app/controllers/concerns/vaccination_mailer_concern.rb @@ -23,7 +23,9 @@ def send_vaccination_confirmation(vaccination_record) VaccinationMailer.with(params).public_send(mailer_action).deliver_later end - TextDeliveryJob.perform_later(text_template_name, **params) + if parent.phone.present? && parent.phone_receive_updates + TextDeliveryJob.perform_later(text_template_name, **params) + end end end diff --git a/app/jobs/text_delivery_job.rb b/app/jobs/text_delivery_job.rb index d0bc70235..9b651410c 100644 --- a/app/jobs/text_delivery_job.rb +++ b/app/jobs/text_delivery_job.rb @@ -22,12 +22,6 @@ def perform( consent_form&.parent_phone || consent&.parent&.phone || parent&.phone return if phone_number.nil? - unless consent_form&.parent_phone_receive_updates || - consent&.parent&.phone_receive_updates || - parent&.phone_receive_updates - return - end - personalisation = GovukNotifyPersonalisation.call( session:, diff --git a/app/models/consent_notification.rb b/app/models/consent_notification.rb index 10c2d76ad..974549555 100644 --- a/app/models/consent_notification.rb +++ b/app/models/consent_notification.rb @@ -86,6 +86,8 @@ def self.create_and_send!( .deliver_later end + next if parent.phone.nil? + TextDeliveryJob.perform_later( text_template, parent:, diff --git a/app/models/session_notification.rb b/app/models/session_notification.rb index f93f9cb17..bf92a4440 100644 --- a/app/models/session_notification.rb +++ b/app/models/session_notification.rb @@ -86,6 +86,11 @@ def self.create_and_send!( .deliver_later end + unless consent.parent.phone.present? && + consent.parent.phone_receive_updates + next + end + TextDeliveryJob.perform_later( :session_school_reminder, consent:, @@ -102,6 +107,8 @@ def self.create_and_send!( .deliver_later end + next if parent.phone.blank? + TextDeliveryJob.perform_later( :"session_#{type}", parent:, diff --git a/spec/jobs/text_delivery_job_spec.rb b/spec/jobs/text_delivery_job_spec.rb index e0de083ba..d478ae6d1 100644 --- a/spec/jobs/text_delivery_job_spec.rb +++ b/spec/jobs/text_delivery_job_spec.rb @@ -82,31 +82,6 @@ expect(notify_log_entry.sent_by).to eq(sent_by) end - context "when the parent doesn't want to receive updates" do - let(:parent) { create(:parent, phone_receive_updates: false) } - - it "doesn't send a text" do - expect(notifications_client).not_to receive(:send_sms) - perform_now - end - end - - context "when the consent's parent doesn't want to receive updates" do - let(:parent) { nil } - let(:consent) do - create( - :consent, - parent: create(:parent, phone_receive_updates: false), - programme: - ) - end - - it "doesn't send a text" do - expect(notifications_client).not_to receive(:send_sms) - perform_now - end - end - context "when the parent doesn't have a phone number" do let(:parent) { create(:parent, phone: nil) } @@ -144,22 +119,6 @@ expect(notify_log_entry.consent_form).to eq(consent_form) end - context "when the parent doesn't want to receive updates" do - let(:consent_form) do - create( - :consent_form, - programme:, - session:, - parent_phone_receive_updates: false - ) - end - - it "doesn't send a text" do - expect(notifications_client).not_to receive(:send_sms) - perform_now - end - end - context "when the parent doesn't have a phone number" do let(:consent_form) do create(:consent_form, programme:, session:, parent_phone: nil) diff --git a/spec/models/consent_notification_spec.rb b/spec/models/consent_notification_spec.rb index e476c1a86..566d44ac6 100644 --- a/spec/models/consent_notification_spec.rb +++ b/spec/models/consent_notification_spec.rb @@ -114,6 +114,18 @@ sent_by: current_user ) end + + context "when parent doesn't want to receive updates by text" do + let(:parent) { parents.first } + + before { parent.update!(phone_receive_updates: false) } + + it "still enqueues a text" do + expect { create_and_send! }.to have_enqueued_text( + :consent_school_request + ).with(parent:, patient:, programme:, session:, sent_by: current_user) + end + end end context "with a request and a clinic location" do @@ -172,6 +184,18 @@ sent_by: current_user ) end + + context "when parent doesn't want to receive updates by text" do + let(:parent) { parents.first } + + before { parent.update!(phone_receive_updates: false) } + + it "still enqueues a text" do + expect { create_and_send! }.to have_enqueued_text( + :consent_clinic_request + ).with(parent:, patient:, programme:, session:, sent_by: current_user) + end + end end context "with an initial reminder" do @@ -229,6 +253,18 @@ sent_by: current_user ) end + + context "when parent doesn't want to receive updates by text" do + let(:parent) { parents.first } + + before { parent.update!(phone_receive_updates: false) } + + it "still enqueues a text" do + expect { create_and_send! }.to have_enqueued_text( + :consent_school_reminder + ).with(parent:, patient:, programme:, session:, sent_by: current_user) + end + end end context "with a subsequent reminder" do @@ -289,6 +325,18 @@ sent_by: current_user ) end + + context "when parent doesn't want to receive updates by text" do + let(:parent) { parents.first } + + before { parent.update!(phone_receive_updates: false) } + + it "still enqueues a text" do + expect { create_and_send! }.to have_enqueued_text( + :consent_school_reminder + ).with(parent:, patient:, programme:, session:, sent_by: current_user) + end + end end end end diff --git a/spec/models/session_notification_spec.rb b/spec/models/session_notification_spec.rb index a28c96b77..c9d7c1910 100644 --- a/spec/models/session_notification_spec.rb +++ b/spec/models/session_notification_spec.rb @@ -85,6 +85,14 @@ :session_school_reminder ).with(consent:, patient_session:, sent_by: current_user) end + + context "when parent doesn't want to receive updates by text" do + before { parents.each { _1.update!(phone_receive_updates: false) } } + + it "doesn't enqueues a text" do + expect { create_and_send! }.not_to have_enqueued_text + end + end end context "with an initial clinic invitation" do @@ -137,6 +145,18 @@ sent_by: current_user ) end + + context "when parent doesn't want to receive updates by text" do + let(:parent) { parents.first } + + before { parent.update!(phone_receive_updates: false) } + + it "still enqueues a text" do + expect { create_and_send! }.to have_enqueued_text( + :session_clinic_initial_invitation + ).with(parent:, patient_session:, sent_by: current_user) + end + end end context "with a subsequent clinic invitation" do @@ -189,6 +209,18 @@ sent_by: current_user ) end + + context "when parent doesn't want to receive updates by text" do + let(:parent) { parents.first } + + before { parent.update!(phone_receive_updates: false) } + + it "still enqueues a text" do + expect { create_and_send! }.to have_enqueued_text( + :session_clinic_subsequent_invitation + ).with(parent:, patient_session:, sent_by: current_user) + end + end end end end