Class: EducationBenefitsClaim

Inherits:
ApplicationRecord show all
Defined in:
app/models/education_benefits_claim.rb

Constant Summary collapse

FORM_TYPES =
%w[1990 1995 1990e 5490 5495 1990n 0993 0994 10203 1990s].freeze
APPLICATION_TYPES =
%w[
  chapter33
  chapter1607
  chapter1606
  chapter32
  chapter35
  transfer_of_entitlement
  vettec
  chapter30
  vrrap
].freeze

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

.form_headers(form_types = FORM_TYPES) ⇒ Object



133
134
135
# File 'app/models/education_benefits_claim.rb', line 133

def self.form_headers(form_types = FORM_TYPES)
  form_types.map { |t| "22-#{t}" }.freeze
end

.unprocessedObject



97
98
99
# File 'app/models/education_benefits_claim.rb', line 97

def self.unprocessed
  where(processed_at: nil)
end

Instance Method Details

#confirmation_numberObject



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

def confirmation_number
  "V-EBC-#{id}"
end

#copy_from_previous_benefitsObject



75
76
77
78
79
80
81
82
83
# File 'app/models/education_benefits_claim.rb', line 75

def copy_from_previous_benefits
  if @application.currentSameAsPrevious
    previous_benefits = @application.previousBenefits

    %w[veteranFullName vaFileNumber veteranSocialSecurityNumber].each do |attr|
      @application.public_send("#{attr}=", previous_benefits.public_send(attr))
    end
  end
end

#create_education_benefits_submissionObject (private)



139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/education_benefits_claim.rb', line 139

def create_education_benefits_submission
  opt = selected_benefits

  EducationBenefitsSubmission.create!(
    opt.merge(
      region:,
      form_type:,
      education_benefits_claim: self
    )
  )
end

#form_typeObject



52
53
54
# File 'app/models/education_benefits_claim.rb', line 52

def form_type
  saved_claim.form_id.gsub('22-', '').downcase
end

#generate_benefits_to_apply_toObject



85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/education_benefits_claim.rb', line 85

def generate_benefits_to_apply_to
  selected_benefits = []
  APPLICATION_TYPES.each do |application_type|
    selected_benefits << application_type if @application.public_send(application_type)
  end
  selected_benefits = selected_benefits.join(', ')

  @application.toursOfDuty&.each do |tour|
    tour.benefitsToApplyTo = selected_benefits if tour.applyPeriodToSelected
  end
end

#open_struct_formObject

This converts the form data into an OpenStruct object so that the template rendering can be cleaner. Piping it through the JSON serializer was a quick and easy way to deeply transform the object.



59
60
61
62
63
64
65
66
67
68
# File 'app/models/education_benefits_claim.rb', line 59

def open_struct_form
  @application ||= lambda do
    @application = saved_claim.open_struct_form
    @application.confirmation_number = confirmation_number

    transform_form

    @application
  end.call
end

#regionObject



101
102
103
# File 'app/models/education_benefits_claim.rb', line 101

def region
  EducationForm::EducationFacility.region_for(self)
end

#regional_officeObject



105
106
107
# File 'app/models/education_benefits_claim.rb', line 105

def regional_office
  EducationForm::EducationFacility.regional_office_for(self)
end

#reprocess_at(region) ⇒ Object

For console access only, right now.



31
32
33
34
35
36
37
38
39
40
# File 'app/models/education_benefits_claim.rb', line 31

def reprocess_at(region)
  key = region.to_sym
  unless EducationForm::EducationFacility::REGIONS.include?(key)
    raise "Invalid region. Must be one of #{EducationForm::EducationFacility::REGIONS.join(', ')}"
  end

  self.regional_processing_office = region
  self.processed_at = nil
  save
end

#selected_benefitsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/education_benefits_claim.rb', line 109

def selected_benefits
  benefits = {}

  case form_type
  when '1990'
    benefits = parsed_form.slice(*APPLICATION_TYPES)
  when '1990n'
    return benefits
  when '0994'
    benefits['vettec'] = true
  when '1990s'
    benefits['vrrap'] = true
  when '1995'
    benefit = parsed_form['benefit']&.underscore
    benefits['chapter33'] = true if benefit.present? && benefit.start_with?('chapter33')
    benefits[benefit] = true if benefit.present? && !benefit.start_with?('chapter33')
  else
    benefit = parsed_form['benefit']&.underscore
    benefits[benefit] = true if benefit.present?
  end

  benefits
end

#set_regionObject (private)



158
159
160
# File 'app/models/education_benefits_claim.rb', line 158

def set_region
  self.regional_processing_office ||= region.to_s
end

#transform_formObject



70
71
72
73
# File 'app/models/education_benefits_claim.rb', line 70

def transform_form
  generate_benefits_to_apply_to if is_1990?
  copy_from_previous_benefits if is_5490?
end

#update_education_benefits_submission_statusObject (private)



151
152
153
154
155
156
# File 'app/models/education_benefits_claim.rb', line 151

def update_education_benefits_submission_status
  if processed_at.present? && attribute_before_last_save(:processed).nil?
    # old claims don't have an education benefits submission associated
    education_benefits_submission&.update!(status: 'processed')
  end
end