Skip to content

Commit

Permalink
general save
Browse files Browse the repository at this point in the history
  • Loading branch information
SamStuckey committed Oct 8, 2024
1 parent 712176e commit a4b0432
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 72 deletions.
3 changes: 2 additions & 1 deletion app/models/form526_submission_remediation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Form526SubmissionRemediation < ApplicationRecord

STATSD_KEY_PREFIX = 'form526_submission_remediation'

enum remediation_type: { manual: 0, ignored_as_duplicate: 1, email_notification_to_vet: 2 }
# [wipn8923] keep
# enum remediation_type: { manual: 0, ignored_as_duplicate: 1, email_notification_to_vet: 2 }

def mark_as_unsuccessful(context)
self.success = false
Expand Down
21 changes: 1 addition & 20 deletions app/sidekiq/form526_status_polling_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def handle_submission(status, form_submission)
if %w[error expired].include? status
log_result('failure')
form_submission.rejected!
notify_veteran
Form526SubmissionFailureEmail.perform_async(form526_submission_id: form_submission.id)
elsif status == 'vbms'
log_result('true_success')
form_submission.accepted!
Expand All @@ -69,27 +69,8 @@ def handle_submission(status, form_submission)
end
end


def log_result(result)
StatsD.increment("#{STATS_KEY}.526.#{result}")
StatsD.increment("#{STATS_KEY}.all_forms.#{result}")
end

def notify_veteran
first_name = parsed_form.dig('veteranFullName', 'first')
# [wipn8923] form526_submission_failure_notification_template_id is not defined anywhere
Settings.vanotify.services.benefits_disability.template_id.form526_submission_failure_notification_template_id
api_key = Settings.vanotify.services.benefits_disability.api_key
salutation = first_name ? "Dear #{first_name}," : ''

VANotify::EmailJob.perform_async(
email,
template_id,
{ 'salutation' => salutation },
api_key
)
Form526SubmissionRemediation.create!(form526_submission_id:,
type: :failure_email,
lifecycle: ["failed with error: #{error_message}"])
end
end
125 changes: 125 additions & 0 deletions app/sidekiq/form526_submission_failure_email_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# frozen_string_literal: true

require 'va_notify/service'

# [wipn8923] new job
class Form526SubmissionFailureEmail
include Sidekiq::Job

attr_reader :submission_id

STATSD_METRIC_PREFIX = 'api.form_526.veteran_notifications.form526_submission_failure_email'

sidekiq_options retry: 14

sidekiq_retries_exhausted do |msg, _ex|
job_id = msg['jid']
error_class = msg['error_class']
error_message = msg['error_message']
form526_submission_id = msg['args'].first

timestamp = Time.now.utc

new_error = {

Check failure on line 23 in app/sidekiq/form526_submission_failure_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/UselessAssignment: Useless assignment to variable - `new_error`.
"#{timestamp.to_i}": {
caller_method: __method__.to_s,
timestamp:,
form526_submission_id:
}
}

Rails.logger.warn(
'Form526SubmissionFailureEmailJob retries exhausted',
{
job_id:,
timestamp:,
form526_submission_id:,
error_class:,
error_message:
}
)

StatsD.increment("#{STATSD_METRIC_PREFIX}.exhausted")
rescue => e
Rails.logger.error(
'Failure in Form526SubmissionFailureEmailJob#sidekiq_retries_exhausted',
{
job_id:,
messaged_content: e.message,
submission_id: submission_id,

Check failure on line 49 in app/sidekiq/form526_submission_failure_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Style/HashSyntax: Omit the hash value.
pre_exhaustion_failure: {
error_class:,
error_message:
}
}
)
raise e
end

def initialize(submission_id)
@submission_id = submission_id
end

def perform
send_email
track_remedial_action
log_dispatch
rescue => e
log_failure(e)
raise
end

private

def submission
@submission ||= Form526Submission.find(submission_id)
end

def send_email(submission)
email_client = VaNotify::Service.new(Settings.vanotify.services.benefits_disability.api_key)
template_id = Settings.vanotify.services.benefits_disability.template_id
.form526_submission_failure_notification_template_id

Check failure on line 81 in app/sidekiq/form526_submission_failure_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/MultilineMethodCallIndentation: Align `.form526_submission_failure_notification_template_id` with `.vanotify` on line 80.
email_address = submission.veteran_email_address
first_name = submission.get_first_name
date_submitted = submission.format_creation_time_for_mailers
email_client.send_email(
email_address:,
template_id:,
personalisation: {
first_name:,
date_submitted:
}
)
end

def track_remedial_action
Form526SubmissionRemediation.create!(form526_submission: submission,
ignored_as_duplicate: true,

Check failure on line 97 in app/sidekiq/form526_submission_failure_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.

Check failure on line 97 in app/sidekiq/form526_submission_failure_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/HashAlignment: Align the keys of a hash literal if they span more than one line.
lifecycle: ["failed with error: #{error_message}"])

Check failure on line 98 in app/sidekiq/form526_submission_failure_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.

Check failure on line 98 in app/sidekiq/form526_submission_failure_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/HashAlignment: Align the keys of a hash literal if they span more than one line.
end

def log_dispatch(form526_submission_id)

Check failure on line 101 in app/sidekiq/form526_submission_failure_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/UnusedMethodArgument: Unused method argument - `form526_submission_id`. If it's necessary, use `_` or `_form526_submission_id` as an argument name to indicate that it won't be used. If it's unnecessary, remove it. You can also write as `log_dispatch(*)` if you want the method to accept any arguments but don't care about them.
Rails.logger.info(
'Form526SubmissionFailureEmail notification dispatched',
{
form526_submission_id: submission.id,
timestamp: Time.now.utc
}
)

StatsD.increment("#{STATSD_METRIC_PREFIX}.success")
end

def log_failure(error)
Rails.logger.info(
'Form526SubmissionFailureEmail notification failed',
{
form526_submission_id: submission.id,
error_message: error.try(:message),
timestamp: Time.now.utc
}
)

StatsD.increment("#{STATSD_METRIC_PREFIX}.error")
end
end
17 changes: 1 addition & 16 deletions lib/sidekiq/form526_backup_submission_process/submit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,7 @@ class Submit
{ job_id:, error_class:, error_message:, timestamp:, form526_submission_id: }
)

# [wipn8923] none of this has been checked yet
first_name = parsed_form.dig('veteranFullName', 'first')
# [wipn8923] form526_submission_failure_notification_template_id is not defined anywhere
Settings.vanotify.services.benefits_disability.template_id.form526_submission_failure_notification_template_id
api_key = Settings.vanotify.services.benefits_disability.api_key
salutation = first_name ? "Dear #{first_name}," : ''

VANotify::EmailJob.perform_async(
email,
template_id,
{ 'salutation' => salutation },
api_key
)
Form526SubmissionRemediation.create!(form526_submission_id:,
type: :failure_email,
lifecycle: ["failed with error: #{error_message}"])
Form526SubmissionFailureEmail.perform_async(form526_submission_id: form_submission.id)
rescue => e
::Rails.logger.error(
'Failure in Form526BackupSubmission#sidekiq_retries_exhausted',
Expand Down
101 changes: 66 additions & 35 deletions spec/sidekiq/form526_status_polling_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,41 @@
end

context 'polling on pending submissions' do
let(:api_response) do
{
'data' => [
{
'id' => backup_submission_a.backup_submitted_claim_id,
'attributes' => {
'guid' => backup_submission_a.backup_submitted_claim_id,
'status' => 'vbms'
}
},
{
'id' => backup_submission_b.backup_submitted_claim_id,
'attributes' => {
'guid' => backup_submission_b.backup_submitted_claim_id,
'status' => 'success'
}
},
{
'id' => backup_submission_c.backup_submitted_claim_id,
'attributes' => {
'guid' => backup_submission_c.backup_submitted_claim_id,
'status' => 'error'
}
},
{
'id' => backup_submission_d.backup_submitted_claim_id,
'attributes' => {
'guid' => backup_submission_d.backup_submitted_claim_id,
'status' => 'expired'
}
}
]
}
end

describe 'submission to the bulk status report endpoint' do
it 'submits only pending form submissions' do
pending_claim_ids = Form526Submission.pending_backup.pluck(:backup_submitted_claim_id)
Expand Down Expand Up @@ -84,41 +119,6 @@
end

describe 'updating the form 526s local submission state' do
let(:api_response) do
{
'data' => [
{
'id' => backup_submission_a.backup_submitted_claim_id,
'attributes' => {
'guid' => backup_submission_a.backup_submitted_claim_id,
'status' => 'vbms'
}
},
{
'id' => backup_submission_b.backup_submitted_claim_id,
'attributes' => {
'guid' => backup_submission_b.backup_submitted_claim_id,
'status' => 'success'
}
},
{
'id' => backup_submission_c.backup_submitted_claim_id,
'attributes' => {
'guid' => backup_submission_c.backup_submitted_claim_id,
'status' => 'error'
}
},
{
'id' => backup_submission_d.backup_submitted_claim_id,
'attributes' => {
'guid' => backup_submission_d.backup_submitted_claim_id,
'status' => 'expired'
}
}
]
}
end

it 'updates local state to reflect the returned statuses' do
pending_claim_ids = Form526Submission.pending_backup
.pluck(:backup_submitted_claim_id)
Expand All @@ -138,6 +138,37 @@
expect(backup_submission_d.reload.backup_submitted_claim_status).to eq 'rejected'
end
end

# [wipn8923] new spec (polling)
context 'when a failure type response is returned from the API' do
describe 'creating and tracking notifications' do
it 'notifies the veteran and marks the submission as remediated' do
pending_claim_ids = Form526Submission.pending_backup
.pluck(:backup_submitted_claim_id)
response = double

allow(response).to receive(:body).and_return(api_response)
allow_any_instance_of(BenefitsIntakeService::Service)
.to receive(:get_bulk_status_of_uploads)
.with(pending_claim_ids)
.and_return(response)

email_params = {}

Check failure on line 156 in spec/sidekiq/form526_status_polling_job_spec.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/UselessAssignment: Useless assignment to variable - `email_params`.
# allow_any_instance_of(VANotify::EmailJob)
# .to receive(:perform_async)
# .with(email_params)

Form526StatusPollingJob.new.perform

# expect_any_instance_of(VANotify::EmailJob).to have_received(:perform)

expect(backup_submission_a.reload.remediated?).to eq false
expect(backup_submission_b.reload.remediated?).to eq false
expect(backup_submission_c.reload.remediated?).to eq true
expect(backup_submission_d.reload.remediated?).to eq true
end
end
end
end
end
end
Loading

0 comments on commit a4b0432

Please sign in to comment.