diff --git a/app/models/health_care_application.rb b/app/models/health_care_application.rb index 64c4ebbf547..dc64848c0b4 100644 --- a/app/models/health_care_application.rb +++ b/app/models/health_care_application.rb @@ -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 + ) + else + api_key = Settings.vanotify.services.health_apps_1010.api_key + VANotify::EmailJob.perform_async(email, template_id, personalisation, api_key, metadata) + end - 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 }) diff --git a/config/features.yml b/config/features.yml index 895f63dd0a4..f84e07ce01e 100644 --- a/config/features.yml +++ b/config/features.yml @@ -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 diff --git a/spec/models/health_care_application_spec.rb b/spec/models/health_care_application_spec.rb index d8f6af98f08..640f66c4f64 100644 --- a/spec/models/health_care_application_spec.rb +++ b/spec/models/health_care_application_spec.rb @@ -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( @@ -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 { @@ -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 - ] + ) 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 @@ -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