Class: Form1010Ezr::Service

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring, SentryLogging, VA1010Forms::Utils
Defined in:
lib/form1010_ezr/service.rb

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.1010ezr'
FORM_ID =
'10-10EZR'

Instance Method Summary collapse

Methods included from SentryLogging

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

Methods included from VA1010Forms::Utils

#es_submit, #log_payload_info, #override_parsed_form, #soap, #submission_body

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Constructor Details

#initialize(user) ⇒ Service

Returns a new instance of Service.

Parameters:

  • user (Object)


24
25
26
27
# File 'lib/form1010_ezr/service.rb', line 24

def initialize(user)
  super()
  @user = user
end

Instance Method Details

#add_financial_flag(parsed_form) ⇒ Object (private)



167
168
169
170
171
172
173
# File 'lib/form1010_ezr/service.rb', line 167

def add_financial_flag(parsed_form)
  if parsed_form['veteranGrossIncome'].present?
    parsed_form.merge('discloseFinancialInformation' => true)
  else
    parsed_form
  end
end

#configure_and_validate_form(parsed_form) ⇒ Object (private)



159
160
161
162
163
164
165
# File 'lib/form1010_ezr/service.rb', line 159

def configure_and_validate_form(parsed_form)
  post_fill_fields(parsed_form)
  validate_form(parsed_form)
  # Due to overriding the JSON form schema, we need to do so after the form has been validated
  override_parsed_form(parsed_form)
  add_financial_flag(parsed_form)
end

#log_and_raise_error(error, form) ⇒ Object (private)



184
185
186
187
188
# File 'lib/form1010_ezr/service.rb', line 184

def log_and_raise_error(error, form)
  log_submission_failure(form)
  Rails.logger.error "10-10EZR form submission failed: #{error.message}"
  raise error
end

#log_submission_failure(parsed_form) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/form1010_ezr/service.rb', line 71

def log_submission_failure(parsed_form)
  StatsD.increment("#{Form1010Ezr::Service::STATSD_KEY_PREFIX}.failed")

  if parsed_form.present?
    PersonalInformationLog.create!(
      data: parsed_form,
      error_class: 'Form1010Ezr Failed'
    )

    log_message_to_sentry(
      '1010EZR failure',
      :error,
      veteran_initials(parsed_form),
      ezr: :failure
    )
  end
end

#log_validation_errors(parsed_form) ⇒ Object (private)



175
176
177
178
179
180
181
182
# File 'lib/form1010_ezr/service.rb', line 175

def log_validation_errors(parsed_form)
  StatsD.increment("#{Form1010Ezr::Service::STATSD_KEY_PREFIX}.validation_error")

  PersonalInformationLog.create(
    data: parsed_form,
    error_class: 'Form1010Ezr ValidationError'
  )
end

#post_fill_fields(parsed_form) ⇒ Object (private)



152
153
154
155
156
157
# File 'lib/form1010_ezr/service.rb', line 152

def post_fill_fields(parsed_form)
  post_fill_required_fields(parsed_form)
  post_fill_required_user_fields(parsed_form)

  parsed_form.compact
end

#post_fill_required_fields(parsed_form) ⇒ Object (private)

<—- Post-fill methods —-> Add required fields not included in the JSON schema, but are required in the Enrollment System API



126
127
128
129
130
# File 'lib/form1010_ezr/service.rb', line 126

def post_fill_required_fields(parsed_form)
  required_fields = HCA::EzrPostfill.post_fill_hash(@user)

  parsed_form.merge!(required_fields)
end

#post_fill_required_user_fields(parsed_form) ⇒ Object (private)

Due to issues with receiving submissions that do not include the Veteran’s DOB, full name, SSN, and/or gender, we’ll try to add them in before we validate the form



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/form1010_ezr/service.rb', line 134

def post_fill_required_user_fields(parsed_form)
  # User fields that are required in the 10-10EZR schema, but not editable on the frontend
  required_user_form_fields = {
    'veteranDateOfBirth' => @user.birth_date,
    'veteranFullName' => @user.full_name_normalized&.compact&.stringify_keys,
    'veteranSocialSecurityNumber' => @user.ssn_normalized,
    'gender' => @user.gender
  }

  required_user_form_fields.each do |key, value|
    next if parsed_form[key].present?

    StatsD.increment("#{Form1010Ezr::Service::STATSD_KEY_PREFIX}.missing_#{key.underscore}")

    parsed_form[key] = value
  end
end

#submit_async(parsed_form) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/form1010_ezr/service.rb', line 29

def submit_async(parsed_form)
  HCA::EzrSubmissionJob.perform_async(
    HealthCareApplication::LOCKBOX.encrypt(parsed_form.to_json),
    @user.uuid
  )

  { success: true, formSubmissionId: nil, timestamp: nil }
end

#submit_form(parsed_form) ⇒ Object

Parameters:

  • parsed_form (HashWithIndifferentAccess)

    JSON form data



60
61
62
63
64
65
66
67
68
69
# File 'lib/form1010_ezr/service.rb', line 60

def submit_form(parsed_form)
  # Log the 'veteranDateOfBirth' to ensure the frontend validation is working as intended
  # REMOVE THE FOLLOWING TWO LINES OF CODE ONCE THE DOB ISSUE HAS BEEN DIAGNOSED - 3/27/24
  @unprocessed_user_dob = parsed_form['veteranDateOfBirth'].clone
  parsed_form = configure_and_validate_form(parsed_form)

  submit_async(parsed_form)
rescue => e
  log_and_raise_error(e, parsed_form)
end

#submit_sync(parsed_form) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/form1010_ezr/service.rb', line 38

def submit_sync(parsed_form)
  res = with_monitoring do
    es_submit(parsed_form, HealthCareApplication.get_user_identifier(@user), FORM_ID)
  end

  # Log the 'formSubmissionId' for successful submissions
  Rails.logger.info(
    '1010EZR successfully submitted',
    submission_id: res[:formSubmissionId],
    veteran_initials: veteran_initials(parsed_form)
  )

  if parsed_form['attachments'].present?
    StatsD.increment("#{Form1010Ezr::Service::STATSD_KEY_PREFIX}.submission_with_attachment")
  end

  res
rescue => e
  log_and_raise_error(e, parsed_form)
end

#validate_form(parsed_form) ⇒ Object (private)

Compare the ‘parsed_form’ to the JSON form schema in vets-json-schema



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/form1010_ezr/service.rb', line 100

def validate_form(parsed_form)
  schema = VetsJsonSchema::SCHEMAS[FORM_ID]
  # @return [Array<String>] array of strings detailing schema validation failures
  validation_errors = JSON::Validator.fully_validate(schema, parsed_form)

  if validation_errors.present?
    # REMOVE THE FOLLOWING SIX LINES OF CODE ONCE THE DOB ISSUE HAS BEEN DIAGNOSED - 3/27/24
    if validation_errors.find { |error| error.include?('veteranDateOfBirth') }.present?
      PersonalInformationLog.create!(
        data: @unprocessed_user_dob,
        error_class: "Form1010Ezr 'veteranDateOfBirth' schema failure"
      )
    end

    log_validation_errors(parsed_form)

    Rails.logger.error(
      "10-10EZR form validation failed. Form does not match schema. Error list: #{validation_errors}"
    )
    raise Common::Exceptions::SchemaValidationErrors, validation_errors
  end
end

#veteran_initials(parsed_form) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/form1010_ezr/service.rb', line 89

def veteran_initials(parsed_form)
  {
    first_initial: parsed_form.dig('veteranFullName', 'first')&.chr || 'no initial provided',
    middle_initial: parsed_form.dig('veteranFullName', 'middle')&.chr || 'no initial provided',
    last_initial: parsed_form.dig('veteranFullName', 'last')&.chr || 'no initial provided'
  }
end