From 3116ecab286199cf4014fbc71170672911b7948b Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Wed, 15 Jan 2025 10:03:05 +0000 Subject: [PATCH 1/3] Remove mailers This removes the unused mailer classses now that we're sending emails using the GOV.UK Notify API directly. --- app/mailers/application_mailer.rb | 110 +------------ app/mailers/consent_mailer.rb | 39 ----- app/mailers/session_mailer.rb | 15 -- app/mailers/triage_mailer.rb | 15 -- app/mailers/vaccination_mailer.rb | 15 -- spec/mailers/consent_mailer_spec.rb | 209 ------------------------ spec/mailers/session_mailer_spec.rb | 145 ---------------- spec/mailers/triage_mailer_spec.rb | 88 ---------- spec/mailers/vaccination_mailer_spec.rb | 187 --------------------- 9 files changed, 1 insertion(+), 822 deletions(-) delete mode 100644 app/mailers/consent_mailer.rb delete mode 100644 app/mailers/session_mailer.rb delete mode 100644 app/mailers/triage_mailer.rb delete mode 100644 app/mailers/vaccination_mailer.rb delete mode 100644 spec/mailers/consent_mailer_spec.rb delete mode 100644 spec/mailers/session_mailer_spec.rb delete mode 100644 spec/mailers/triage_mailer_spec.rb delete mode 100644 spec/mailers/vaccination_mailer_spec.rb diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 96cd4d1d8..26148f2d6 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,112 +1,4 @@ # frozen_string_literal: true -class ApplicationMailer < Mail::Notify::Mailer - before_action :attach_data_for_notify_log_entry - after_deliver :log_delivery - - private - - def app_template_mail(template_name) - template_mail( - GOVUK_NOTIFY_EMAIL_TEMPLATES.fetch(template_name), - to:, - reply_to_id:, - personalisation: - ) - end - - def consent - @consent ||= params[:consent] - end - - def consent_form - @consent_form ||= params[:consent_form] - end - - def vaccination_record - @vaccination_record ||= params[:vaccination_record] - end - - def patient_session - @patient_session ||= params[:patient_session] - end - - def patient - @patient ||= params[:patient] - end - - def parent - @parent ||= params[:parent] - end - - def programme - @programme ||= params[:programme] - end - - def session - @session ||= params[:session] - end - - def sent_by - @sent_by ||= params[:sent_by] - end - - def to - consent_form&.parent_email || consent&.parent&.email || parent.email - end - - def reply_to_id - team = - session&.team || patient_session&.team || consent_form&.team || - vaccination_record&.team - - return team.reply_to_id if team&.reply_to_id - - organisation = - session&.organisation || patient_session&.organisation || - consent_form&.organisation || consent&.organisation || - vaccination_record&.organisation - - organisation.reply_to_id - end - - def personalisation - GovukNotifyPersonalisation.call( - consent:, - consent_form:, - patient:, - patient_session:, - programme:, - session:, - vaccination_record: - ) - end - - def attach_data_for_notify_log_entry - # https://stackoverflow.com/a/28004917 - - patient_id = (patient || consent&.patient || patient_session&.patient)&.id - consent_form_id = consent_form&.id - - message.instance_variable_set(:@consent_form_id, consent_form_id) - message.instance_variable_set(:@patient_id, patient_id) - message.instance_variable_set(:@sent_by_user_id, sent_by&.id) - - message.class.send(:attr_reader, :consent_form_id) - message.class.send(:attr_reader, :patient_id) - message.class.send(:attr_reader, :sent_by_user_id) - end - - def log_delivery - mail.to.map do |recipient| - NotifyLogEntry.create!( - consent_form_id: mail.consent_form_id, - patient_id: mail.patient_id, - recipient:, - sent_by_user_id: mail.sent_by_user_id, - template_id: mail.template_id, - type: :email - ) - end - end +class ApplicationMailer < ActionMailer::Base end diff --git a/app/mailers/consent_mailer.rb b/app/mailers/consent_mailer.rb deleted file mode 100644 index c3d65099d..000000000 --- a/app/mailers/consent_mailer.rb +++ /dev/null @@ -1,39 +0,0 @@ -# frozen_string_literal: true - -class ConsentMailer < ApplicationMailer - def confirmation_clinic - app_template_mail(:consent_confirmation_clinic) - end - - def confirmation_given - app_template_mail(:consent_confirmation_given) - end - - def confirmation_triage - app_template_mail(:consent_confirmation_triage) - end - - def confirmation_injection - app_template_mail(:consent_confirmation_injection) - end - - def confirmation_refused - app_template_mail(:consent_confirmation_refused) - end - - def school_request - app_template_mail(:consent_school_request) - end - - def school_initial_reminder - app_template_mail(:consent_school_initial_reminder) - end - - def school_subsequent_reminder - app_template_mail(:consent_school_subsequent_reminder) - end - - def clinic_request - app_template_mail(:consent_clinic_request) - end -end diff --git a/app/mailers/session_mailer.rb b/app/mailers/session_mailer.rb deleted file mode 100644 index 853eafe68..000000000 --- a/app/mailers/session_mailer.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -class SessionMailer < ApplicationMailer - def school_reminder - app_template_mail(:session_school_reminder) - end - - def clinic_initial_invitation - app_template_mail(:session_clinic_initial_invitation) - end - - def clinic_subsequent_invitation - app_template_mail(:session_clinic_subsequent_invitation) - end -end diff --git a/app/mailers/triage_mailer.rb b/app/mailers/triage_mailer.rb deleted file mode 100644 index 9d87c5bae..000000000 --- a/app/mailers/triage_mailer.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -class TriageMailer < ApplicationMailer - def vaccination_at_clinic - app_template_mail(:triage_vaccination_at_clinic) - end - - def vaccination_will_happen - app_template_mail(:triage_vaccination_will_happen) - end - - def vaccination_wont_happen - app_template_mail(:triage_vaccination_wont_happen) - end -end diff --git a/app/mailers/vaccination_mailer.rb b/app/mailers/vaccination_mailer.rb deleted file mode 100644 index f7be9c32d..000000000 --- a/app/mailers/vaccination_mailer.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -class VaccinationMailer < ApplicationMailer - def confirmation_administered - app_template_mail(:vaccination_confirmation_administered) - end - - def confirmation_not_administered - app_template_mail(:vaccination_confirmation_not_administered) - end - - def deleted - app_template_mail(:vaccination_deleted) - end -end diff --git a/spec/mailers/consent_mailer_spec.rb b/spec/mailers/consent_mailer_spec.rb deleted file mode 100644 index 5be30ab6f..000000000 --- a/spec/mailers/consent_mailer_spec.rb +++ /dev/null @@ -1,209 +0,0 @@ -# frozen_string_literal: true - -describe ConsentMailer do - describe "#confirmation_clinic" do - subject(:mail) { described_class.with(consent_form:).confirmation_clinic } - - let(:programme) { create(:programme, :flu) } - let(:consent_form) do - create( - :consent_form, - :recorded, - :given, - programme:, - session: create(:session, programme:) - ) - end - - it { should have_attributes(to: [consent_form.parent_email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it "sets the personalisation" do - expect(personalisation.keys).to include( - :full_and_preferred_patient_name, - :short_patient_name_apos, - :team_email, - :team_name, - :team_phone, - :vaccination - ) - end - end - end - - describe "#confirmation_injection" do - subject(:mail) do - described_class.with(consent_form:).confirmation_injection - end - - let(:programme) { create(:programme, :flu) } - let(:consent_form) do - create( - :consent_form, - :recorded, - :refused, - reason: :contains_gelatine, - programme:, - session: create(:session, programme:) - ) - end - - it "calls template_mail with correct reason_for_refusal" do - expect(mail.message.header["personalisation"].unparsed_value).to include( - reason_for_refusal: "of the gelatine in the nasal spray" - ) - end - end - - describe "#school_request" do - subject(:mail) do - described_class.with( - session:, - patient:, - parent:, - programme: - ).school_request - end - - let(:parent) { create(:parent) } - let(:patient) { create(:patient, parents: [parent]) } - let(:programme) { create(:programme) } - let(:date) { Date.current } - let(:session) { create(:session, date:, patients: [patient], programme:) } - - it { should have_attributes(to: [parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - let(:consent_deadline) { (date - 1.day).strftime("%A %-d %B") } - let(:consent_link) do - start_parent_interface_consent_forms_url(session, programme) - end - - it { should include(consent_deadline:) } - it { should include(consent_link:) } - it { should include(full_and_preferred_patient_name: patient.full_name) } - it { should include(location_name: session.location.name) } - it { should include(next_session_date: date.strftime("%A %-d %B")) } - it { should include(team_email: session.team.email) } - it { should include(team_phone: session.team.phone) } - end - end - - describe "#clinic_request" do - subject(:mail) do - described_class.with( - session:, - patient:, - parent:, - programme: - ).clinic_request - end - - let(:parent) { create(:parent) } - let(:patient) { create(:patient, parents: [parent]) } - let(:programme) { create(:programme) } - let(:date) { Date.current } - let(:session) { create(:session, date:, patients: [patient], programme:) } - - it { should have_attributes(to: [parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - let(:consent_link) do - start_parent_interface_consent_forms_url(session, programme) - end - - it { should include(consent_link:) } - it { should include(full_and_preferred_patient_name: patient.full_name) } - it { should include(team_email: session.team.email) } - it { should include(team_phone: session.team.phone) } - end - end - - describe "#school_initial_reminder" do - subject(:mail) do - described_class.with( - session:, - patient:, - parent:, - programme: - ).school_initial_reminder - end - - let(:parent) { create(:parent) } - let(:patient) { create(:patient, parents: [parent]) } - let(:programme) { create(:programme) } - let(:date) { Date.current } - let(:session) { create(:session, date:, patients: [patient], programme:) } - - it { should have_attributes(to: [parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it { should include(next_session_date: date.strftime("%A %-d %B")) } - it { should include(location_name: session.location.name) } - it { should include(team_email: session.team.email) } - it { should include(team_phone: session.team.phone) } - - it "includes consent details" do - expect(personalisation).to include( - consent_deadline: (date - 1.day).strftime("%A %-d %B"), - consent_link: - start_parent_interface_consent_forms_url(session, programme) - ) - end - end - end - - describe "#school_subsequent_reminder" do - subject(:mail) do - described_class.with( - session:, - patient:, - parent:, - programme: - ).school_subsequent_reminder - end - - let(:parent) { create(:parent) } - let(:patient) { create(:patient, parents: [parent]) } - let(:programme) { create(:programme) } - let(:date) { Date.current } - let(:session) { create(:session, date:, patients: [patient], programme:) } - - it { should have_attributes(to: [parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it { should include(next_session_date: date.strftime("%A %-d %B")) } - it { should include(location_name: session.location.name) } - it { should include(team_email: session.team.email) } - it { should include(team_phone: session.team.phone) } - - it "includes consent details" do - expect(personalisation).to include( - consent_deadline: (date - 1.day).strftime("%A %-d %B"), - consent_link: - start_parent_interface_consent_forms_url(session, programme) - ) - end - end - end -end diff --git a/spec/mailers/session_mailer_spec.rb b/spec/mailers/session_mailer_spec.rb deleted file mode 100644 index 257698985..000000000 --- a/spec/mailers/session_mailer_spec.rb +++ /dev/null @@ -1,145 +0,0 @@ -# frozen_string_literal: true - -describe SessionMailer do - describe "#school_reminder" do - subject(:mail) do - described_class.with( - programme:, - session:, - patient:, - parent: - ).school_reminder - end - - let(:programme) { create(:programme) } - let(:parent) { create(:parent) } - let(:patient) do - create(:patient, preferred_given_name: "Joey", parents: [parent]) - end - let(:session) { create(:session, programme:, patients: [patient]) } - - it { should have_attributes(to: [parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it "sets the personalisation" do - expect(personalisation.keys).to include( - :full_and_preferred_patient_name, - :location_name, - :next_session_date, - :next_session_dates, - :next_session_dates_or, - :short_patient_name, - :short_patient_name_apos, - :subsequent_session_dates_offered_message, - :team_email, - :team_name, - :team_phone, - :vaccination - ) - end - end - end - - describe "#clinic_initial_invitation" do - subject(:mail) do - described_class.with(patient_session:, parent:).clinic_initial_invitation - end - - let(:programme) { create(:programme) } - let(:organisation) { create(:organisation, programmes: [programme]) } - let(:team) do - create( - :team, - name: "SAIS organisation", - email: "sais@example.com", - phone: "07987 654321", - organisation: - ) - end - let(:parent) { create(:parent) } - let(:patient) do - create( - :patient, - given_name: "John", - family_name: "Smith", - preferred_given_name: "Joey", - parents: [parent] - ) - end - let(:session) { create(:session, organisation:, programme:, team:) } - let(:patient_session) { create(:patient_session, patient:, session:) } - - it { should have_attributes(to: [parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it do - expect(personalisation).to include( - full_and_preferred_patient_name: "John Smith (known as Joey Smith)" - ) - end - - it { should include(team_name: "SAIS organisation") } - it { should include(team_email: "sais@example.com") } - it { should include(team_phone: "07987 654321") } - end - end - - describe "#clinic_subsequent_invitation" do - subject(:mail) do - described_class.with( - patient_session:, - parent: - ).clinic_subsequent_invitation - end - - let(:programme) { create(:programme) } - let(:organisation) { create(:organisation, programmes: [programme]) } - let(:team) do - create( - :team, - name: "SAIS organisation", - email: "sais@example.com", - phone: "07987 654321", - organisation: - ) - end - let(:parent) { create(:parent) } - let(:patient) do - create( - :patient, - given_name: "John", - family_name: "Smith", - preferred_given_name: "Joey", - parents: [parent] - ) - end - let(:session) { create(:session, organisation:, programme:, team:) } - let(:patient_session) { create(:patient_session, patient:, session:) } - - it { should have_attributes(to: [parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it do - expect(personalisation).to include( - full_and_preferred_patient_name: "John Smith (known as Joey Smith)" - ) - end - - it { should include(team_name: "SAIS organisation") } - it { should include(team_email: "sais@example.com") } - it { should include(team_phone: "07987 654321") } - end - end -end diff --git a/spec/mailers/triage_mailer_spec.rb b/spec/mailers/triage_mailer_spec.rb deleted file mode 100644 index a5c7074d2..000000000 --- a/spec/mailers/triage_mailer_spec.rb +++ /dev/null @@ -1,88 +0,0 @@ -# frozen_string_literal: true - -describe TriageMailer do - let(:session) { patient_session.session } - - describe "#vaccination_at_clinic" do - subject(:mail) do - described_class.with(consent:, session:).vaccination_at_clinic - end - - let(:patient_session) { create(:patient_session, :delay_vaccination) } - let(:consent) { patient_session.patient.consents.first } - - it { should have_attributes(to: [consent.parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value.keys - end - - it do - expect(personalisation).to include( - :full_and_preferred_patient_name, - :short_patient_name, - :team_name, - :team_email, - :team_phone - ) - end - end - end - - describe "#vaccination_will_happen" do - subject(:mail) do - described_class.with(consent:, session:).vaccination_will_happen - end - - let(:patient_session) do - create(:patient_session, :consent_given_triage_not_needed) - end - let(:consent) { patient_session.patient.consents.first } - - it { should have_attributes(to: [consent.parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it do - expect(personalisation).to include( - :full_and_preferred_patient_name, - :short_patient_name, - :team_name, - :team_email, - :team_phone - ) - end - end - end - - describe "#vaccination_wont_happen" do - subject(:mail) do - described_class.with(consent:, session:).vaccination_wont_happen - end - - let(:patient_session) { create(:patient_session, :consent_refused) } - let(:consent) { patient_session.patient.consents.first } - - it { should have_attributes(to: [consent.parent.email]) } - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it do - expect(personalisation).to include( - :full_and_preferred_patient_name, - :short_patient_name, - :team_name, - :team_email, - :team_phone - ) - end - end - end -end diff --git a/spec/mailers/vaccination_mailer_spec.rb b/spec/mailers/vaccination_mailer_spec.rb deleted file mode 100644 index da14d9d8f..000000000 --- a/spec/mailers/vaccination_mailer_spec.rb +++ /dev/null @@ -1,187 +0,0 @@ -# frozen_string_literal: true - -describe VaccinationMailer do - let(:programme) { create(:programme) } - let(:session) { create(:session, programme:) } - let(:consent) { patient.consents.last } - let(:parent) { consent.parent } - - describe "#confirmation_administered" do - subject(:mail) do - described_class.with( - consent:, - vaccination_record: - ).confirmation_administered - end - - let(:patient) do - create(:patient, :consent_given_triage_not_needed, programme:) - end - let(:patient_session) { create(:patient_session, patient:, session:) } - let(:vaccination_record) do - create(:vaccination_record, programme:, patient_session:) - end - - it do - expect(mail).to have_attributes( - to: [parent.email], - template_id: - GOVUK_NOTIFY_EMAIL_TEMPLATES.fetch( - :vaccination_confirmation_administered - ) - ) - end - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it "sets the personalisation" do - expect(personalisation.keys).to include( - :batch_name, - :day_month_year_of_vaccination, - :full_and_preferred_patient_name, - :location_name, - :team_email, - :team_name, - :team_phone, - :today_or_date_of_vaccination - ) - end - - it "sets the correct batch number" do - expect(personalisation).to include( - batch_name: vaccination_record.batch.name - ) - end - - it "sets the day month and year of vaccination" do - expect(personalisation).to include( - day_month_year_of_vaccination: - vaccination_record.performed_at.strftime("%d/%m/%Y") - ) - end - - describe "today_or_date_of_vaccination field" do - subject { personalisation[:today_or_date_of_vaccination] } - - let(:vaccination_record) do - create( - :vaccination_record, - programme:, - patient_session:, - performed_at: - ) - end - - context "when the vaccination was performed today" do - let(:performed_at) { Time.zone.today } - - it { should eq("today") } - end - - context "when the vaccination was create 2 days ago" do - let(:performed_at) { Date.new(2023, 3, 1) } - - it { should eq("1 March 2023") } - end - end - end - end - - describe "#confirmation_not_administered" do - subject(:mail) do - described_class.with( - parent:, - vaccination_record: - ).confirmation_not_administered - end - - let(:patient) do - create(:patient, :consent_given_triage_not_needed, programme:) - end - let(:patient_session) { create(:patient_session, session:, patient:) } - let(:vaccination_record) do - create( - :vaccination_record, - :not_administered, - programme:, - patient_session: - ) - end - - it do - expect(mail).to have_attributes( - to: [parent.email], - template_id: - GOVUK_NOTIFY_EMAIL_TEMPLATES.fetch( - :vaccination_confirmation_not_administered - ) - ) - end - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it "sets the personalisation" do - expect(personalisation.keys).to include( - :full_and_preferred_patient_name, - :reason_did_not_vaccinate, - :short_patient_name, - :show_additional_instructions, - :team_email, - :team_name, - :team_phone - ) - end - end - end - - describe "#deleted" do - subject(:mail) do - described_class.with(parent:, vaccination_record:).deleted - end - - let(:patient) do - create(:patient, :consent_given_triage_not_needed, programme:) - end - let(:patient_session) { create(:patient_session, session:, patient:) } - let(:vaccination_record) do - create( - :vaccination_record, - :not_administered, - :discarded, - programme:, - patient_session: - ) - end - - it do - expect(mail).to have_attributes( - to: [parent.email], - template_id: GOVUK_NOTIFY_EMAIL_TEMPLATES.fetch(:vaccination_deleted) - ) - end - - describe "personalisation" do - subject(:personalisation) do - mail.message.header["personalisation"].unparsed_value - end - - it "sets the personalisation" do - expect(personalisation[:outcome_administered]).to eq("no") - expect(personalisation[:outcome_not_administered]).to eq("yes") - expect(personalisation.keys).to include( - :outcome_administered, - :outcome_not_administered, - :team_email, - :team_name, - :team_phone - ) - end - end - end -end From 5da9c6979968dd119dde6c9da51157461124fc7c Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Wed, 15 Jan 2025 10:03:09 +0000 Subject: [PATCH 2/3] Remove `mail-notify` This Gem is no longer being used so we can safely remove it. --- Gemfile | 1 - Gemfile.lock | 8 -------- 2 files changed, 9 deletions(-) diff --git a/Gemfile b/Gemfile index 3f90a595f..d561c2000 100644 --- a/Gemfile +++ b/Gemfile @@ -43,7 +43,6 @@ gem "govuk_design_system_formbuilder" gem "govuk_markdown" gem "jsonb_accessor" gem "jwt" -gem "mail-notify" gem "notifications-ruby-client" gem "okcomputer" gem "omniauth_openid_connect" diff --git a/Gemfile.lock b/Gemfile.lock index 088bd5414..b75b6684e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -338,13 +338,6 @@ GEM net-imap net-pop net-smtp - mail-notify (2.0.0) - actionmailer (>= 5.2.8.1) - actionpack (>= 5.2.8.1) - actionview (>= 5.2.8.1) - activesupport (>= 5.2.8.1) - notifications-ruby-client (~> 6.0) - rack (>= 2.1.4.1) marcel (1.0.4) matrix (0.4.2) mechanize (2.12.2) @@ -742,7 +735,6 @@ DEPENDENCIES jsbundling-rails jsonb_accessor jwt - mail-notify mechanize notifications-ruby-client okcomputer From ac744b9c6fd17d743a81a804263fc6c1eaf70cba Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Wed, 15 Jan 2025 10:06:01 +0000 Subject: [PATCH 3/3] Remove `action_mailer` We're no longer using `ActionMailer` to send emails so we can safely remove code that references it. --- app/jobs/notify_delivery_job.rb | 10 +++-- config/application.rb | 2 - config/environments/development.rb | 19 --------- config/environments/production.rb | 21 --------- config/environments/staging.rb | 4 -- config/environments/test.rb | 9 ---- config/settings/test.yml | 2 + spec/jobs/email_delivery_job_spec.rb | 6 +-- spec/jobs/sms_delivery_job_spec.rb | 6 +-- spec/mailers/application_mailer_spec.rb | 57 ------------------------- 10 files changed, 14 insertions(+), 122 deletions(-) delete mode 100644 spec/mailers/application_mailer_spec.rb diff --git a/app/jobs/notify_delivery_job.rb b/app/jobs/notify_delivery_job.rb index 320df7651..455ccaf2c 100644 --- a/app/jobs/notify_delivery_job.rb +++ b/app/jobs/notify_delivery_job.rb @@ -1,14 +1,16 @@ # frozen_string_literal: true +require "notifications/client" + class NotifyDeliveryJob < ApplicationJob - queue_as { Rails.configuration.action_mailer.deliver_later_queue_name } + queue_as :mailer retry_on Notifications::Client::ServerError, wait: :polynomially_longer def self.client @client ||= Notifications::Client.new( - Rails.configuration.action_mailer.notify_settings[:api_key] + Settings.govuk_notify["#{Settings.govuk_notify.mode}_key"] ) end @@ -17,11 +19,11 @@ def self.deliveries end def self.send_via_notify? - Rails.configuration.action_mailer.delivery_method == :notify + Settings.govuk_notify&.enabled end def self.send_via_test? - Rails.configuration.action_mailer.delivery_method == :test + Rails.env.test? end class UnknownTemplate < StandardError diff --git a/config/application.rb b/config/application.rb index 458f0c4d3..2b0809eaf 100644 --- a/config/application.rb +++ b/config/application.rb @@ -64,8 +64,6 @@ class Application < Rails::Application config.active_job.queue_adapter = :good_job config.good_job.execution_mode = :async - config.action_mailer.deliver_later_queue_name = :mailer - config.view_component.default_preview_layout = "component_preview" config.view_component.preview_controller = "ComponentPreviewsController" config.view_component.preview_paths << Rails.root.join( diff --git a/config/environments/development.rb b/config/environments/development.rb index 6f2d744f4..d22f42f94 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -36,25 +36,6 @@ # Store uploaded files on the local file system (see config/storage.yml for options). config.active_storage.service = :local - # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false - - # Make template changes take effect immediately. - config.action_mailer.perform_caching = false - - # Set localhost to be used by links generated in mailer templates. - config.action_mailer.default_url_options = { host: "localhost", port: 4000 } - - if Settings.govuk_notify&.enabled - config.action_mailer.delivery_method = :notify - config.action_mailer.notify_settings = { - api_key: Settings.govuk_notify["#{Settings.govuk_notify.mode}_key"] - } - else - config.action_mailer.default_options = { from: "no-reply@nhs.net" } - config.action_mailer.delivery_method = :file - end - # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log diff --git a/config/environments/production.rb b/config/environments/production.rb index f8ebde4f6..b1f15257a 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -77,27 +77,6 @@ # Replace the default in-process and non-durable queuing backend for Active Job. # config.active_job.queue_adapter = :resque - # Ignore bad email addresses and do not raise email delivery errors. - # Set this to true and configure the email server for immediate delivery to raise delivery errors. - # config.action_mailer.raise_delivery_errors = false - - # Set host to be used by links generated in mailer templates. - config.action_mailer.default_url_options = { - host: - if Settings.is_review - "#{ENV["HEROKU_APP_NAME"]}.herokuapp.com" - else - Settings.host - end, - protocol: "https" - } - - # Configure GOV.UK Notify. - config.action_mailer.delivery_method = :notify - config.action_mailer.notify_settings = { - api_key: Settings.govuk_notify.live_key - } - # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). config.i18n.fallbacks = true diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 8fc7962f0..8d0e2a5dd 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -20,8 +20,4 @@ description: "Track the status of DPS exports" } ) - - config.action_mailer.notify_settings = { - api_key: Settings.govuk_notify["#{Settings.govuk_notify.mode}_key"] - } end diff --git a/config/environments/test.rb b/config/environments/test.rb index 9f77a34d9..d91a781d5 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -35,15 +35,6 @@ # Store uploaded files on the local file system in a temporary directory. config.active_storage.service = :test - # Tell Action Mailer not to deliver emails to the real world. - # The :test delivery method accumulates sent emails in the - # ActionMailer::Base.deliveries array. - config.action_mailer.delivery_method = :test - - # Set host to be used by links generated in mailer templates. - config.action_mailer.default_url_options = { host: "localhost:4000" } - config.action_mailer.default_options = { from: "no-reply@nhs.net" } - # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr diff --git a/config/settings/test.yml b/config/settings/test.yml index 5d9a71c00..8cb103347 100644 --- a/config/settings/test.yml +++ b/config/settings/test.yml @@ -9,6 +9,8 @@ cis2: govuk_notify: callback_bearer_token: test-bearer-token + enabled: false + mode: test mesh: base_url: https://localhost:8700 diff --git a/spec/jobs/email_delivery_job_spec.rb b/spec/jobs/email_delivery_job_spec.rb index d8010bcec..a4d7d18b4 100644 --- a/spec/jobs/email_delivery_job_spec.rb +++ b/spec/jobs/email_delivery_job_spec.rb @@ -2,11 +2,11 @@ describe EmailDeliveryJob do before(:all) do - Rails.configuration.action_mailer.delivery_method = :notify - Rails.configuration.action_mailer.notify_settings = { api_key: "abc" } + Settings.govuk_notify.enabled = true + Settings.govuk_notify.test_key = "abc" end - after(:all) { Rails.configuration.action_mailer.delivery_method = :test } + after(:all) { Settings.govuk_notify.enabled = false } let(:response) do instance_double( diff --git a/spec/jobs/sms_delivery_job_spec.rb b/spec/jobs/sms_delivery_job_spec.rb index 7763f5538..03102a98d 100644 --- a/spec/jobs/sms_delivery_job_spec.rb +++ b/spec/jobs/sms_delivery_job_spec.rb @@ -2,11 +2,11 @@ describe SMSDeliveryJob do before(:all) do - Rails.configuration.action_mailer.delivery_method = :notify - Rails.configuration.action_mailer.notify_settings = { api_key: "abc" } + Settings.govuk_notify.enabled = true + Settings.govuk_notify.test_key = "abc" end - after(:all) { Rails.configuration.action_mailer.delivery_method = :test } + after(:all) { Settings.govuk_notify.enabled = false } let(:response) do instance_double( diff --git a/spec/mailers/application_mailer_spec.rb b/spec/mailers/application_mailer_spec.rb deleted file mode 100644 index 63db94a5f..000000000 --- a/spec/mailers/application_mailer_spec.rb +++ /dev/null @@ -1,57 +0,0 @@ -# frozen_string_literal: true - -describe ApplicationMailer do - subject(:mail) do - mailer_class.with(session:, parent:, patient:).example_email - end - - let(:mailer_class) do - Class.new(ApplicationMailer) do - def example_email - app_template_mail(GOVUK_NOTIFY_EMAIL_TEMPLATES.keys.first) - end - end - end - - let(:programmes) { create_list(:programme, 1) } - let(:organisation) { create(:organisation, programmes:) } - let(:team) { create(:team, organisation:) } - - let(:location) { create(:school, team:) } - let(:session) { create(:session, location:, organisation:, programmes:) } - let(:parent) { create(:parent) } - let(:patient) { create(:patient) } - - describe "#reply_to_id" do - subject { mail.reply_to_id } - - it { should be_nil } - - context "when the organisation has a reply_to_id" do - let(:reply_to_id) { SecureRandom.uuid } - - let(:organisation) { create(:organisation, programmes:, reply_to_id:) } - - it { should eq(reply_to_id) } - end - - context "when the team has a reply_to_id" do - let(:reply_to_id) { SecureRandom.uuid } - - let(:team) { create(:team, organisation:, reply_to_id:) } - - it { should eq(reply_to_id) } - end - - context "when the team and the organisation has a reply_to_id" do - let(:reply_to_id) { SecureRandom.uuid } - - let(:organisation) do - create(:organisation, programmes:, reply_to_id: SecureRandom.uuid) - end - let(:team) { create(:team, organisation:, reply_to_id:) } - - it { should eq(reply_to_id) } - end - end -end