Class: V0::LettersGeneratorController

Inherits:
ApplicationController show all
Defined in:
app/controllers/v0/letters_generator_controller.rb

Constant Summary collapse

DOWNLOAD_PARAMS =
%i[
  id
  format
  military_service
  service_connected_disabilities
  service_connected_evaluation
  non_service_connected_pension
  monthly_award
  unemployable
  special_monthly_compensation
  adapted_housing
  chapter35_eligibility
  death_result_of_disability
  survivors_award
  letters_generator
].freeze

Constants inherited from ApplicationController

ApplicationController::VERSION_STATUS

Constants included from SignIn::Authentication

SignIn::Authentication::BEARER_PATTERN

Constants included from ExceptionHandling

ExceptionHandling::SKIP_SENTRY_EXCEPTION_TYPES

Instance Method Summary collapse

Methods inherited from ApplicationController

#clear_saved_form, #cors_preflight, #routing_error

Methods included from Traceable

#set_trace_tags

Methods included from SentryLogging

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

Methods included from SignIn::Authentication

#authenticate, #authenticate_service_account, #load_user

Methods included from Headers

#block_unknown_hosts, #set_app_info_headers

Methods included from AuthenticationAndSSOConcerns

#authenticate, #clear_session, #extend_session!, #load_user, #log_sso_info, #render_unauthorized, #reset_session, #set_api_cookie!, #set_session_expiration_header, #sso_logging_info, #validate_inbound_login_params, #validate_session

Instance Method Details

#beneficiaryObject



72
73
74
75
# File 'app/controllers/v0/letters_generator_controller.rb', line 72

def beneficiary
  response = service.get_benefit_information(@current_user.icn)
  render json: response
end

#downloadObject

rubocop:disable Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/v0/letters_generator_controller.rb', line 35

def download # rubocop:disable Metrics/MethodLength
  permitted_params = params.permit(DOWNLOAD_PARAMS)
  letter_options =
    permitted_params.to_h
                    .except('id')
                    .transform_values { |v| ActiveModel::Type::Boolean.new.cast(v) }
                    .transform_keys { |k| k.camelize(:lower) }

  icns = { icn: @current_user.icn }

  if dependent_letter?(permitted_params['id'])
    # Gets the sponsor's icn if one exists
    sponsor_icn = Lighthouse::LettersGenerator::VeteranSponsorResolver.get_sponsor_icn(@current_user)
    is_dependent = Lighthouse::LettersGenerator::VeteranSponsorResolver.dependent?(@current_user)

    # This is an exceptional case.
    # A Veteran who is not a dependent should not be able to access this endpoint.
    # A dependent (Veteran or not) must have a Veteran sponsor.
    if sponsor_icn.nil?
      raise Common::Exceptions::BadRequest.new(
        {
          detail: "User #{@current_user.uuid} is #{is_dependent ? 'not' : ''} a Veteran and has no associated sponsor.", # rubocop:disable Layout/LineLength
          source: self.class.name
        }
      )
    else
      icns[:sponsor_icn] = sponsor_icn
    end
  end

  response = service.download_letter(icns, params[:id], letter_options)
  send_data response,
            filename: "#{params[:id]}.pdf",
            type: 'application/pdf',
            disposition: 'attachment'
end

#indexObject



30
31
32
33
# File 'app/controllers/v0/letters_generator_controller.rb', line 30

def index
  response = service.get_eligible_letter_types(@current_user.icn)
  render json: response
end