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