Class: IHub::Appointments::Service

Inherits:
Service show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/ihub/appointments/service.rb

Constant Summary

Constants inherited from Service

Service::STATSD_KEY_PREFIX

Instance Method Summary collapse

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Service

#initialize

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

Constructor Details

This class inherits a constructor from IHub::Service

Instance Method Details

#appointmentsIHub::Appointments::Response

Fetches a collection of veteran appointment data from iHub.

Per iHub docs, requires a service parameter of noFilter=true, in non-production environments.

Returns:

  • (IHub::Appointments::Response)

    Sample response: {

    :status       => 200,
    :appointments => [
      {
        "clinic_code"             => "409",
        "clinic_name"             => "ZZCHY WID BACK",
        "start_time"              => "1996-01-12T08:12:00",
        "type_name"               => "REGULAR",
        "status_name"             => "CHECKED OUT",
        "status_code"             => "2",
        "other_information"       => "",
        "type_code"               => "9",
        "appointment_status_code" => nil,
        "local_id"                => "2960112.0812",
        "appointment_status_name" => nil,
        "assigning_facility"      => nil,
        "facility_name"           => "CHEYENNE VAMC",
        "facility_code"           => "442"
      },
      ...
    ]
    

    }



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ihub/appointments/service.rb', line 45

def appointments
  icn_present!

  with_monitoring do
    response = perform(:get, appointments_url, nil)

    report_error!(response)

    IHub::Appointments::Response.from(response)
  end
rescue => e
  Sentry.set_extras(
    message: e.message,
    url: config.base_path
  )
  Sentry.set_tags(ihub: 'appointments')

  raise e
end

#appointments_urlObject (private)



75
76
77
78
79
80
81
# File 'lib/ihub/appointments/service.rb', line 75

def appointments_url
  if Settings.ihub.in_production
    @user.icn
  else
    "#{@user.icn}?noFilter=true"
  end
end

#icn_present!Object (private)



67
68
69
70
71
72
73
# File 'lib/ihub/appointments/service.rb', line 67

def icn_present!
  if @user.icn.blank?
    error = Common::Client::Errors::ClientError.new('User has no ICN', 500, 'User has no ICN')

    raise_backend_exception('IHUB_102', self.class, error)
  end
end

#log_error(response) ⇒ Object (private)



103
104
105
106
107
108
# File 'lib/ihub/appointments/service.rb', line 103

def log_error(response)
  Sentry.set_extras(
    response_body: response.body.merge('status_code' => response.status)
  )
  Sentry.set_tags(ihub: 'appointments_error_occurred')
end

#report_error!(response) ⇒ Object (private)

When an iHub error occurs, iHub sets a ‘error_occurred’ key to true, returns a status of 200, and includes the error’s details in the response.body hash.

Parameters:

  • response (Faraday::Env)

    The raw response from the iHub Appointments endpoint. Sample response.body when an error has occurred:

    {
      "error_occurred" => true,
      "error_message"  => "An unexpected error occurred processing request!",
      "status"         => "Error",
      "debug_info"     => "Invalid CRM Webpart URL parameters. Invalid client name.",
      "data"           => []
    }
    


96
97
98
99
100
101
# File 'lib/ihub/appointments/service.rb', line 96

def report_error!(response)
  if response.body&.dig('error_occurred')
    log_error(response)
    raise_backend_exception('IHUB_101', self.class, response)
  end
end