Class: ZeroSilentFailures::ManualRemediation::SavedClaim

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(saved_claim_id) ⇒ SavedClaim

constructor

Parameters:

  • saved_claim_id (Integer)

    the id of the claim needing manual remediation



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

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

#claimObject (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

#zipfileObject (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_zipfileObject (private)

upload zipfile to AWS

Raises:

  • Error if unable to upload

See Also:

  • Aws::S3::Resource


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_classObject (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

#filesObject (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

Returns:

  • (Hash)

    the name and path to the file



131
132
133
134
135
136
137
138
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 131

def generate_attachment_pdf(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_pdfHash (private)

create and stamp the claim form pdf

Returns:

  • (Hash)

    the name and path to the file



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_metadataObject (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_jsonHash (private)

create the claim metadata.json file

Returns:

  • (Hash)

    the name and path to the file



112
113
114
115
116
117
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 112

def 
   = 

  metafile = Common::FileHelpers.generate_random_file(.to_json)
  { name: "#{claim.form_id}_#{claim.id}-metadata.json", path: metafile }
end

#on_prod?Boolean (private)

is the current environment production

Returns:

  • (Boolean)


75
76
77
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 75

def on_prod?
  Settings.vsp_environment == 'production'
end

#package_claimObject

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.persistent_attachments.each do |pa|
    files << generate_attachment_pdf(pa)
  end

  zip_files
end

#runObject

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

Parameters:

  • pdf_path (String)

    the path to a generated pdf; ie. claim.to_pdf

  • timestamp (String|Datetime)

    the timestamp to be used; should be the claim.created_at

Returns:

  • (String)

    the path to the stamped pdf

See Also:

  • PDFUtilites::DatestampPdf#run


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, timestamp)
  stamped = pdf_path
  stamps(timestamp).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

Parameters:

  • timestamp (String|Datetime)

    the timestamp to be used; should be the claim.created_at

Returns:

  • (Array<Hash>)

    the list of stamps to be applied

See Also:

  • PDFUtilites::DatestampPdf#run


148
149
150
151
152
# File 'lib/zero_silent_failures/manual_remediation/saved_claim.rb', line 148

def stamps(timestamp)
  [
    { text: 'VA.GOV', x: 5, y: 5, timestamp: }
  ]
end

#update_databaseObject

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_documentsObject

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_filesObject (private)

package all files for claim

Raises:

  • Error if unable to add a file to the zip



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