Class: SavedClaim

Inherits:
ApplicationRecord show all
Includes:
SetGuid
Defined in:
app/models/saved_claim.rb

Overview

Base class to hold common functionality for Claim submissions. Subclasses need to several constants and methods defined:

  • ‘FORM` should align with the identifier of the form as found in

    the vets-json-schema struct so that validations can be run on submit
    
  • ‘CONFIRMATION` should be a small ident used as part of the confirmation

    number to quickly determine the form/product type
    
  • ‘regional_office()`, which returns an array or string of the location of the claim processing facility

  • ‘attachment_keys()` returns a list of symbols corresponding to the keys in the JSON submission of file upload references. These are iterated over in the `process_attachments!` method to associate the previously unmoored files to the submitted claim, and to begin processing them.

Defined Under Namespace

Classes: Ask, Burial, CaregiversAssistanceClaim, CoeClaim, DependencyClaim, DependencyVerificationClaim, DisabilityCompensation, EducationBenefits, EducationCareerCounselingClaim, HigherLevelReview, IncomeAndAssets, NoticeOfDisagreement, Pension, SupplementalClaim, VeteranReadinessEmploymentClaim

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

descendants_using_encryption, lockbox_options, #timestamp_attributes_for_update_in_model, #valid?

Class Method Details

.add_form_and_validation(form_id) ⇒ Object



41
42
43
44
# File 'app/models/saved_claim.rb', line 41

def self.add_form_and_validation(form_id)
  const_set('FORM', form_id)
  validates(:form_id, inclusion: [form_id])
end

Instance Method Details

#after_create_metricsObject (private)



171
172
173
174
175
176
177
178
# File 'app/models/saved_claim.rb', line 171

def after_create_metrics
  tags = ["form_id:#{form_id}"]
  StatsD.increment('saved_claim.create', tags:)
  if form_start_date
    claim_duration = created_at - form_start_date
    StatsD.measure('saved_claim.time-to-file', claim_duration, tags:)
  end
end

#after_destroy_metricsObject (private)



180
181
182
# File 'app/models/saved_claim.rb', line 180

def after_destroy_metrics
  StatsD.increment('saved_claim.destroy', tags: ["form_id:#{form_id}"])
end

#attachment_keysObject (private)



167
168
169
# File 'app/models/saved_claim.rb', line 167

def attachment_keys
  []
end

#business_lineObject



114
115
116
# File 'app/models/saved_claim.rb', line 114

def business_line
  ''
end

#confirmation_numberObject



56
57
58
# File 'app/models/saved_claim.rb', line 56

def confirmation_number
  guid
end

#emailObject



118
119
120
# File 'app/models/saved_claim.rb', line 118

def email
  nil
end

#form_is_stringObject



73
74
75
# File 'app/models/saved_claim.rb', line 73

def form_is_string
  form.is_a?(String)
end

#form_matches_schemaObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/saved_claim.rb', line 81

def form_matches_schema
  return unless form_is_string

  schema = VetsJsonSchema::SCHEMAS[self.class::FORM]
  clear_cache = false

  schema_errors = validate_schema(schema)
  unless schema_errors.empty?
    Rails.logger.error('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })
    clear_cache = true
  end

  validation_errors = validate_form(schema, clear_cache)
  validation_errors.each do |e|
    errors.add(e[:fragment], e[:message])
    e[:errors]&.flatten(2)&.each { |nested| errors.add(nested[:fragment], nested[:message]) if nested.is_a? Hash }
  end

  unless validation_errors.empty?
    Rails.logger.error('SavedClaim form did not pass validation', { guid:, errors: validation_errors })
  end
end

#form_must_be_stringObject



77
78
79
# File 'app/models/saved_claim.rb', line 77

def form_must_be_string
  errors.add(:form, :invalid_format, message: 'must be a json string') unless form_is_string
end

#insert_notification(email_template_id) ⇒ Object

insert notifcation after VANotify email send



127
128
129
130
131
132
133
# File 'app/models/saved_claim.rb', line 127

def insert_notification(email_template_id)
  claim_va_notifications.create!(
    form_type: form_id,
    email_sent: true,
    email_template_id: email_template_id
  )
end

#open_struct_formObject

Convert the json into an OStruct



61
62
63
# File 'app/models/saved_claim.rb', line 61

def open_struct_form
  @application ||= JSON.parse(form, object_class: OpenStruct)
end

#parsed_formObject



65
66
67
# File 'app/models/saved_claim.rb', line 65

def parsed_form
  @parsed_form ||= JSON.parse(form)
end

#process_attachments!Object

Run after a claim is saved, this processes any files and workflows that are present and sends them to our internal partners for processing.



48
49
50
51
52
53
54
# File 'app/models/saved_claim.rb', line 48

def process_attachments!
  refs = attachment_keys.map { |key| Array(open_struct_form.send(key)) }.flatten
  files = PersistentAttachment.where(guid: refs.map(&:confirmationCode))
  files.find_each { |f| f.update(saved_claim_id: id) }

  Lighthouse::SubmitBenefitsIntakeClaim.perform_async(id)
end

#submitted_atObject



69
70
71
# File 'app/models/saved_claim.rb', line 69

def 
  created_at
end

#to_pdf(file_name = nil) ⇒ Object



104
105
106
# File 'app/models/saved_claim.rb', line 104

def to_pdf(file_name = nil)
  PdfFill::Filler.fill_form(self, file_name)
end

#update_form(key, value) ⇒ Object



108
109
110
111
112
# File 'app/models/saved_claim.rb', line 108

def update_form(key, value)
  application = parsed_form
  application[key] = value
  self.form = JSON.generate(application)
end

#va_notification?(email_template_id) ⇒ Boolean

Find notifcation by args*

Parameters:

  • email_template_id

Returns:

  • (Boolean)

See Also:



141
142
143
144
145
146
# File 'app/models/saved_claim.rb', line 141

def va_notification?(email_template_id)
  claim_va_notifications.find_by(
    form_type: form_id,
    email_template_id: email_template_id
  )
end

#validate_form(schema, clear_cache) ⇒ Object (private)



157
158
159
160
161
162
163
164
165
# File 'app/models/saved_claim.rb', line 157

def validate_form(schema, clear_cache)
  JSON::Validator.fully_validate(schema, parsed_form, { errors_as_objects: true, clear_cache: })
rescue => e
  PersonalInformationLog.create(data: { schema:, parsed_form:, params: { errors_as_objects: true, clear_cache: } },
                                error_class: 'SavedClaim FormValidationError')
  Rails.logger.error('Error during form validation!',
                     { error: e.message, backtrace: e.backtrace, schema:, clear_cache: })
  raise
end

#validate_schema(schema) ⇒ Object (private)



150
151
152
153
154
155
# File 'app/models/saved_claim.rb', line 150

def validate_schema(schema)
  JSON::Validator.fully_validate_schema(schema, { errors_as_objects: true })
rescue => e
  Rails.logger.error('Error during schema validation!', { error: e.message, backtrace: e.backtrace, schema: })
  raise
end