Class: Lighthouse::LettersGenerator::Service

Inherits:
Common::Client::Base show all
Defined in:
lib/lighthouse/letters_generator/service.rb

Constant Summary collapse

LETTER_TYPES =
%w[
  benefit_summary
  benefit_summary_dependent
  benefit_verification
  certificate_of_eligibility
  civil_service
  commissary
  medicare_partd
  minimum_essential_coverage
  proof_of_service
  service_verification
].to_set.freeze
BENEFICIARY_KEY_TRANFORMS =
{
  awardEffectiveDateTime: :awardEffectiveDate,
  chapter35Eligibility: :hasChapter35Eligibility,
  nonServiceConnectedPension: :hasNonServiceConnectedPension,
  serviceConnectedDisabilities: :hasServiceConnectedDisabilities,
  adaptedHousing: :hasAdaptedHousing,
  individualUnemployabilityGranted: :hasIndividualUnemployabilityGranted,
  specialMonthlyCompensation: :hasSpecialMonthlyCompensation
}.freeze

Instance Method Summary collapse

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

#create_invalid_type_error(letter_type) ⇒ Object (private)



160
161
162
163
164
165
166
167
# File 'lib/lighthouse/letters_generator/service.rb', line 160

def create_invalid_type_error(letter_type)
  error = {}
  error['title'] = 'Invalid letter type'
  error['message'] = "Letter type of #{letter_type.downcase} is not one of the expected options"
  error['status'] = 400

  error
end

#download_letter(icns, letter_type, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/lighthouse/letters_generator/service.rb', line 81

def download_letter(icns, letter_type, options = {})
  endpoint = "letters/#{letter_type}/letter"
  log = "Downloading letter from #{config.generator_url}/#{endpoint}"
  params = icns.merge(options)

  response = get_from_lighthouse(endpoint, params, log)
  response.body
end

#get_benefit_information(icn) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lighthouse/letters_generator/service.rb', line 69

def get_benefit_information(icn)
  endpoint = 'eligible-letters'
  log = "Retrieving benefit information from #{config.generator_url}/#{endpoint}"
  params = { icn: }

  response = get_from_lighthouse(endpoint, params, log)
  {
    benefitInformation: transform_benefit_information(response.body['benefitInformation']),
    militaryService: transform_military_services(response.body['militaryServices'])
  }
end

#get_eligible_letter_types(icn) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lighthouse/letters_generator/service.rb', line 57

def get_eligible_letter_types(icn)
  endpoint = 'eligible-letters'
  log = "Retrieving eligible letter types and destination from #{config.generator_url}/#{endpoint}"
  params = { icn: }

  response = get_from_lighthouse(endpoint, params, log)
  {
    letters: transform_letters(response.body['letters']),
    letter_destination: response.body['letterDestination']
  }
end

#get_from_lighthouse(endpoint, params, log) ⇒ Object (private)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/lighthouse/letters_generator/service.rb', line 96

def get_from_lighthouse(endpoint, params, log)
  Lighthouse::LettersGenerator.measure_time(log) do
    config.connection.get(
      endpoint,
      params,
      { Authorization: "Bearer #{config.get_access_token}" }
    )
  end
rescue Faraday::ClientError, Faraday::ServerError => e
  Sentry.set_tags(
    team: 'benefits-claim-appeal-status',
    feature: 'letters-generator'
  )

  handle_error(e, config.service_name, endpoint)
end

#get_letter(icn, letter_type, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/lighthouse/letters_generator/service.rb', line 48

def get_letter(icn, letter_type, options = {})
  endpoint = "letter-contents/#{letter_type}"
  log = "Retrieving letter from #{config.generator_url}/#{endpoint}"
  params = { icn: }.merge(options)

  response = get_from_lighthouse(endpoint, params, log)
  response.body
end

#handle_error(error, lighthouse_client_id, endpoint) ⇒ Object (private)



113
114
115
116
117
118
119
120
# File 'lib/lighthouse/letters_generator/service.rb', line 113

def handle_error(error, lighthouse_client_id, endpoint)
  Lighthouse::ServiceException.send_error(
    error,
    self.class.to_s.underscore,
    lighthouse_client_id,
    "#{config.generator_url}/#{endpoint}"
  )
end

#transform_benefit_information(info) ⇒ Object (private)



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/lighthouse/letters_generator/service.rb', line 140

def transform_benefit_information(info)
  symbolized_info = info.deep_transform_keys(&:to_sym)

  transformed_info = symbolized_info.reduce({}) do |acc, (k, v)|
    if BENEFICIARY_KEY_TRANFORMS.key? k
      acc.merge({ BENEFICIARY_KEY_TRANFORMS[k] => v })
    else
      acc.merge({ k => v })
    end
  end

  monthly_award_amount = symbolized_info[:monthlyAwardAmount] ? symbolized_info[:monthlyAwardAmount][:value] : 0

  # Don't return chapter35EligibilityDateTime
  # It's not currently (June 2023) used on the frontend, and in fact causes problems
  transformed_info
    .merge({ monthlyAwardAmount: monthly_award_amount })
    .except(:chapter35EligibilityDateTime)
end

#transform_letters(letters) ⇒ Object (private)



122
123
124
125
126
127
128
129
# File 'lib/lighthouse/letters_generator/service.rb', line 122

def transform_letters(letters)
  letters.map do |letter|
    {
      letterType: letter['letterType'].downcase,
      name: letter['letterName']
    }
  end
end

#transform_military_services(services_info) ⇒ Object (private)



131
132
133
134
135
136
137
138
# File 'lib/lighthouse/letters_generator/service.rb', line 131

def transform_military_services(services_info)
  services_info.map do |service|
    service[:enteredDate] = service.delete 'enteredDateTime'
    service[:releasedDate] = service.delete 'releasedDateTime'

    service.transform_keys(&:to_sym)
  end
end

#valid_type?(letter_type) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/lighthouse/letters_generator/service.rb', line 90

def valid_type?(letter_type)
  LETTER_TYPES.include? letter_type.downcase
end