diff --git a/config/settings.yml b/config/settings.yml index 7209a57527..b28dbfdfcd 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -1402,7 +1402,7 @@ vanotify: email: confirmation: template_id: form527ez_confirmation_email_template_id - flipper: false + flipper_id: false error: null received: null pensions: *vanotify_services_pension @@ -1411,7 +1411,7 @@ vanotify: email: confirmation: template_id: burial_claim_confirmation_email_template_id - flipper: false + flipper_id: false error: null received: null burials: *vanotify_services_burial diff --git a/lib/va_notify/notification_email.rb b/lib/va_notify/notification_email.rb new file mode 100644 index 0000000000..1703d9eacd --- /dev/null +++ b/lib/va_notify/notification_email.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module VANotify + module NotificationEmail + + STATSD = 'api.va_notify.notification_email' + + CONFIRMATION = :confirmation + ERROR = :error + RECEIVED = :received + + # error indicating failure to send email + class FailureToSend < StandardError; end + + def monitor_send_failure(error_message, tags:, context: nil) + metric = "#{VANotify::NotificationEmail::STATSD}.failure" + payload = { + statsd: metric, + error_message:, + context: + } + + StatsD.increment(metric, tags:) + Rails.logger.error('VANotify::NotificationEmail #send failure!', **payload) + end + end +end diff --git a/lib/va_notify/notification_email/saved_claim.rb b/lib/va_notify/notification_email/saved_claim.rb index a6139ff9a5..7eabe17a3b 100644 --- a/lib/va_notify/notification_email/saved_claim.rb +++ b/lib/va_notify/notification_email/saved_claim.rb @@ -1,45 +1,25 @@ # frozen_string_literal: true +require 'va_notify/notification_email' + module VANotify module NotificationEmail - STATSD = 'api.va_notify.notification_email' - - CONFIRMATION = :confirmation - ERROR = :error - RECEIVED = :received - - # error indicating failure to send email - class FailureToSend < StandardError; end - - def monitor_send_failure(error_message, tags:, context: {}) - metric = "#{VANotify::NotificationEmail::STATSD}.failure" - StatsD.increment(metric, tags:) - - payload = { - statsd: metric, - error_message:, - context: - } - Rails.logger.error('VANotify::NotificationEmail #send failure!', **payload) - end - class SavedClaim - def initialize(saved_claim, user: nil, service_name: nil) + def initialize(saved_claim, service_name: nil) @claim = saved_claim - @user = user @vanotify_service = service_name @config = Settings.vanotify.services[vanotify_service] raise ArgumentError, "Invalid service_name '#{vanotify_service}'" unless config end - def send(email_type, at: nil) - email_config = config&.email&.[](email_type) + def deliver(email_type, at: nil) + email_config = config&.email[email_type] raise ArgumentError, "Invalid email_type '#{email_type}'" unless email_config email_template_id = able_to_send?(email_config) return unless email_template_id - at ? enqueue_email(email_template_id, at) : send_email_now(email_template_id) + at ? enqueue_email(email_template_id, at) : send_email(email_template_id) claim.insert_notification(email_config.template_id) rescue => e @@ -57,7 +37,7 @@ def send(email_type, at: nil) private - attr_reader :claim, :config, :user + attr_reader :claim, :config def vanotify_service @vanotify_service ||= claim.form_id.downcase.gsub(/-/, '_') @@ -75,7 +55,7 @@ def able_to_send?(email_config) raise VANotify::NotificationEmail::FailureToSend, 'Notification already sent' end - email_config.template_id if flipper?(email_config.flipper) + email_config.template_id if flipper?(email_config.flipper_id) end def enqueue_email(email_template_id, at) @@ -87,7 +67,7 @@ def enqueue_email(email_template_id, at) ) end - def send_email_now(email_template_id) + def send_email(email_template_id) VANotify::EmailJob.perform_async( email, email_template_id, @@ -96,11 +76,11 @@ def send_email_now(email_template_id) end def email - claim.email || user&.email + claim.email end def first_name - claim.first_name || user&.first_name + claim.first_name end def personalization diff --git a/modules/pensions/app/models/pensions/saved_claim.rb b/modules/pensions/app/models/pensions/saved_claim.rb index ec6b33ff8c..9fc1e8c31c 100644 --- a/modules/pensions/app/models/pensions/saved_claim.rb +++ b/modules/pensions/app/models/pensions/saved_claim.rb @@ -77,29 +77,6 @@ def first_name parsed_form.dig('veteranFullName', 'first') end - ## - # enqueue the sending of the submission confirmation email - # - # @see VANotify::EmailJob - # - def send_confirmation_email - if email.blank? || va_notification?(Settings.vanotify.services.va_gov.template_id.form527ez_confirmation_email) - return - end - - VANotify::EmailJob.perform_async( - email, - Settings.vanotify.services.va_gov.template_id.form527ez_confirmation_email, - { - 'first_name' => parsed_form.dig('veteranFullName', 'first')&.upcase.presence, - 'date_submitted' => Time.zone.today.strftime('%B %d, %Y'), - 'confirmation_number' => guid - } - ) - - insert_notification(Settings.vanotify.services.va_gov.template_id.form527ez_confirmation_email) - end - # Run after a claim is saved, this processes any files and workflows that are present # and sends them to our internal partners for processing. # Only removed Sidekiq call from super diff --git a/modules/pensions/app/sidekiq/pensions/pension_benefit_intake_job.rb b/modules/pensions/app/sidekiq/pensions/pension_benefit_intake_job.rb index 3bbf47fdd6..675a948fe3 100644 --- a/modules/pensions/app/sidekiq/pensions/pension_benefit_intake_job.rb +++ b/modules/pensions/app/sidekiq/pensions/pension_benefit_intake_job.rb @@ -4,6 +4,7 @@ require 'lighthouse/benefits_intake/metadata' require 'pensions/tag_sentry' require 'pensions/monitor' +require 'pensions/notification_email' require 'pdf_utilities/datestamp_pdf' module Pensions @@ -202,7 +203,8 @@ def form_submission_polling # Being VANotify job to send email to veteran # def send_confirmation_email - @claim.respond_to?(:send_confirmation_email) && @claim.send_confirmation_email + Pensions::NotificationEmail.new(@claim).deliver(:confirmation) + # @claim.respond_to?(:send_confirmation_email) && @claim.send_confirmation_email rescue => e @pension_monitor.track_send_confirmation_email_failure(@claim, @intake_service, @user_account_uuid, e) end diff --git a/modules/pensions/lib/pensions/notification_email.rb b/modules/pensions/lib/pensions/notification_email.rb index b558041945..8f0d045ee3 100644 --- a/modules/pensions/lib/pensions/notification_email.rb +++ b/modules/pensions/lib/pensions/notification_email.rb @@ -6,8 +6,8 @@ module Pensions class NotificationEmail < ::VANotify::NotificationEmail::SavedClaim # @see VANotify::NotificationEmail::SavedClaim # pass thru to super class, no additional processing needed - def initialize(_saved_claim) - super(claim, 'pensions') + def initialize(saved_claim) + super(saved_claim, service_name: 'pensions') end end end diff --git a/modules/pensions/spec/models/pensions/saved_claim_spec.rb b/modules/pensions/spec/models/pensions/saved_claim_spec.rb index 87ba96dca5..1924a1a9fa 100644 --- a/modules/pensions/spec/models/pensions/saved_claim_spec.rb +++ b/modules/pensions/spec/models/pensions/saved_claim_spec.rb @@ -63,24 +63,6 @@ expect { claim.destroy }.to change(PersistentAttachment, :count).by(-2) end end - - it '#send_confirmation_email' do - allow(VANotify::EmailJob).to receive(:perform_async) - allow(Settings.vanotify.services.va_gov.template_id).to receive(:form527ez_confirmation_email).and_return(0) - - claim.send_confirmation_email - claim.send_confirmation_email - - expect(VANotify::EmailJob).to have_received(:perform_async).with( - 'foo@foo.com', - 0, - { - 'first_name' => 'TEST', - 'date_submitted' => Time.zone.today.strftime('%B %d, %Y'), - 'confirmation_number' => claim.guid - } - ).once - end end describe '#email' do @@ -88,4 +70,10 @@ expect(instance.email).to eq('foo@foo.com') end end + + describe '#first_name' do + it 'returns the users first name' do + expect(instance.first_name).to eq('Test') + end + end end diff --git a/modules/pensions/spec/sidekiq/pensions/pension_benefit_intake_job_spec.rb b/modules/pensions/spec/sidekiq/pensions/pension_benefit_intake_job_spec.rb index 84123a1006..da93428348 100644 --- a/modules/pensions/spec/sidekiq/pensions/pension_benefit_intake_job_spec.rb +++ b/modules/pensions/spec/sidekiq/pensions/pension_benefit_intake_job_spec.rb @@ -3,6 +3,7 @@ require 'rails_helper' require 'lighthouse/benefits_intake/service' require 'lighthouse/benefits_intake/metadata' +require 'pensions/notification_email' RSpec.describe Pensions::PensionBenefitIntakeJob, :uploader_helpers do stub_virus_scan @@ -161,16 +162,21 @@ describe '#send_confirmation_email' do let(:monitor_error) { create(:monitor_error) } + let(:notification) { double('notification') } before do job.instance_variable_set(:@claim, claim) - allow(claim).to receive(:send_confirmation_email).and_raise(monitor_error) + + allow(Pensions::NotificationEmail).to receive(:new).and_return(notification) + allow(notification).to receive(:deliver).and_raise(monitor_error) job.instance_variable_set(:@pension_monitor, monitor) allow(monitor).to receive(:track_send_confirmation_email_failure) end it 'errors and logs but does not reraise' do + expect(Pensions::NotificationEmail).to receive(:new).with claim + expect(notification).to receive(:deliver).with(:confirmation) expect(monitor).to receive(:track_send_confirmation_email_failure) job.send(:send_confirmation_email) end