Class: CentralMail::SubmitCentralForm686cJob

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

Defined Under Namespace

Classes: CentralMailResponseError

Constant Summary collapse

FOREIGN_POSTALCODE =
'00000'
FORM_ID =
'686C-674'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SentryLogging

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

Instance Attribute Details

#attachment_pathsObject (readonly)

Returns the value of attribute attachment_paths.



18
19
20
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 18

def attachment_paths
  @attachment_paths
end

#claimObject (readonly)

Returns the value of attribute claim.



18
19
20
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 18

def claim
  @claim
end

#form_pathObject (readonly)

Returns the value of attribute form_path.



18
19
20
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 18

def form_path
  @form_path
end

Instance Method Details

#check_success(response, saved_claim_id, user_struct) ⇒ Object

rubocop:disable Metrics/MethodLength #Temporary disable until flipper removed



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 85

def check_success(response, saved_claim_id, user_struct)
  if Flipper.enabled?(:dependents_central_submission_lighthouse)
    if response.success?
      Rails.logger.info('CentralMail::SubmitCentralForm686cJob succeeded!',
                        { user_uuid: user_struct['uuid'], saved_claim_id:, icn: user_struct['icn'] })
      update_submission('success')
      send_confirmation_email(OpenStruct.new(user_struct))
    else
      Rails.logger.info('CentralMail::SubmitCentralForm686cJob Unsuccessful',
                        { response: response['message'].presence || response['errors'] })
      raise CentralMailResponseError
    end
  elsif response.success?
    Rails.logger.info('CentralMail::SubmitCentralForm686cJob succeeded!',
                      { user_uuid: user_struct['uuid'], saved_claim_id:, icn: user_struct['icn'],
                        centralmail_uuid: extract_uuid_from_central_mail_message(response) })
    update_submission('success')
    send_confirmation_email(OpenStruct.new(user_struct))
  else
    Rails.logger.info('CentralMail::SubmitCentralForm686cJob Unsuccessful',
                      { response: response.body })
    raise CentralMailResponseError
  end
end

#cleanup_file_pathsObject



79
80
81
82
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 79

def cleanup_file_paths
  Common::FileHelpers.delete_file_if_exists(form_path)
  attachment_paths.each { |p| Common::FileHelpers.delete_file_if_exists(p) }
end

#create_request_bodyObject

rubocop:enable Metrics/MethodLength



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 112

def create_request_body
  body = {
    'metadata' => .to_json
  }

  body['document'] = to_faraday_upload(form_path)

  i = 0
  attachment_paths.each do |file_path|
    body["attachment#{i += 1}"] = to_faraday_upload(file_path)
  end

  body
end

#extract_uuid_from_central_mail_message(data) ⇒ Object



22
23
24
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 22

def extract_uuid_from_central_mail_message(data)
  data.body[/(?<=\[).*?(?=\])/].split(': ').last if data.body.present?
end

#generate_attachment_metadata(attachment_paths) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 189

def (attachment_paths)
   = {}
  i = 0
  attachment_paths.each do |file_path|
    i += 1
     = get_hash_and_pages(file_path)
    ["ahash#{i}"] = [:hash]
    ["numberPages#{i}"] = [:pages]
  end
  
end

#generate_metadataObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 155

def 
  form = claim.parsed_form['dependents_application']
   = get_hash_and_pages(form_path)
  address = form['veteran_contact_information']['veteran_address']
  receive_date = claim.created_at.in_time_zone('Central Time (US & Canada)')
   = {
    'veteranFirstName' => form['veteran_information']['full_name']['first'],
    'veteranLastName' => form['veteran_information']['full_name']['last'],
    'fileNumber' => form['veteran_information']['file_number'] || form['veteran_information']['ssn'],
    'receiveDt' => receive_date.strftime('%Y-%m-%d %H:%M:%S'),
    'uuid' => claim.guid,
    'zipCode' => address['country_name'] == 'USA' ? address['zip_code'] : FOREIGN_POSTALCODE,
    'source' => 'va.gov',
    'hashV' => [:hash],
    'numberAttachments' => attachment_paths.size,
    'docType' => claim.form_id,
    'numberPages' => [:pages]
  }
  .merge((attachment_paths))
end

#generate_metadata_lhObject



176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 176

def 
  form = claim.parsed_form['dependents_application']
  address = form['veteran_contact_information']['veteran_address']
  {
    veteran_first_name: form['veteran_information']['full_name']['first'],
    veteran_last_name: form['veteran_information']['full_name']['last'],
    file_number: form['veteran_information']['file_number'] || form['veteran_information']['ssn'],
    zip: address['country_name'] == 'USA' ? address['zip_code'] : FOREIGN_POSTALCODE,
    doc_type: claim.form_id,
    claim_date: claim.created_at
  }
end

#get_files_from_claimObject



69
70
71
72
73
74
75
76
77
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 69

def get_files_from_claim
  # process the main pdf record and the attachments as we would for a vbms submission
  form_674_path = process_pdf(claim.to_pdf(form_id: '21-674')) if claim.submittable_674?
  form_686c_path = process_pdf(claim.to_pdf(form_id: '686C-674')) if claim.submittable_686?
  @form_path = form_686c_path || form_674_path
  @attachment_paths = claim.persistent_attachments.map { |pa| process_pdf(pa.to_pdf) }
  # Treat 674 as first attachment
  attachment_paths.insert(0, form_674_path) if form_686c_path.present? && form_674_path.present?
end

#get_hash_and_pages(file_path) ⇒ Object



148
149
150
151
152
153
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 148

def get_hash_and_pages(file_path)
  {
    hash: Digest::SHA256.file(file_path).hexdigest,
    pages: PdfInfo::Metadata.read(file_path).pages
  }
end

#perform(saved_claim_id, encrypted_vet_info, encrypted_user_struct) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 26

def perform(saved_claim_id, encrypted_vet_info, encrypted_user_struct)
  vet_info = JSON.parse(KmsEncrypted::Box.new.decrypt(encrypted_vet_info))
  user_struct = JSON.parse(KmsEncrypted::Box.new.decrypt(encrypted_user_struct))
  # if the 686c-674 has failed we want to call this central mail job (credit to submit_saved_claim_job.rb)
  # have to re-find the claim and add the relevant veteran info
  Rails.logger.info('CentralMail::SubmitCentralForm686cJob running!',
                    { user_uuid: user_struct['uuid'], saved_claim_id:, icn: user_struct['icn'] })
  @claim = SavedClaim::DependencyClaim.find(saved_claim_id)
  claim.add_veteran_info(vet_info)

  get_files_from_claim

  use_lighthouse = Flipper.enabled?(:dependents_central_submission_lighthouse)
  result = use_lighthouse ? upload_to_lh : CentralMail::Service.new.upload(create_request_body)
  check_success(result, saved_claim_id, user_struct)
rescue => e
  # if we fail, update the associated central mail record to failed and send the user the failure email
  Rails.logger.warn('CentralMail::SubmitCentralForm686cJob failed!',
                    { user_uuid: user_struct['uuid'], saved_claim_id:, icn: user_struct['icn'], error: e.message })
  update_submission('failed')
  DependentsApplicationFailureMailer.build(OpenStruct.new(user_struct)).deliver_now if user_struct['email'].present?
  raise
ensure
  cleanup_file_paths
end

#process_pdf(pdf_path) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 138

def process_pdf(pdf_path)
  stamped_path1 = CentralMail::DatestampPdf.new(pdf_path).run(text: 'VA.GOV', x: 5, y: 5)
  CentralMail::DatestampPdf.new(stamped_path1).run(
    text: 'FDC Reviewed - va.gov Submission',
    x: 429,
    y: 770,
    text_only: true
  )
end

#send_confirmation_email(user) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 201

def send_confirmation_email(user)
  return if user.va_profile_email.blank?

  VANotify::ConfirmationEmail.send(
    email_address: user.va_profile_email,
    template_id: Settings.vanotify.services.va_gov.template_id.form686c_confirmation_email,
    first_name: user&.first_name&.upcase,
    user_uuid_and_form_id: "#{user.uuid}_#{FORM_ID}"
  )
end

#to_faraday_upload(file_path) ⇒ Object



131
132
133
134
135
136
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 131

def to_faraday_upload(file_path)
  Faraday::UploadIO.new(
    file_path,
    Mime[:pdf].to_s
  )
end

#update_submission(state) ⇒ Object



127
128
129
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 127

def update_submission(state)
  claim.central_mail_submission.update!(state:) if claim.respond_to?(:central_mail_submission)
end

#upload_to_lhObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/sidekiq/central_mail/submit_central_form686c_job.rb', line 52

def upload_to_lh
  Rails.logger.info({ message: 'SubmitCentralForm686cJob Lighthouse Initiate Submission Attempt',
                      claim_id: claim.id })
  lighthouse_service = BenefitsIntakeService::Service.new(with_upload_location: true)
  uuid = lighthouse_service.uuid
  Rails.logger.info({ message: 'SubmitCentralForm686cJob Lighthouse Submission Attempt', claim_id: claim.id,
                      uuid: })
  response = lighthouse_service.upload_form(
    main_document: split_file_and_path(form_path),
    attachments: attachment_paths.map(&method(:split_file_and_path)),
    form_metadata: 
  )
  Rails.logger.info({ message: 'SubmitCentralForm686cJob Lighthouse Submission Successful', claim_id: claim.id,
                      uuid: })
  response
end