Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch more errors in NotificationEmail #19640

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,33 @@ 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]
@confirmation_number = config[:confirmation_number]
@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
Expand All @@ -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?
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading