Class: LGY::Service

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

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.lgy'
SENTRY_TAG =
{ team: 'vfs-ebenefits' }.freeze

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 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(edipi: nil, icn: nil) ⇒ Service

Returns a new instance of Service.



14
15
16
17
# File 'lib/lgy/service.rb', line 14

def initialize(edipi: nil, icn: nil)
  @edipi = edipi
  @icn = icn
end

Instance Method Details

#coe_statusObject

rubocop:disable Metrics/MethodLength



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lgy/service.rb', line 20

def coe_status
  if get_determination.body['status'] == 'ELIGIBLE' && get_application.status == 404
    { status: 'ELIGIBLE', reference_number: get_determination.body['reference_number'] }
  elsif get_determination.body['status'] == 'UNABLE_TO_DETERMINE_AUTOMATICALLY'
    { status: 'UNABLE_TO_DETERMINE_AUTOMATICALLY', reference_number: get_determination.body['reference_number'] }
  elsif get_determination.body['status'] == 'ELIGIBLE' && get_application.status == 200
    { status: 'AVAILABLE', application_create_date: get_application.body['create_date'],
      reference_number: get_determination.body['reference_number'] }
  elsif get_determination.body['status'] == 'NOT_ELIGIBLE'
    { status: 'DENIED', application_create_date: get_determination.body['determination_date'],
      reference_number: get_determination.body['reference_number'] }
  elsif get_determination.body['status'] == 'PENDING' && get_application.status == 404
    # Kelli said we'll never having a pending status w/o an application, but LGY sqa data is getting hand crafted
    { status: 'PENDING', reference_number: get_determination.body['reference_number'] }
  elsif get_determination.body['status'] == 'PENDING' && get_application.body['status'] == 'SUBMITTED'
    # SUBMITTED & RECEIVED ARE COMBINED ON LGY SIDE
    { status: 'PENDING', application_create_date: get_application.body['create_date'],
      reference_number: get_determination.body['reference_number'] }
  elsif get_determination.body['status'] == 'PENDING' && get_application.body['status'] == 'RETURNED'
    { status: 'PENDING_UPLOAD', application_create_date: get_application.body['create_date'],
      reference_number: get_determination.body['reference_number'] }
  else
    log_message_to_sentry(
      'Unexpected COE statuses!',
      :error,
      {
        determination_status: get_determination.body['status'],
        application_status: get_application.body['status'],
        get_application_status: get_application.status
      },
      { team: 'vfs-ebenefits' }
    )
    nil
  end
end

#end_pointObject (private)



199
200
201
# File 'lib/lgy/service.rb', line 199

def end_point
  "#{Settings.lgy.base_url}/eligibility-manager/api/eligibility"
end

#get_applicationObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lgy/service.rb', line 68

def get_application
  @get_application ||= with_monitoring do
    perform(
      :get,
      "#{end_point}/application",
      { 'edipi' => @edipi, 'icn' => @icn },
      request_headers
    )
  end
rescue Common::Client::Errors::ClientError => e
  # if the Veteran is automatically approved, LGY will return a 404 (no application exists)
  return e if e.status == 404

  raise e
end

#get_coe_documentsObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/lgy/service.rb', line 133

def get_coe_documents
  with_monitoring do
    perform(
      :get,
      "#{end_point}/documents",
      { 'edipi' => @edipi, 'icn' => @icn },
      request_headers
    )
  end
end

#get_coe_fileObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/lgy/service.rb', line 104

def get_coe_file
  with_monitoring do
    perform(
      :get,
      "#{end_point}/documents/coe/file",
      { 'edipi' => @edipi, 'icn' => @icn },
      request_headers.merge(pdf_headers)
    )
  end
rescue Common::Client::Errors::ClientError => e
  # a 404 is expected if no COE is available
  return e if e.status == 404

  raise e
end

#get_determinationObject

rubocop:enable Metrics/MethodLength



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

def get_determination
  @get_determination ||= with_monitoring do
    perform(
      :get,
      "#{end_point}/determination",
      { 'edipi' => @edipi, 'icn' => @icn },
      request_headers
    )
  end
end

#get_document(id) ⇒ Object

It is necessary to fetch a list of the user’s documents and check whether the requested document is within that list, to ensure that a vet cannot access documents belonging to other vets. LGY does not have a similar validation on their end.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/lgy/service.rb', line 148

def get_document(id)
  with_monitoring do
    document_ids = get_coe_documents.body.map { |doc| doc['id'].to_s }
    if document_ids.include?(id)
      perform(
        :get,
        "#{end_point}/document/#{id}/file",
        { 'edipi' => @edipi, 'icn' => @icn },
        request_headers.merge(pdf_headers)
      )
    else
      raise Common::Exceptions::RecordNotFound, id
    end
  end
end

#grant_manager_end_pointObject (private)



203
204
205
# File 'lib/lgy/service.rb', line 203

def grant_manager_end_point
  "#{Settings.lgy_sahsha.base_url}/grant-manager/api/grants"
end

#pdf_headersObject



191
192
193
194
195
# File 'lib/lgy/service.rb', line 191

def pdf_headers
  {
    'Accept' => 'application/octet-stream', 'Content-Type' => 'application/octet-stream'
  }
end

#post_document(payload:) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/lgy/service.rb', line 120

def post_document(payload:)
  with_monitoring do
    perform(
      :post,
      "#{end_point}/document?edipi=#{@edipi}&icn=#{@icn}",
      payload.to_json,
      request_headers
    )
  end
rescue Common::Client::Errors::ClientError => e
  raise e
end

#post_grant_application(payload:) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/lgy/service.rb', line 164

def post_grant_application(payload:)
  with_monitoring do
    perform(
      :post,
      "#{grant_manager_end_point}/application/createGrantApplication",
      payload.to_json,
      sahsha_request_headers
    )
  end
rescue Common::Client::Errors::ClientError => e
  raise e
end

#put_application(payload:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lgy/service.rb', line 84

def put_application(payload:)
  with_monitoring do
    response = perform(
      :put,
      "#{end_point}/application?edipi=#{@edipi}&icn=#{@icn}",
      payload.to_json,
      request_headers
    )
    response.body
  end
rescue Common::Client::Errors::ClientError => e
  log_message_to_sentry(
    "COE application submission failed with http status: #{e.status}",
    :error,
    { message: e.message, status: e.status, body: e.body },
    { team: 'vfs-ebenefits' }
  )
  raise e
end

#request_headersObject



177
178
179
180
181
# File 'lib/lgy/service.rb', line 177

def request_headers
  {
    Authorization: "api-key { \"appId\":\"#{Settings.lgy.app_id}\", \"apiKey\": \"#{Settings.lgy.api_key}\"}"
  }
end

#sahsha_request_headersObject



183
184
185
186
187
188
189
# File 'lib/lgy/service.rb', line 183

def sahsha_request_headers
  {
    Authorization: "api-key { \"appId\":\"#{Settings.lgy_sahsha.app_id}\", \"apiKey\": \"#{
      Settings.lgy_sahsha.api_key
    }\"}"
  }
end