Class: V0::CoeController

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

Constant Summary

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 Attribute Summary

Attributes inherited from ApplicationController

#current_user

Instance Method Summary collapse

Methods inherited from ApplicationController

#clear_saved_form, #cors_preflight, #pagination_params, #render_job_id, #routing_error, #set_csrf_header

Methods included from Traceable

#set_trace_tags

Methods included from SentryControllerLogging

#set_tags_and_extra_context, #tags_context, #user_context

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 Instrumentation

#append_info_to_payload

Methods included from SignIn::Authentication

#access_token, #access_token_authenticate, #authenticate, #authenticate_access_token, #bearer_token, #cookie_access_token, #handle_authenticate_error, #load_user, #load_user_object, #scrub_bearer_token, #validate_request_ip

Methods included from Headers

#set_app_info_headers

Methods included from ExceptionHandling

#render_errors, #report_mapped_exception, #report_original_exception, #skip_sentry_exception?, #skip_sentry_exception_types

Methods included from AuthenticationAndSSOConcerns

#authenticate, #clear_session, #extend_session!, #load_user, #log_sso_info, #render_unauthorized, #reset_session, #set_api_cookie!, #set_current_user, #set_session_expiration_header, #set_session_object, #sign_in_service_exp_time, #sign_in_service_session, #sso_cookie_content, #sso_logging_info, #validate_inbound_login_params, #validate_session

Methods included from SignIn::AudienceValidator

#authenticate, #validate_audience!

Instance Method Details

#attachmentsObject (private)



97
98
99
# File 'app/controllers/v0/coe_controller.rb', line 97

def attachments
  params[:files]
end

#build_document_data(attachment) ⇒ Object (private)



105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/v0/coe_controller.rb', line 105

def build_document_data(attachment)
  file_data = attachment['file']
  index = file_data.index(';base64,') || 0
  file_data = file_data[index + 8..] if index.positive?

  {
    'documentType' => attachment['file_type'],
    'description' => attachment['document_type'],
    'contentsBase64' => file_data,
    'fileName' => attachment['file_name']
  }
end

#document_downloadObject



82
83
84
85
# File 'app/controllers/v0/coe_controller.rb', line 82

def document_download
  res = lgy_service.get_document(params[:id])
  send_data(res.body, type: 'application/pdf', disposition: 'attachment')
end

#document_uploadObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/v0/coe_controller.rb', line 48

def document_upload
  status = 201

  # Each document is uploaded individually
  attachments.each do |attachment|
    file_extension = attachment['file_type']

    if %w[jpg jpeg png pdf].include? file_extension.downcase
      document_data = build_document_data(attachment)

      status = post_document(document_data)
      break unless status == 201
    end
  end
  render(json: status, status: status == 201 ? 200 : 500)
end

#documentsObject



36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/v0/coe_controller.rb', line 36

def documents
  documents = lgy_service.get_coe_documents.body
  # Vet-uploaded docs have documentType `Veteran Correspondence`. We are not
  # currently displaying these on the COE status page, so they are omitted.
  # In the near future, we will display them, and can remove this `reject`
  # block.
  notification_letters = documents.reject { |doc| doc['document_type']&.include?('Veteran Correspondence') }
  # Documents should be sorted from most to least recent
  sorted_notification_letters = notification_letters.sort_by { |doc| doc['create_date'] }.reverse
  render json: { data: { attributes: sorted_notification_letters } }, status: :ok
end

#download_coeObject



14
15
16
17
18
# File 'app/controllers/v0/coe_controller.rb', line 14

def download_coe
  res = lgy_service.get_coe_file

  send_data(res.body, type: 'application/pdf', disposition: 'attachment')
end

#filtered_paramsObject (private)



93
94
95
# File 'app/controllers/v0/coe_controller.rb', line 93

def filtered_params
  params.require(:lgy_coe_claim).permit(:form)
end

#lgy_serviceObject (private)



89
90
91
# File 'app/controllers/v0/coe_controller.rb', line 89

def lgy_service
  @lgy_service ||= LGY::Service.new(edipi: @current_user.edipi, icn: @current_user.icn)
end

#post_document(document_data) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/v0/coe_controller.rb', line 65

def post_document(document_data)
  response = lgy_service.post_document(payload: document_data)
  response.status
rescue Common::Client::Errors::ClientError => e
  # 502-503 errors happen frequently from LGY endpoint at the time of implementation
  # and have not been corrected yet. We would like to seperate these from our monitoring for now
  # See https://github.com/department-of-veterans-affairs/va.gov-team/issues/90411
  # and https://github.com/department-of-veterans-affairs/va.gov-team/issues/91111
  if [503, 504].include?(e.status)
    Rails.logger.info('LGY server unavailable or unresponsive',
                      { status: e.status, messsage: e.message, body: e.body })
  else
    Rails.logger.error('LGY API returned error', { status: e.status, messsage: e.message, body: e.body })
  end
  e.status
end

#stats_keyObject (private)



101
102
103
# File 'app/controllers/v0/coe_controller.rb', line 101

def stats_key
  'api.lgy_coe'
end

#statusObject



9
10
11
12
# File 'app/controllers/v0/coe_controller.rb', line 9

def status
  coe_status = lgy_service.coe_status
  render json: { data: { attributes: coe_status } }, status: :ok
end

#submit_coe_claimObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/v0/coe_controller.rb', line 20

def submit_coe_claim
  claim = SavedClaim::CoeClaim.new(form: filtered_params[:form])

  unless claim.save
    StatsD.increment("#{stats_key}.failure")
    Sentry.set_tags(team: 'vfs-ebenefits') # tag sentry logs with team name
    raise Common::Exceptions::ValidationErrors, claim
  end

  response = claim.send_to_lgy(edipi: current_user.edipi, icn: current_user.icn)

  Rails.logger.info "ClaimID=#{claim.confirmation_number} Form=#{claim.class::FORM}"
  clear_saved_form(claim.form_id)
  render json: { data: { attributes: { reference_number: response, claim: } } }
end