Class: HCA::EnrollmentEligibility::Service

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/hca/enrollment_eligibility/service.rb

Constant Summary collapse

XPATH_PREFIX =
'env:Envelope/env:Body/getEESummaryResponse/summary/'
STATSD_KEY_PREFIX =
'api.hca_ee'
NAME_MAPPINGS =
[
  %i[first givenName],
  %i[middle middleName],
  %i[last familyName],
  %i[suffix suffix]
].freeze
INSURANCE_MAPPINGS =

left API key, right schema key

{
  'companyName' => 'insuranceName',
  'policyNumber' => 'insurancePolicyNumber',
  'policyHolderName' => 'insurancePolicyHolderName',
  'groupNumber' => 'insuranceGroupCode'
}.freeze
MARITAL_STATUSES =
%w[
  Married
  Never Married
  Separated
  Widowed
  Divorced
].freeze
MEDICARE =
'Medicare'

Instance Method Summary collapse

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

Methods included from SentryLogging

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

Instance Method Details

#build_lookup_user_xml(icn) ⇒ Object (private)

rubocop:disable Metrics/MethodLength



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/hca/enrollment_eligibility/service.rb', line 290

def build_lookup_user_xml(icn)
  Nokogiri::XML::Builder.new do |xml|
    xml.public_send(
      'SOAP-ENV:Envelope',
      'xmlns:SOAP-ENV' => 'http://schemas.xmlsoap.org/soap/envelope/',
      'xmlns:sch' => 'http://jaxws.webservices.esr.med.va.gov/schemas'
    ) do
      xml.public_send('SOAP-ENV:Header') do
        xml.public_send(
          'wsse:Security',
          'SOAP-ENV:mustUnderstand' => '1',
          'xmlns:wsse' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
        ) do
          xml.public_send(
            'wsse:UsernameToken',
            'wsu:Id' => 'XWSSGID-1281117217796-43574433',
            'xmlns:wsu' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'
          ) do
            xml.public_send('wsse:Username', Settings.hca.ee.user)
            xml.public_send(
              'wsse:Password',
              Settings.hca.ee.pass,
              'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'
            )
          end
        end
      end

      xml.public_send('SOAP-ENV:Body') do
        xml.public_send('sch:getEESummaryRequest') do
          xml.public_send('sch:key', icn)
          xml.public_send('sch:requestName', 'HCAData')
        end
      end
    end
  end.to_xml
end

#convert_insurance_hash(response, providers) ⇒ Object (private)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hca/enrollment_eligibility/service.rb', line 109

def convert_insurance_hash(response, providers)
  strip_medicare(providers).merge(
    {
      isEnrolledMedicarePartA: get_xpath(
        response,
        "#{XPATH_PREFIX}insuranceList/insurance/enrolledInPartA"
      ) == 'true',
      medicarePartAEffectiveDate: part_a_effective_date(response),
      isMedicaidEligible: ActiveModel::Type::Boolean.new.cast(
        get_xpath(
          response,
          "#{XPATH_PREFIX}enrollmentDeterminationInfo/eligibleForMedicaid"
        )
      )
    }
  )
end

#get_ezr_data(icn) ⇒ Object



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

def get_ezr_data(icn)
  response = with_monitoring do
    lookup_user_req(icn)
  end

  providers = parse_insurance_providers(response)
  dependents = parse_dependents(response)
  spouse = parse_spouse(response)

  OpenStruct.new(
    convert_insurance_hash(
      response, providers
    ).merge(
      dependents.present? ? { dependents: } : {}
    ).merge(spouse)
  )
end

#get_locate_value(node, key) ⇒ Object (private)



216
217
218
219
220
221
# File 'lib/hca/enrollment_eligibility/service.rb', line 216

def get_locate_value(node, key)
  res = node.locate(key)[0]
  return if res.nil?

  res.nodes[0]
end

#get_locate_value_bool(node, key) ⇒ Object (private)



212
213
214
# File 'lib/hca/enrollment_eligibility/service.rb', line 212

def get_locate_value_bool(node, key)
  ActiveModel::Type::Boolean.new.cast(get_locate_value(node, key))
end

#get_locate_value_date(node, key) ⇒ Object (private)



208
209
210
# File 'lib/hca/enrollment_eligibility/service.rb', line 208

def get_locate_value_date(node, key)
  parse_es_date(get_locate_value(node, key))
end

#get_marital_status(response) ⇒ Object (private)



127
128
129
130
131
132
133
134
135
136
# File 'lib/hca/enrollment_eligibility/service.rb', line 127

def get_marital_status(response)
  marital_status = get_locate_value(
    response,
    "#{XPATH_PREFIX}demographics/maritalStatus"
  )

  return unless MARITAL_STATUSES.include?(marital_status)

  marital_status
end

#get_xpath(response, xpath) ⇒ Object (private)



282
283
284
285
286
287
# File 'lib/hca/enrollment_eligibility/service.rb', line 282

def get_xpath(response, xpath)
  node = response.locate(xpath)
  return if node.blank?

  node[0].nodes[0]
end

#income_year_is_last_year?(response) ⇒ Boolean (private)

Returns:

  • (Boolean)


328
329
330
331
332
333
334
335
# File 'lib/hca/enrollment_eligibility/service.rb', line 328

def income_year_is_last_year?(response)
  income_year = get_xpath(
    response,
    "#{XPATH_PREFIX}financialsInfo/incomeTest/incomeYear"
  )

  income_year == (DateTime.now.utc.year - 1).to_s
end

#lookup_user(icn) ⇒ Object

rubocop:disable Metrics/MethodLength



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hca/enrollment_eligibility/service.rb', line 60

def lookup_user(icn)
  response = with_monitoring do
    lookup_user_req(icn)
  end

  {
    enrollment_status: get_xpath(
      response,
      "#{XPATH_PREFIX}enrollmentDeterminationInfo/enrollmentStatus"
    ),
    application_date: get_xpath(
      response,
      "#{XPATH_PREFIX}enrollmentDeterminationInfo/applicationDate"
    ),
    enrollment_date: get_xpath(
      response,
      "#{XPATH_PREFIX}enrollmentDeterminationInfo/enrollmentDate"
    ),
    preferred_facility: get_xpath(
      response,
      "#{XPATH_PREFIX}demographics/preferredFacility"
    ),
    ineligibility_reason: get_xpath(
      response,
      "#{XPATH_PREFIX}enrollmentDeterminationInfo/ineligibilityFactor/reason"
    ),
    effective_date: get_xpath(
      response,
      "#{XPATH_PREFIX}enrollmentDeterminationInfo/effectiveDate"
    ),
    primary_eligibility: get_xpath(
      response,
      "#{XPATH_PREFIX}enrollmentDeterminationInfo/primaryEligibility/type"
    ),
    veteran: get_xpath(
      response,
      "#{XPATH_PREFIX}enrollmentDeterminationInfo/veteran"
    ),
    priority_group: get_xpath(
      response,
      "#{XPATH_PREFIX}enrollmentDeterminationInfo/priorityGroup"
    ),
    can_submit_financial_info: !income_year_is_last_year?(response)
  }
end

#lookup_user_req(icn) ⇒ Object (private)



278
279
280
# File 'lib/hca/enrollment_eligibility/service.rb', line 278

def lookup_user_req(icn)
  perform(:post, '', build_lookup_user_xml(icn)).body
end

#parse_dependents(response) ⇒ Object (private)

rubocop:enable Metrics/MethodLength



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/hca/enrollment_eligibility/service.rb', line 181

def parse_dependents(response)
  dependents = []

  response.locate(
    "#{XPATH_PREFIX}financialsInfo/financialStatement/dependentFinancialsList"
  )[0]&.nodes&.each do |dep_node|
    dependent = {
      fullName: {},
      socialSecurityNumber: get_locate_value(dep_node, 'dependentInfo/ssns/ssn/ssnText'),
      becameDependent: get_locate_value_date(dep_node, 'dependentInfo/startDate'),
      dependentRelation: get_locate_value(dep_node, 'dependentInfo/relationship').downcase.upcase_first,
      disabledBefore18: get_locate_value_bool(dep_node, 'incapableOfSelfSupport'),
      attendedSchoolLastYear: get_locate_value_bool(dep_node, 'attendedSchool'),
      cohabitedLastYear: get_locate_value_bool(dep_node, 'livedWithPatient'),
      dateOfBirth: get_locate_value_date(dep_node, 'dependentInfo/dob')
    }

    NAME_MAPPINGS.each do |mapping|
      dependent[:fullName][mapping[0]] = get_locate_value(dep_node, "dependentInfo/#{mapping[1]}")
    end

    dependents << Common::HashHelpers.deep_compact(dependent)
  end

  dependents
end

#parse_es_date(date_str) ⇒ Object (private)



260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/hca/enrollment_eligibility/service.rb', line 260

def parse_es_date(date_str)
  return if date_str.blank?

  Date.parse(date_str).to_s
rescue Date::Error => e
  log_exception_to_sentry(e)
  PersonalInformationLog.create!(
    data: date_str,
    error_class: 'Form1010Ezr DateError'
  )

  nil
end

#parse_insurance_providers(response) ⇒ Object (private)



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/hca/enrollment_eligibility/service.rb', line 223

def parse_insurance_providers(response)
  providers = []

  response.locate("#{XPATH_PREFIX}insuranceList")[0]&.nodes&.each do |insurance_node|
    insurance = {}

    insurance_node.nodes.each do |insurance_inner|
      INSURANCE_MAPPINGS.each do |k, v|
        insurance[v] = insurance_inner.nodes[0] if insurance_inner.value == k
      end
    end

    providers << insurance
  end

  providers
end

#parse_spouse(response) ⇒ Object (private)

rubocop:disable Metrics/MethodLength



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/hca/enrollment_eligibility/service.rb', line 139

def parse_spouse(response)
  spouse_financials_xpath =
    "#{XPATH_PREFIX}financialsInfo/financialStatement/spouseFinancialsList/spouseFinancials/"

  Common::HashHelpers.deep_compact(
    {
      spouseFullName: lambda do
        return_val = {}

        NAME_MAPPINGS.each do |mapping|
          return_val[mapping[0]] = get_locate_value(
            response,
            "#{spouse_financials_xpath}spouse/#{mapping[1]}"
          )
        end

        return if return_val.compact.blank?

        return_val
      end.call,
      maritalStatus: get_marital_status(response),
      dateOfMarriage: get_locate_value_date(
        response,
        "#{spouse_financials_xpath}spouse/startDate"
      ),
      cohabitedLastYear: get_locate_value_bool(
        response,
        "#{spouse_financials_xpath}livedWithPatient"
      ),
      spouseDateOfBirth: get_locate_value_date(
        response,
        "#{spouse_financials_xpath}spouse/dob"
      ),
      spouseSocialSecurityNumber: get_locate_value(
        response,
        "#{spouse_financials_xpath}spouse/ssns/ssn/ssnText"
      )
    }
  )
end

#part_a_effective_date(response) ⇒ Object (private)



274
275
276
# File 'lib/hca/enrollment_eligibility/service.rb', line 274

def part_a_effective_date(response)
  get_locate_value_date(response, "#{XPATH_PREFIX}insuranceList/insurance/partAEffectiveDate")
end

#strip_medicare(providers) ⇒ Object (private)



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/hca/enrollment_eligibility/service.rb', line 241

def strip_medicare(providers)
  return_val = {
    providers: [],
    medicareClaimNumber: nil
  }

  providers.each do |provider|
    if provider['insuranceName'] == MEDICARE
      return_val[:medicareClaimNumber] = provider['insurancePolicyNumber']
    else
      return_val[:providers] << provider
    end
  end

  return_val.delete(:providers) if return_val[:providers].blank?

  return_val
end