diff --git a/modules/simple_forms_api/app/services/simple_forms_api/notification_email.rb b/modules/simple_forms_api/app/services/simple_forms_api/notification_email.rb index 58c87eeddd5..75814a69eb8 100644 --- a/modules/simple_forms_api/app/services/simple_forms_api/notification_email.rb +++ b/modules/simple_forms_api/app/services/simple_forms_api/notification_email.rb @@ -63,7 +63,10 @@ class NotificationEmail SUPPORTED_FORMS = TEMPLATE_IDS.keys def initialize(config, notification_type: :confirmation, user: nil, user_account: nil) + @notification_type = notification_type + check_missing_keys(config) + check_if_form_is_supported(config) @form_data = config[:form_data] @form_number = config[:form_number] @@ -71,23 +74,22 @@ def initialize(config, notification_type: :confirmation, user: nil, user_account @date_submitted = config[:date_submitted] @expiration_date = config[:expiration_date] @lighthouse_updated_at = config[:lighthouse_updated_at] - @notification_type = notification_type @user = user @user_account = user_account end def send(at: nil) - return unless SUPPORTED_FORMS.include?(form_number) return unless flipper? template_id = TEMPLATE_IDS[form_number][notification_type] return unless template_id - if at - enqueue_email(at, template_id) - else - send_email_now(template_id) - end + sent_to_va_notify = if at + enqueue_email(at, template_id) + else + send_email_now(template_id) + end + StatsD.increment('silent_failure', tags: statsd_tags) if error_notification? && !sent_to_va_notify end private @@ -97,7 +99,17 @@ def check_missing_keys(config) if config[:form_number] == 'vba_21_0966_intent_api' && config[:expiration_date].nil? missing_keys << :expiration_date end - raise ArgumentError, "Missing keys: #{missing_keys.join(', ')}" if missing_keys.any? + if missing_keys.any? + StatsD.increment('silent_failure', tags: statsd_tags) if error_notification? + raise ArgumentError, "Missing keys: #{missing_keys.join(', ')}" + end + end + + def check_if_form_is_supported(config) + unless SUPPORTED_FORMS.include?(config[:form_number]) + StatsD.increment('silent_failure', tags: statsd_tags) if error_notification? + raise ArgumentError, "Unsupported form: given form number was #{config[:form_number]}" + end end def flipper? @@ -397,5 +409,9 @@ def form40_10007_first_name def statsd_tags { 'service' => 'veteran-facing-forms', 'function' => "#{form_number} form submission to Lighthouse" } end + + def error_notification? + notification_type == :error + end end end diff --git a/modules/simple_forms_api/spec/services/notification_email_spec.rb b/modules/simple_forms_api/spec/services/notification_email_spec.rb index f8aae6aeb10..db5b8669a3e 100644 --- a/modules/simple_forms_api/spec/services/notification_email_spec.rb +++ b/modules/simple_forms_api/spec/services/notification_email_spec.rb @@ -3,6 +3,15 @@ require 'rails_helper' require SimpleFormsApi::Engine.root.join('spec', 'spec_helper.rb') +shared_examples 'an error notification email' do + it 'increments StatsD' do + allow(StatsD).to receive(:increment) + + expect { described_class.new(config, notification_type: :error) }.to raise_error(ArgumentError) + expect(StatsD).to have_received(:increment).with('silent_failure', tags: anything) + end +end + describe SimpleFormsApi::NotificationEmail do %i[confirmation error received].each do |notification_type| describe '#initialize' do @@ -26,6 +35,8 @@ it 'fails' do expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError) end + + it_behaves_like 'an error notification email' if notification_type == :error end context 'missing form_number' do @@ -37,6 +48,8 @@ it 'fails' do expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError) end + + it_behaves_like 'an error notification email' if notification_type == :error end context 'missing confirmation_number' do @@ -47,6 +60,8 @@ it 'fails' do expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError) end + + it_behaves_like 'an error notification email' if notification_type == :error end context 'missing date_submitted' do @@ -57,6 +72,21 @@ it 'fails' do expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError) end + + it_behaves_like 'an error notification email' if notification_type == :error + end + + context 'form not supported' do + let(:config) do + { form_data: {}, form_number: 'nonsense', confirmation_number: 'confirmation_number', + date_submitted: Time.zone.today.strftime('%B %d, %Y') } + end + + it 'fails' do + expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError) + end + + it_behaves_like 'an error notification email' if notification_type == :error end end @@ -89,6 +119,25 @@ expect(VANotify::EmailJob).to have_received(:perform_async) end + + context 'did not send to VA Notify because of no first name', if: notification_type == :error do + let(:profile) { double(given_names: []) } + let(:mpi_profile) { double(profile:, error: nil) } + + it 'increments StatsD' do + data['witness_full_name']['first'] = nil + allow(VANotify::EmailJob).to receive(:perform_async) + allow(VANotify::UserAccountJob).to receive(:perform_at) + allow_any_instance_of(MPI::Service).to receive(:find_profile_by_identifier).and_return(mpi_profile) + allow(StatsD).to receive(:increment) + + subject = described_class.new(config, notification_type:) + subject.send + + expect(VANotify::EmailJob).not_to have_received(:perform_async) + expect(StatsD).to have_received(:increment).with('silent_failure', tags: anything) + end + end end context 'flipper is off' do