Skip to content
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
22 changes: 11 additions & 11 deletions app/models/health_care_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,19 @@ def log_submission_failure_details
def send_failure_email
first_name = parsed_form.dig('veteranFullName', 'first')
template_id = Settings.vanotify.services.health_apps_1010.template_id.form1010_ez_failure_email
api_key = Settings.vanotify.services.health_apps_1010.api_key
personalisation = { 'salutation' => first_name ? "Dear #{first_name}," : '' }
metadata = { callback_metadata: { notification_type: 'error', form_number: FORM_ID, statsd_tags: DD_ZSF_TAGS } }

salutation = first_name ? "Dear #{first_name}," : ''
metadata =
{
callback_metadata: {
notification_type: 'error',
form_number: FORM_ID,
statsd_tags: DD_ZSF_TAGS
}
}
if Flipper.enabled?(:va_notify_v2_health_care_application_model_send_failure_email)
VANotify::V2::QueueEmailJob.enqueue(
email, template_id, personalisation,
'Settings.vanotify.services.health_apps_1010.api_key', metadata
)
Comment on lines +317 to +321
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send_failure_email hard-codes the API key path string ('Settings.vanotify.services.health_apps_1010.api_key') inline for the V2 job. Elsewhere the codebase uses an API_KEY_PATH constant for this (e.g., app/sidekiq/hca/ezr_submission_job.rb and app/sidekiq/form1010cg/submission_job.rb), which avoids typos and makes future refactors safer. Consider defining a constant on HealthCareApplication (or reusing an existing constant) and using it for the V2 enqueue call (and potentially the V1 path too).

Copilot uses AI. Check for mistakes.
else
api_key = Settings.vanotify.services.health_apps_1010.api_key
VANotify::EmailJob.perform_async(email, template_id, personalisation, api_key, metadata)
end
Comment on lines 312 to +325
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method reads Settings values (template_id and, when the flag is off, api_key). In this repo Settings values can come through Parameter Store with unexpected types due to env_parse_values (e.g., integers/strings), so it’s safer to explicitly coerce/validate these before using them (e.g., ensure they’re non-blank Strings and fail fast/log clearly if not). This will help avoid silent misconfiguration causing notification failures at runtime.

Copilot uses AI. Check for mistakes.

VANotify::EmailJob.perform_async(email, template_id, { 'salutation' => salutation }, api_key, metadata)
StatsD.increment("#{HCA::Service::STATSD_KEY_PREFIX}.submission_failure_email_sent")
rescue => e
Rails.logger.error('[10-10EZ] - Failure sending Submission Failure Email', { exception: e })
Expand Down
3 changes: 3 additions & 0 deletions config/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,9 @@ features:
va_notify_v2_form1010ezr_submission:
actor_type: user
description: If enabled, HCA::EzrSubmissionJob will use VANotify::V2::QueueEmailJob instead of VANotify::EmailJob
va_notify_v2_health_care_application_model_send_failure_email:
actor_type: user
description: If enabled, HealthCareApplication will use VANotify::V2::QueueEmailJob instead of VANotify::EmailJob
va_notify_v2_in_progress_form_reminder:
actor_type: user
description: If enabled, InProgressFormReminder uses VANotify::V2::QueueUserAccountJob instead of VANotify::UserAccountJob
Expand Down
136 changes: 77 additions & 59 deletions spec/models/health_care_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ def self.expect_job_submission(job)

before do
allow(VANotify::EmailJob).to receive(:perform_async)
allow(VANotify::V2::QueueEmailJob).to receive(:enqueue)
allow_any_instance_of(MPI::Service).to receive(
:find_profile_by_attributes
).and_return(
Expand All @@ -738,7 +739,6 @@ def self.expect_job_submission(job)
context 'has form' do
context 'with email address' do
let(:email_address) { health_care_application.parsed_form['email'] }
let(:api_key) { Settings.vanotify.services.health_apps_1010.api_key }
let(:template_id) { Settings.vanotify.services.health_apps_1010.template_id.form1010_ez_failure_email }
let(:callback_metadata) do
{
Expand All @@ -749,71 +749,79 @@ def self.expect_job_submission(job)
}
}
end
let(:standard_error) { StandardError.new('Test error') }

let(:template_params) do
[
email_address,
template_id,
{
'salutation' => "Dear #{health_care_application.parsed_form['veteranFullName']['first']},"
},
api_key,
callback_metadata
]
end
context 'when va_notify_v2_health_care_application_model_send_failure_email is enabled' do
before do
allow(Flipper).to receive(:enabled?)
.with(:va_notify_v2_health_care_application_model_send_failure_email).and_return(true)
end

let(:standard_error) { StandardError.new('Test error') }
it 'sends a failure email using V2 QueueEmailJob' do
subject
expect(VANotify::V2::QueueEmailJob).to have_received(:enqueue).with(
email_address,
template_id,
{ 'salutation' => "Dear #{health_care_application.parsed_form['veteranFullName']['first']}," },
'Settings.vanotify.services.health_apps_1010.api_key',
callback_metadata
)
end

it 'sends a failure email to the email address provided on the form' do
subject
expect(VANotify::EmailJob).to have_received(:perform_async).with(*template_params)
end
it 'increments statsd' do
expect { subject }.to trigger_statsd_increment("#{statsd_key_prefix}.submission_failure_email_sent")
end

it 'logs error if email job throws error' do
allow(VANotify::EmailJob).to receive(:perform_async).and_raise(standard_error)
allow(Rails.logger).to receive(:error)
expect(Rails.logger).to receive(:error).with(
'[10-10EZ] - Failure sending Submission Failure Email',
{ exception: standard_error }
)
expect(Rails.logger).to receive(:info).with(
'[10-10EZ] - HCA total failure',
{
first_initial: 'F',
middle_initial: 'M',
last_initial: 'Z'
}
)
expect { subject }.not_to raise_error
end
it 'logs error if email job throws error' do
allow(VANotify::V2::QueueEmailJob).to receive(:enqueue).and_raise(standard_error)
allow(Rails.logger).to receive(:error)
expect(Rails.logger).to receive(:error).with(
'[10-10EZ] - Failure sending Submission Failure Email',
{ exception: standard_error }
)
expect { subject }.not_to raise_error
end

it 'increments statsd' do
expect { subject }.to trigger_statsd_increment("#{statsd_key_prefix}.submission_failure_email_sent")
context 'without first name' do
subject do
health_care_application.parsed_form['veteranFullName'] = nil
super()
end

it 'sends a failure email with empty salutation' do
subject
expect(VANotify::V2::QueueEmailJob).to have_received(:enqueue).with(
email_address,
template_id,
{ 'salutation' => '' },
'Settings.vanotify.services.health_apps_1010.api_key',
callback_metadata
)
end
end
end

context 'without first name' do
subject do
health_care_application.parsed_form['veteranFullName'] = nil
super()
context 'when va_notify_v2_health_care_application_model_send_failure_email is disabled' do
before do
allow(Flipper).to receive(:enabled?)
.with(:va_notify_v2_health_care_application_model_send_failure_email).and_return(false)
end

let(:template_params_no_name) do
[
let(:api_key) { Settings.vanotify.services.health_apps_1010.api_key }

it 'sends a failure email using V1 EmailJob' do
subject
expect(VANotify::EmailJob).to have_received(:perform_async).with(
email_address,
template_id,
{
'salutation' => ''
},
{ 'salutation' => "Dear #{health_care_application.parsed_form['veteranFullName']['first']}," },
api_key,
callback_metadata
]
)
Comment on lines +760 to +820
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The enabled/disabled contexts assert the expected job is called, but they don’t assert the other job is not called. Adding negative expectations (V2 enabled => VANotify::EmailJob not called; V2 disabled => VANotify::V2::QueueEmailJob not called) would better protect against regressions that accidentally send duplicate emails during the migration.

Copilot uses AI. Check for mistakes.
end

let(:standard_error) { StandardError.new('Test error') }

it 'sends a failure email without personalisations to the email address provided on the form' do
subject
expect(VANotify::EmailJob).to have_received(:perform_async).with(*template_params_no_name)
it 'increments statsd' do
expect { subject }.to trigger_statsd_increment("#{statsd_key_prefix}.submission_failure_email_sent")
end

it 'logs error if email job throws error' do
Expand All @@ -823,16 +831,26 @@ def self.expect_job_submission(job)
'[10-10EZ] - Failure sending Submission Failure Email',
{ exception: standard_error }
)
expect(Rails.logger).to receive(:info).with(
'[10-10EZ] - HCA total failure',
{
first_initial: 'no initial provided',
middle_initial: 'no initial provided',
last_initial: 'no initial provided'
}
)
expect { subject }.not_to raise_error
end

context 'without first name' do
subject do
health_care_application.parsed_form['veteranFullName'] = nil
super()
end

it 'sends a failure email with empty salutation' do
subject
expect(VANotify::EmailJob).to have_received(:perform_async).with(
email_address,
template_id,
{ 'salutation' => '' },
api_key,
callback_metadata
)
end
end
end
end

Expand Down
Loading