Class: MDOT::Client

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/mdot/client.rb

Overview

Medical Devices Ordering Tool

This service integrates with the DLC API and allows veterans to reorder medical devices such as hearing aid batteries.

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.mdot'

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_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

Constructor Details

#initialize(current_user) ⇒ Client

Returns a new instance of Client.



27
28
29
30
# File 'lib/mdot/client.rb', line 27

def initialize(current_user)
  @user = current_user
  @supplies = 'supplies'
end

Instance Method Details

#get_suppliesFaraday::Response

GETs medical supplies available for reorder for veteran.

Returns:

  • (Faraday::Response)

    Faraday response instance.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mdot/client.rb', line 37

def get_supplies
  with_monitoring_and_error_handling do
    raw_response = perform(:get, @supplies, nil, headers)

    MDOT::Response.new(
      response: raw_response,
      schema: :supplies,
      uuid: @user.uuid
    )
  end
end

#handle_client_error(error) ⇒ Object (private)



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mdot/client.rb', line 125

def handle_client_error(error)
  save_error_details(error)
  code = if error&.status == 503
           'MDOT_service_unavailable'
         elsif error&.body&.fetch('result', nil)
           "MDOT_#{error.body['result'].downcase}"
         end

  raise_backend_exception(
    MDOT::ExceptionKey.new(code),
    self.class,
    error
  )
end

#handle_error(error) ⇒ Object (private)



140
141
142
143
144
145
146
147
148
149
# File 'lib/mdot/client.rb', line 140

def handle_error(error)
  case error
  when Faraday::ParsingError
    handle_parsing_error(error)
  when Common::Client::Errors::ClientError
    handle_client_error(error)
  else
    raise error
  end
end

#handle_parsing_error(error) ⇒ Object (private)



114
115
116
117
118
119
120
121
122
123
# File 'lib/mdot/client.rb', line 114

def handle_parsing_error(error)
  Sentry.set_extras(
    message: error.message,
    url: config.base_path
  )
  raise_backend_exception(
    MDOT::ExceptionKey.new('MDOT_502'),
    self.class
  )
end

#headersObject (private)



71
72
73
74
75
76
77
78
79
80
# File 'lib/mdot/client.rb', line 71

def headers
  {
    VA_VETERAN_FIRST_NAME: @user.first_name,
    VA_VETERAN_MIDDLE_NAME: @user.middle_name,
    VA_VETERAN_LAST_NAME: @user.last_name,
    VA_VETERAN_ID: @user.ssn.to_s.last(4),
    VA_VETERAN_BIRTH_DATE: @user.birth_date,
    VA_ICN: @user.icn
  }
end

#raise_backend_exception(key, source, error = nil) ⇒ Object (private)



104
105
106
107
108
109
110
111
112
# File 'lib/mdot/client.rb', line 104

def raise_backend_exception(key, source, error = nil)
  exception = MDOT::Exceptions::ServiceException.new(
    key,
    { source: source.to_s },
    error&.status,
    error&.body
  )
  raise exception
end

#save_error_details(error) ⇒ Object (private)



94
95
96
97
98
99
100
101
102
# File 'lib/mdot/client.rb', line 94

def save_error_details(error)
  Sentry.set_tags(external_service: self.class.to_s.underscore)

  Sentry.set_extras(
    url: config.base_path,
    message: error.message,
    body: error.body
  )
end

#submission_headersObject (private)



82
83
84
85
86
# File 'lib/mdot/client.rb', line 82

def submission_headers
  {
    VAAPIKEY: MDOT::Token.find(@user.uuid).token
  }
end

#submit_order(request_body) ⇒ Faraday::Response

POSTs to DLC endpoint to create a new order.

Returns:

  • (Faraday::Response)

    Faraday response instance.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mdot/client.rb', line 54

def submit_order(request_body)
  request_body.deep_transform_keys! { |key| key.to_s.camelize(:lower) }

  if request_body['order'].blank?
    raise_backend_exception(
      MDOT::ExceptionKey.new('MDOT_supplies_not_selected'),
      self.class
    )
  end

  with_monitoring_and_error_handling do
    perform(:post, @supplies, request_body, submission_headers).body
  end
end

#with_monitoring_and_error_handlingObject (private)



88
89
90
91
92
# File 'lib/mdot/client.rb', line 88

def with_monitoring_and_error_handling(&)
  with_monitoring(2, &)
rescue => e
  handle_error(e)
end