Class: CentralMail::SubmitForm4142Job

Inherits:
EVSS::DisabilityCompensationForm::Job show all
Extended by:
Logging::ThirdPartyTransaction::MethodWrapper
Defined in:
app/sidekiq/central_mail/submit_form4142_job.rb

Defined Under Namespace

Classes: BenefitsIntake4142Error, CentralMailResponseError

Constant Summary collapse

INITIAL_FAILURE_EMAIL =
:form526_send_4142_failure_notification
POLLING_FLIPPER_KEY =
:disability_526_form4142_polling_records
POLLED_FAILURE_EMAIL =
:disability_526_form4142_polling_record_failure_email
FORM4142_FORMSUBMISSION_TYPE =
"#{Form526Submission::FORM_526}_#{Form526Submission::FORM_4142}".freeze
ZSF_DD_TAG_FUNCTION =
'526_form_4142_upload_failure_email_queuing'
CENTRAL_MAIL_STATSD_KEY_PREFIX =
'worker.evss.submit_form4142'
LIGHTHOUSE_STATSD_KEY_PREFIX =
'worker.lighthouse.submit_form4142'

Constants included from Sidekiq::Form526JobStatusTracker::JobTracker

Sidekiq::Form526JobStatusTracker::JobTracker::STATSD_KEY_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging::ThirdPartyTransaction::MethodWrapper

wrap_with_logging

Methods inherited from EVSS::DisabilityCompensationForm::Job

#submission

Methods included from Sidekiq::Form526JobStatusTracker::JobTracker

#error_message, #job_success, #job_try, #klass, #log_error, #log_info, #metrics, #non_retryable_error_handler, #retryable_error_handler, #update_background_job_errors, #upsert_job_status, #with_tracking

Methods included from Sidekiq::Form526JobStatusTracker::BackupSubmission

#send_backup_submission_if_enabled

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Instance Attribute Details

#submission_idObject

this is required to make instance variables available to logs via the wrap_with_logging method



25
26
27
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 25

def submission_id
  @submission_id
end

Instance Method Details

#create_form_submission_attempt(form526_submission) ⇒ Object (private)



248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 248

def create_form_submission_attempt(form526_submission)
  form_submission = form526_submission.saved_claim.form_submissions.find_by(form_type: FORM4142_FORMSUBMISSION_TYPE)
  if form_submission.blank?
    form_submission = FormSubmission.create(
      form_type: FORM4142_FORMSUBMISSION_TYPE, # form526_form4142
      form_data: '{}', # we have this already in the Form526Submission.form['form4142']
      user_account: form526_submission.,
      saved_claim: form526_submission.saved_claim
    )
  end
  FormSubmissionAttempt.create(form_submission:, benefits_intake_uuid: lighthouse_service.uuid)
end

#create_service_error(key, source, response, _error = nil) ⇒ Object (private)



234
235
236
237
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 234

def create_service_error(key, source, response, _error = nil)
  response_values = response_values(key, source, response.status, response.body)
  CentralMailResponseError.new(key, response_values, nil, nil)
end

#determine_zipObject (private)



212
213
214
215
216
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 212

def determine_zip
  submission.form.dig('form526', 'form526', 'veteran', 'currentMailingAddress', 'zipFirstFive') ||
    submission.form.dig('form526', 'form526', 'veteran', 'mailingAddress', 'zipFirstFive') ||
    '00000'
end

#generate_metadataObject (private)



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 194

def 
  vet_name = submission.full_name
  filenumber = submission.auth_headers['va_eauth_birlsfilenumber']

   = {
    'veteranFirstName' => vet_name[:first],
    'veteranLastName' => vet_name[:last],
    'zipCode' => determine_zip,
    'source' => 'Form526Submission va.gov',
    'docType' => '4142',
    'businessLine' => '',
    'fileNumber' => filenumber
  }

  SimpleFormsApiSubmission::MetadataValidator
    .validate(, zip_code_is_us_based: usa_based?)
end

#handle_service_exception(response) ⇒ Object (private)

Cannot move job straight to dead queue dynamically within an executing job raising error for all the exceptions as sidekiq will then move into dead queue after all retries are exhausted



229
230
231
232
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 229

def handle_service_exception(response)
  error = create_service_error(nil, self.class, response)
  raise error
end

#lighthouse_serviceObject (private)



161
162
163
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 161

def lighthouse_service
  @lighthouse_service ||= BenefitsIntakeService::Service.new(with_upload_location: true)
end

#payload_hash(lighthouse_service_location) ⇒ Object (private)



165
166
167
168
169
170
171
172
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 165

def payload_hash(lighthouse_service_location)
  {
    upload_url: lighthouse_service_location,
    file: { file: @pdf_path, file_name: @pdf_path.split('/').last },
    metadata: .to_json,
    attachments: []
  }
end

#perform(submission_id) ⇒ Object

Performs an asynchronous job for submitting a Form 4142 to central mail service

Parameters:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 115

def perform(submission_id)
  @submission_id = submission_id

  Sentry.set_tags(source: '526EZ-all-claims')
  super(submission_id)

  with_tracking('Form4142 Submission', submission.saved_claim_id, submission.id) do
    @pdf_path = processor.pdf_path
    response = upload_to_api
    handle_service_exception(response) if response_can_be_logged(response)
  end
rescue => e
  # Cannot move job straight to dead queue dynamically within an executing job
  # raising error for all the exceptions as sidekiq will then move into dead queue
  # after all retries are exhausted
  retryable_error_handler(e)
  raise e
ensure
  File.delete(@pdf_path) if File.exist?(@pdf_path.to_s)
end

#processorObject (private)



145
146
147
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 145

def processor
  @processor ||= EVSS::DisabilityCompensationForm::Form4142Processor.new(submission, jid)
end

#response_can_be_logged(response) ⇒ Object (private)



138
139
140
141
142
143
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 138

def response_can_be_logged(response)
  response.present? &&
    response.respond_to?(:status) &&
    response.status.respond_to?(:between?) &&
    response.status.between?(201, 600)
end

#response_values(key, source, status, detail) ⇒ Object (private)



239
240
241
242
243
244
245
246
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 239

def response_values(key, source, status, detail)
  {
    status:,
    detail:,
    code: key,
    source: source.to_s
  }
end

#upload_to_apiObject (private)



149
150
151
152
153
154
155
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 149

def upload_to_api
  if Flipper.enabled?(:disability_compensation_form4142_supplemental)
    upload_to_lighthouse
  else
    upload_to_central_mail
  end
end

#upload_to_central_mailObject (private)



157
158
159
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 157

def upload_to_central_mail
  CentralMail::Service.new.upload(processor.request_body)
end

#upload_to_lighthouseObject (private)



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 174

def upload_to_lighthouse
  log_info = { benefits_intake_uuid: lighthouse_service.uuid, submission_id: @submission_id }

  Rails.logger.info(
    'Successful Form4142 Upload Intake UUID acquired from Lighthouse',
    log_info
  )

  payload = payload_hash(lighthouse_service.location)
  response = lighthouse_service.upload_doc(**payload)

  if Flipper.enabled?(POLLING_FLIPPER_KEY)
    form526_submission = Form526Submission.find(@submission_id)
    form_submission_attempt = create_form_submission_attempt(form526_submission)
    log_info[:form_submission_id] = form_submission_attempt.form_submission.id
  end
  Rails.logger.info('Successful Form4142 Submission to Lighthouse', log_info)
  response
end

#usa_based?Boolean (private)

Returns:

  • (Boolean)


218
219
220
221
222
223
224
# File 'app/sidekiq/central_mail/submit_form4142_job.rb', line 218

def usa_based?
  country =
    submission.form.dig('form526', 'form526', 'veteran', 'currentMailingAddress', 'country') ||
    submission.form.dig('form526', 'form526', 'veteran', 'mailingAddress', 'country')

  %w[USA US].include?(country&.upcase)
end