Skip to content

Commit

Permalink
Catch more errors in NotificationEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrillberg committed Nov 27, 2024
1 parent a654614 commit a65e30b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 8 deletions.
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
75 changes: 75 additions & 0 deletions modules/simple_forms_api/spec/services/notification_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
it 'fails' do
expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
end

context 'error email', if: notification_type == :error do
it 'increments StatsD' do
allow(StatsD).to receive(:increment)

expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
expect(StatsD).to have_received(:increment).with('silent_failure', tags: anything)
end
end
end

context 'missing form_number' do
Expand All @@ -37,6 +46,15 @@
it 'fails' do
expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
end

context 'error email', if: notification_type == :error do
it 'increments StatsD' do
allow(StatsD).to receive(:increment)

expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
expect(StatsD).to have_received(:increment).with('silent_failure', tags: anything)
end
end
end

context 'missing confirmation_number' do
Expand All @@ -47,6 +65,15 @@
it 'fails' do
expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
end

context 'error email', if: notification_type == :error do
it 'increments StatsD' do
allow(StatsD).to receive(:increment)

expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
expect(StatsD).to have_received(:increment).with('silent_failure', tags: anything)
end
end
end

context 'missing date_submitted' do
Expand All @@ -57,6 +84,35 @@
it 'fails' do
expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
end

context 'error email', if: notification_type == :error do
it 'increments StatsD' do
allow(StatsD).to receive(:increment)

expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
expect(StatsD).to have_received(:increment).with('silent_failure', tags: anything)
end
end
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

context 'error email', if: notification_type == :error do
it 'increments StatsD' do
allow(StatsD).to receive(:increment)

expect { described_class.new(config, notification_type:) }.to raise_error(ArgumentError)
expect(StatsD).to have_received(:increment).with('silent_failure', tags: anything)
end
end
end
end

Expand Down Expand Up @@ -89,6 +145,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

0 comments on commit a65e30b

Please sign in to comment.