-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ndbex/95814 add va notify webhook (#21065)
* level set * level set with master * removing yarn.lock * FD * pulling in main * Levelset * state fix * Added feature flag for the dependents action needed email callback * Decided to go the simpler route and not implement a custom callback at this time * Setup email failure job * Added in a flipper feature falg and check for it in the senf_failure_email method in dependency claim * All tests are passing! Huzzahgit add . * Change to the file name of the job spec * Fix as flipper was calling a failure when calling send_failure_email * codeowners file change * meant to be sidekiq and not it's spec * whoops * more codeowners * Called personlisation from the claim.send_failure_email method * disabled :dependents_failure_callback_email flipper in before block * Removed callback_options from the submit_central_form686c job * Added context to the flipper * Rubocop * Refactor for allow(Flipper) --------- Co-authored-by: micahaspyr <[email protected]>
- Loading branch information
1 parent
7076695
commit 84264e4
Showing
7 changed files
with
389 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'va_notify/service' | ||
|
||
class Dependents::Form686c674FailureEmailJob | ||
include Sidekiq::Job | ||
|
||
FORM_ID = '686C-674' | ||
FORM_ID_674 = '21-674' | ||
STATSD_KEY_PREFIX = 'api.dependents.form_686c_674_failure_email_job' | ||
ZSF_DD_TAG_FUNCTION = '686c_674_failure_email_queuing' | ||
|
||
sidekiq_options retry: 16 | ||
|
||
sidekiq_retries_exhausted do |msg, ex| | ||
Rails.logger.error('Form686c674FailureEmailJob exhausted all retries', | ||
{ | ||
saved_claim_id: msg['args'].first, | ||
error_message: ex.message | ||
}) | ||
end | ||
|
||
def perform(claim_id, email, template_id, personalisation) | ||
@claim = SavedClaim::DependencyClaim.find(claim_id) | ||
va_notify_client.send_email(email_address: email, | ||
template_id:, | ||
personalisation:) | ||
rescue => e | ||
Rails.logger.warn('Form686c674FailureEmailJob failed, retrying send...', { claim_id:, error: e }) | ||
end | ||
|
||
private | ||
|
||
def va_notify_client | ||
@va_notify_client ||= VaNotify::Service.new(Settings.vanotify.services.va_gov.api_key, callback_options) | ||
end | ||
|
||
def callback_options | ||
{ | ||
callback_metadata: { | ||
notification_type: 'error', | ||
form_id: @claim.form_id, | ||
statsd_tags: { service: 'dependent-change', function: ZSF_DD_TAG_FUNCTION } | ||
} | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
spec/sidekiq/dependents/form686c674_failure_email_job_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Dependents::Form686c674FailureEmailJob, type: :job do | ||
let(:job) { described_class.new } | ||
let(:claim_id) { 123 } | ||
let(:email) { '[email protected]' } | ||
let(:template_id) { 'test-template-id' } | ||
let(:claim) do | ||
instance_double( | ||
SavedClaim::DependencyClaim, | ||
form_id: described_class::FORM_ID, | ||
confirmation_number: 'ABCD1234', | ||
parsed_form: { | ||
'veteran_information' => { | ||
'full_name' => { | ||
'first' => 'John' | ||
} | ||
} | ||
} | ||
) | ||
end | ||
let(:personalisation) do | ||
{ | ||
'first_name' => 'JOHN', | ||
'date_submitted' => 'January 01, 2023', | ||
'confirmation_number' => 'ABCD1234' | ||
} | ||
end | ||
let(:va_notify_client) { instance_double(VaNotify::Service) } | ||
|
||
describe '#perform' do | ||
before do | ||
allow(SavedClaim::DependencyClaim).to receive(:find).with(claim_id).and_return(claim) | ||
allow(VaNotify::Service).to receive(:new).and_return(va_notify_client) | ||
allow(va_notify_client).to receive(:send_email) | ||
allow(Rails.logger).to receive(:warn) | ||
end | ||
|
||
it 'sends an email with the correct parameters' do | ||
expect(va_notify_client).to receive(:send_email).with( | ||
email_address: email, | ||
template_id:, | ||
personalisation: { | ||
'first_name' => 'JOHN', | ||
'date_submitted' => 'January 01, 2023', | ||
'confirmation_number' => 'ABCD1234' | ||
} | ||
) | ||
|
||
job.perform(claim_id, email, template_id, personalisation) | ||
end | ||
|
||
context 'when an error occurs' do | ||
before do | ||
allow(va_notify_client).to receive(:send_email).and_raise(StandardError.new('Test error')) | ||
end | ||
|
||
it 'logs the error and allows retry' do | ||
expect(Rails.logger).to receive(:warn).with( | ||
'Form686c674FailureEmailJob failed, retrying send...', | ||
{ claim_id:, error: instance_of(StandardError) } | ||
) | ||
|
||
# Should not raise error | ||
expect { job.perform(claim_id, email, template_id, personalisation) }.not_to raise_error | ||
end | ||
end | ||
end | ||
|
||
describe 'sidekiq_retries_exhausted' do | ||
it 'logs an error when retries are exhausted' do | ||
msg = { 'args' => [claim_id] } | ||
ex = StandardError.new('Test exhausted error') | ||
|
||
expect(Rails.logger).to receive(:error).with( | ||
'Form686c674FailureEmailJob exhausted all retries', | ||
{ | ||
saved_claim_id: claim_id, | ||
error_message: 'Test exhausted error' | ||
} | ||
) | ||
|
||
described_class.sidekiq_retries_exhausted_block.call(msg, ex) | ||
end | ||
end | ||
|
||
describe '#va_notify_client' do | ||
before do | ||
allow(SavedClaim::DependencyClaim).to receive(:find).with(claim_id).and_return(claim) | ||
allow(VaNotify::Service).to receive(:new).and_return(va_notify_client) | ||
|
||
vanotify = double('vanotify') | ||
services = double('services') | ||
va_gov = double('va_gov') | ||
|
||
allow(Settings).to receive(:vanotify).and_return(vanotify) | ||
allow(vanotify).to receive(:services).and_return(services) | ||
allow(services).to receive(:va_gov).and_return(va_gov) | ||
allow(va_gov).to receive(:api_key).and_return('test-api-key') | ||
end | ||
|
||
it 'initializes VaNotify::Service with correct parameters' do | ||
expected_callback_options = { | ||
callback_metadata: { | ||
notification_type: 'error', | ||
form_id: described_class::FORM_ID, | ||
statsd_tags: { service: 'dependent-change', function: described_class::ZSF_DD_TAG_FUNCTION } | ||
} | ||
} | ||
|
||
expect(VaNotify::Service).to receive(:new).with('test-api-key', expected_callback_options) | ||
|
||
# Just allow the job to execute, which should create the client | ||
allow(va_notify_client).to receive(:send_email) | ||
job.perform(claim_id, email, template_id, personalisation) | ||
end | ||
end | ||
|
||
describe '#personalisation' do | ||
before do | ||
allow(SavedClaim::DependencyClaim).to receive(:find).with(claim_id).and_return(claim) | ||
today = double('today') | ||
allow(Time.zone).to receive(:today).and_return(today) | ||
allow(today).to receive(:strftime).with('%B %d, %Y').and_return('January 01, 2023') | ||
# Create the service but don't set expectations on send_email yet | ||
allow(VaNotify::Service).to receive(:new).and_return(va_notify_client) | ||
end | ||
|
||
it 'sends correct personalisation data in the email' do | ||
# Instead of directly calling the private method, test what gets sent to va_notify_client | ||
expect(va_notify_client).to receive(:send_email).with( | ||
email_address: email, | ||
template_id:, | ||
personalisation: | ||
) | ||
|
||
job.perform(claim_id, email, template_id, { | ||
'first_name' => 'JOHN', | ||
'date_submitted' => 'January 01, 2023', | ||
'confirmation_number' => 'ABCD1234' | ||
}) | ||
end | ||
|
||
context 'when first name is nil' do | ||
let(:claim) do | ||
instance_double( | ||
SavedClaim::DependencyClaim, | ||
form_id: described_class::FORM_ID, | ||
confirmation_number: 'ABCD1234', | ||
parsed_form: { | ||
'veteran_information' => { | ||
'full_name' => { | ||
'first' => nil | ||
} | ||
} | ||
} | ||
) | ||
end | ||
|
||
it 'handles nil first_name gracefully' do | ||
expect(va_notify_client).to receive(:send_email).with( | ||
email_address: email, | ||
template_id:, | ||
personalisation: hash_including('first_name' => nil) | ||
) | ||
personalisation['first_name'] = nil | ||
job.perform(claim_id, email, template_id, personalisation) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.