Class: ZeroSilentFailures::ManualRemediation::SavedClaim
- Inherits:
-
Object
- Object
- ZeroSilentFailures::ManualRemediation::SavedClaim
- Defined in:
- lib/zero_silent_failures/manual_remediation/saved_claim.rb
Overview
super class for manual remediation of a SavedClaim type
Instance Attribute Summary collapse
-
#aws_download_zip_link ⇒ Object
readonly
Returns the value of attribute aws_download_zip_link.
-
#claim ⇒ Object
readonly
Returns the value of attribute claim.
-
#zipfile ⇒ Object
readonly
Returns the value of attribute zipfile.
Instance Method Summary collapse
-
#aws_upload_zipfile ⇒ Object
private
upload zipfile to AWS.
-
#claim_class ⇒ Object
private
claim class to be used - inheritor should override to specific type.
-
#files ⇒ Object
private
array of files to be packaged.
-
#generate_attachment_pdf(pa) ⇒ Hash
private
create and stamp a claim attachment pdf.
-
#generate_form_pdf ⇒ Hash
private
create and stamp the claim form pdf.
-
#generate_metadata ⇒ Object
private
assemble metadata for claim - inheritor should append to super if needed.
-
#generate_metadata_json ⇒ Hash
private
create the claim metadata.json file.
-
#initialize(saved_claim_id) ⇒ SavedClaim
constructor
constructor.
-
#on_prod? ⇒ Boolean
private
is the current environment production.
-
#package_claim ⇒ Object
generate metadata, claim.pdf, attachment.pdf, and zip.
-
#run ⇒ Object
process the claim for remediation: - generate and stamp claim and attachment pdf - create a zipfile of generated pdf and metadata.json - upload zipfile to AWS S3 bucket (only if running on prod) - update database records for claim FormSubmissionAttempts - all generated files are deleted.
-
#stamp_pdf(pdf_path, timestamp) ⇒ String
private
stamp a generated pdf if there is an error stamping the pdf, the original path is returned - user uploaded attachments can be malformed.
-
#stamps(timestamp) ⇒ Array<Hash>
private
list of stamps to be applied to a generated pdf for claim - inheritor should append to super if needed.
-
#update_database ⇒ Object
update FormSubmissionAttempt records for claim.
-
#upload_documents ⇒ Object
upload documents to s3 bucket (only if running on prod).
-
#zip_files ⇒ Object
private
package all files for claim.
Constructor Details
#initialize(saved_claim_id) ⇒ SavedClaim
constructor
15 16 17 18 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 15 def initialize(saved_claim_id) @claim = claim_class.find(saved_claim_id) @zipfile = "#{Common::FileHelpers.random_file_path}.zip" end |
Instance Attribute Details
#aws_download_zip_link ⇒ Object (readonly)
Returns the value of attribute aws_download_zip_link.
10 11 12 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 10 def aws_download_zip_link @aws_download_zip_link end |
#claim ⇒ Object (readonly)
Returns the value of attribute claim.
10 11 12 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 10 def claim @claim end |
#zipfile ⇒ Object (readonly)
Returns the value of attribute zipfile.
10 11 12 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 10 def zipfile @zipfile end |
Instance Method Details
#aws_upload_zipfile ⇒ Object (private)
upload zipfile to AWS
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 198 def aws_upload_zipfile s3_resource = Aws::S3::Resource.new(region: Settings.vba_documents.s3.region, access_key_id: Settings.vba_documents.s3.aws_access_key_id, secret_access_key: Settings.vba_documents.s3.aws_secret_access_key) obj = s3_resource.bucket(Settings.vba_documents.s3.bucket).object(File.basename(zipfile)) obj.upload_file(zipfile, content_type: Mime[:zip].to_s) @aws_download_zip_link = obj.presigned_url(:get, expires_in: 1.day.to_i) Rails.logger.info("AWS Download Zip Link #{aws_download_zip_link}") rescue => e Rails.logger.error 'Error uploading to AWS' raise e end |
#claim_class ⇒ Object (private)
claim class to be used
-
inheritor should override to specific type
81 82 83 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 81 def claim_class ::SavedClaim end |
#files ⇒ Object (private)
array of files to be packaged
86 87 88 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 86 def files @files ||= [] end |
#generate_attachment_pdf(pa) ⇒ Hash (private)
create and stamp a claim attachment pdf
131 132 133 134 135 136 137 138 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 131 def (pa) filename = "#{claim.form_id}_#{claim.id}-attachment_#{pa.id}.pdf" filepath = pa.to_pdf Rails.logger.info "Stamping #{claim.form_id} #{claim.id} Attachment #{pa.id} - #{filepath}" stamped = stamp_pdf(filepath, claim.created_at) { name: filename, path: stamped } end |
#generate_form_pdf ⇒ Hash (private)
create and stamp the claim form pdf
121 122 123 124 125 126 127 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 121 def generate_form_pdf filepath = claim.to_pdf Rails.logger.info "Stamping #{claim.form_id} #{claim.id} - #{filepath}" stamped = stamp_pdf(filepath, claim.created_at) { name: File.basename(filepath), path: stamped } end |
#generate_metadata ⇒ Object (private)
assemble metadata for claim
-
inheritor should append to super if needed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 92 def form = claim.parsed_form address = form['claimantAddress'] || form['veteranAddress'] { claimId: claim.id, docType: claim.form_id, formStartDate: claim.form_start_date, claimSubmissionDate: claim.created_at, claimConfirmation: claim.guid, veteranFirstName: form['veteranFullName']['first'], veteranLastName: form['veteranFullName']['last'], fileNumber: form['vaFileNumber'] || form['veteranSocialSecurityNumber'], zipCode: address['postalCode'], businessLine: claim.business_line } end |
#generate_metadata_json ⇒ Hash (private)
create the claim metadata.json file
112 113 114 115 116 117 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 112 def = = Common::FileHelpers.generate_random_file(.to_json) { name: "#{claim.form_id}_#{claim.id}-metadata.json", path: } end |
#on_prod? ⇒ Boolean (private)
is the current environment production
75 76 77 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 75 def on_prod? Settings.vsp_environment == 'production' end |
#package_claim ⇒ Object
generate metadata, claim.pdf, attachment.pdf, and zip
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 43 def package_claim files << files << generate_form_pdf claim..each do |pa| files << (pa) end zip_files end |
#run ⇒ Object
process the claim for remediation:
-
generate and stamp claim and attachment pdf
-
create a zipfile of generated pdf and metadata.json
-
upload zipfile to AWS S3 bucket (only if running on prod)
-
update database records for claim FormSubmissionAttempts
-
all generated files are deleted
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 26 def run Rails.logger.info "Manual Remediation for #{claim.form_id} #{claim.id} started" # reset in case there are multiple runs with the same instance @files = [] package_claim upload_documents update_database Rails.logger.info "Manual Remediation for #{claim.form_id} #{claim.id} Complete!" ensure Common::FileHelpers.delete_file_if_exists(zipfile) if on_prod? files.each { |file| Common::FileHelpers.delete_file_if_exists(file[:path]) } end |
#stamp_pdf(pdf_path, timestamp) ⇒ String (private)
stamp a generated pdf if there is an error stamping the pdf, the original path is returned
-
user uploaded attachments can be malformed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 164 def stamp_pdf(pdf_path, ) stamped = pdf_path stamps().each do |stamp| previous = stamped stamped = PDFUtilities::DatestampPdf.new(previous).run(**stamp) Common::FileHelpers.delete_file_if_exists(previous) end stamped rescue Common::FileHelpers.delete_file_if_exists(stamped) if stamped != pdf_path Rails.logger.error "Error stamping pdf: #{pdf_path}" pdf_path end |
#stamps(timestamp) ⇒ Array<Hash> (private)
list of stamps to be applied to a generated pdf for claim
-
inheritor should append to super if needed
148 149 150 151 152 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 148 def stamps() [ { text: 'VA.GOV', x: 5, y: 5, timestamp: } ] end |
#update_database ⇒ Object
update FormSubmissionAttempt records for claim
65 66 67 68 69 70 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 65 def update_database Rails.logger.info "Updating database - #{claim.form_id} #{claim.id}" fs_ids = claim.form_submissions.map(&:id) FormSubmissionAttempt.where(form_submission_id: fs_ids, aasm_state: 'failure')&.map(&:manual!) end |
#upload_documents ⇒ Object
upload documents to s3 bucket (only if running on prod)
55 56 57 58 59 60 61 62 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 55 def upload_documents Rails.logger.info "Uploading documents - #{claim.form_id} #{claim.id}" aws_upload_zipfile if on_prod? # @todo ? upload to sharepoint directly ? aws_download_zip_link || zipfile end |
#zip_files ⇒ Object (private)
package all files for claim
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 181 def zip_files Zip::File.open(zipfile, Zip::File::CREATE) do |zip| files.each do |file| Rails.logger.info("Adding to zip: #{file}") zip.add(file[:name], file[:path]) rescue => e Rails.logger.error "Error adding to zip: #{file}" raise e end end Rails.logger.info("Packaged #{claim.form_id} #{claim.id} - #{zipfile}") end |