Class: Lighthouse::SubmitBenefitsIntakeClaim

Inherits:
Object
  • Object
show all
Includes:
SentryLogging, Sidekiq::Job
Defined in:
app/sidekiq/lighthouse/submit_benefits_intake_claim.rb

Defined Under Namespace

Classes: BenefitsIntakeClaimError

Constant Summary collapse

FOREIGN_POSTALCODE =
'00000'
STATSD_KEY_PREFIX =
'worker.lighthouse.submit_benefits_intake_claim'
RETRY =
16

Instance Method Summary collapse

Methods included from SentryLogging

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

Instance Method Details

#check_zipcode(address) ⇒ Object (private)



188
189
190
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 188

def check_zipcode(address)
  address['country'].upcase.in?(%w[USA US])
end

#cleanup_file_pathsObject (private)



183
184
185
186
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 183

def cleanup_file_paths
  Common::FileHelpers.delete_file_if_exists(@pdf_path) if @pdf_path
  @attachment_paths&.each { |p| Common::FileHelpers.delete_file_if_exists(p) }
end

#create_form_submission_attemptObject (private)



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 165

def create_form_submission_attempt
  Rails.logger.info('Lighthouse::SubmitBenefitsIntakeClaim job starting', {
                      claim_id: @claim.id,
                      benefits_intake_uuid: @lighthouse_service.uuid,
                      confirmation_number: @claim.confirmation_number
                    })
  FormSubmissionAttempt.transaction do
    form_submission = FormSubmission.create(
      form_type: @claim.form_id,
      form_data: @claim.to_json,
      saved_claim: @claim,
      saved_claim_id: @claim.id
    )
    @form_submission_attempt = FormSubmissionAttempt.create(form_submission:,
                                                            benefits_intake_uuid: @lighthouse_service.uuid)
  end
end

#generate_log_details(e = nil) ⇒ Object (private)



155
156
157
158
159
160
161
162
163
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 155

def generate_log_details(e = nil)
  details = {
    claim_id: @claim&.id,
    benefits_intake_uuid: @lighthouse_service&.uuid,
    confirmation_number: @claim&.confirmation_number
  }
  details['error'] = e.message if e
  details
end

#generate_metadataObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 79

def 
  form = @claim.parsed_form
  veteran_full_name = form['veteranFullName']
  address = form['claimantAddress'] || form['veteranAddress']

  # also validates/manipulates the metadata
  BenefitsIntake::Metadata.generate(
    veteran_full_name['first'],
    veteran_full_name['last'],
    form['vaFileNumber'] || form['veteranSocialSecurityNumber'],
    address['postalCode'],
    "#{@claim.class} va.gov",
    @claim.form_id,
    @claim.business_line
  )
end

#init(saved_claim_id) ⇒ Object



73
74
75
76
77
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 73

def init(saved_claim_id)
  @claim = SavedClaim.find(saved_claim_id)

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

#lighthouse_service_upload_payloadObject (private)



130
131
132
133
134
135
136
137
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 130

def lighthouse_service_upload_payload
  {
    upload_url: @lighthouse_service.location,
    file: split_file_and_path(@pdf_path),
    metadata: .to_json,
    attachments: @attachment_paths.map(&method(:split_file_and_path))
  }
end

#perform(saved_claim_id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 45

def perform(saved_claim_id)
  init(saved_claim_id)

  # Create document stamps
  @pdf_path = process_record(@claim)

  @attachment_paths = @claim.persistent_attachments.map { |record| process_record(record) }

  create_form_submission_attempt

  response = @lighthouse_service.upload_doc(**lighthouse_service_upload_payload)
  raise BenefitsIntakeClaimError, response.body unless response.success?

  Rails.logger.info('Lighthouse::SubmitBenefitsIntakeClaim succeeded', generate_log_details)
  StatsD.increment("#{STATSD_KEY_PREFIX}.success")

  send_confirmation_email

  @lighthouse_service.uuid
rescue => e
  Rails.logger.warn('Lighthouse::SubmitBenefitsIntakeClaim failed, retrying...', generate_log_details(e))
  StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
  @form_submission_attempt&.fail!
  raise
ensure
  cleanup_file_paths
end

#process_record(record) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 96

def process_record(record)
  pdf_path = record.to_pdf
  # coordinates 0, 0 is bottom left of the PDF
  # This is the bottom left of the form, right under the form date, e.g. "AUG 2022"
  stamped_path1 = PDFUtilities::DatestampPdf.new(pdf_path).run(text: 'VA.GOV', x: 5, y: 5,
                                                               timestamp: record.created_at)
  # This is the top right of the PDF, above "OMB approved line"
  stamped_path2 = PDFUtilities::DatestampPdf.new(stamped_path1).run(
    text: 'FDC Reviewed - va.gov Submission',
    x: 400,
    y: 770,
    text_only: true
  )

  document = stamped_path2

  if ['21P-530V2'].include?(record.form_id)
    # If you are doing a burial form, add the extra box that is filled out
    document = stamped_pdf_with_form(record.form_id, stamped_path2,
                                     record.created_at)
  end

  @lighthouse_service.valid_document?(document:)
rescue => e
  StatsD.increment("#{STATSD_KEY_PREFIX}.document_upload_error")
  raise e
end

#send_confirmation_emailObject (private)



192
193
194
195
196
197
198
199
200
201
202
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 192

def send_confirmation_email
  @claim.respond_to?(:send_confirmation_email) && @claim.send_confirmation_email

  if %w[21P-530V2 21P-530].include?(@claim&.form_id)
    Burials::NotificationEmail.new(@claim.id).deliver(:confirmation)
  end
rescue => e
  Rails.logger.warn('Lighthouse::SubmitBenefitsIntakeClaim send_confirmation_email failed',
                    generate_log_details(e))
  StatsD.increment("#{STATSD_KEY_PREFIX}.send_confirmation_email.failure")
end

#split_file_and_path(path) ⇒ Object



124
125
126
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 124

def split_file_and_path(path)
  { file: path, file_name: path.split('/').last }
end

#stamped_pdf_with_form(form_id, path, timestamp) ⇒ Object (private)

This seems to be specific to burials This is stamping the (Do not write in this space portion of the PDF)



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/sidekiq/lighthouse/submit_benefits_intake_claim.rb', line 141

def stamped_pdf_with_form(form_id, path, timestamp)
  PDFUtilities::DatestampPdf.new(path).run(
    text: 'Application Submitted on va.gov',
    x: 425,
    y: 675,
    text_only: true, # passing as text only because we override how the date is stamped in this instance
    timestamp:,
    page_number: 5,
    size: 9,
    template: "lib/pdf_fill/forms/pdfs/#{form_id}.pdf",
    multistamp: true
  )
end