Class: VAProfile::Demographics::Service

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

Constant Summary collapse

OID =
'2.16.840.1.113883.4.349'
STATSD_KEY_PREFIX =
"#{VAProfile::Service::STATSD_KEY_PREFIX}.demographics".freeze

Instance Method Summary collapse

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

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Service

breakers_service, #final_failure?, #handle_error, #initialize, #log_dates, #parse_messages, #perform, #person_transaction?, #person_transaction_failure?, #raise_backend_exception, #raise_invalid_body, #report_stats_on, #save_error_details

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 VAProfile::Service

Instance Method Details

#aaidObject (private)



110
111
112
113
114
# File 'lib/va_profile/demographics/service.rb', line 110

def aaid
  return '^PN^200VIDM^USDVA' if @user&.idme_uuid.present?

  '^PN^200VLGN^USDVA' if @user&.logingov_uuid.present?
end

#build_response(status, body) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/va_profile/demographics/service.rb', line 86

def build_response(status, body)
  DemographicResponse.from(
    status:,
    body:,
    id: @user.,
    type: 'mvi_models_mvi_profiles',
    gender: @user.gender_mpi,
    birth_date: @user.birth_date_mpi
  )
end

#csp_idObject (private)

For now, only valid if CSP (credential service provider) is id.me or login.gov



104
105
106
107
108
# File 'lib/va_profile/demographics/service.rb', line 104

def csp_id
  return @user&.idme_uuid if @user&.idme_uuid.present?

  @user&.logingov_uuid.presence
end

#csp_id_with_aaidObject (private)



99
100
101
# File 'lib/va_profile/demographics/service.rb', line 99

def csp_id_with_aaid
  "#{csp_id}#{aaid}"
end

#get_demographicsObject

Returns a response object containing the user’s preferred name, and gender-identity



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
# File 'lib/va_profile/demographics/service.rb', line 22

def get_demographics
  with_monitoring do
    return build_response(401, nil) unless DemographicsPolicy.new(@user).access_update?

    response = perform(:get, identity_path)
    build_response(response&.status, response&.body)
  end
rescue Common::Client::Errors::ClientError => e
  if e.status == 404
    log_exception_to_sentry(
      e,
      { csp_id_with_aaid: },
      { va_profile: :demographics_not_found },
      :warning
    )

    return build_response(404, nil)
  elsif e.status >= 400 && e.status < 500
    return build_response(e.status, nil)
  end

  handle_error(e)
rescue => e
  handle_error(e)
end

#identity_path(dir = nil) ⇒ Object

VA Profile demographic endpoints use the OID (Organizational Identifier), the CSP ID, and the Assigning Authority ID to identify which person will be updated/retrieved.



81
82
83
84
# File 'lib/va_profile/demographics/service.rb', line 81

def identity_path(dir = nil)
  path = "#{OID}/#{ERB::Util.url_encode(csp_id_with_aaid.to_s)}"
  dir ? "#{path}/#{dir}" : path
end

#post_or_put_data(method, model, path, response_class) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/va_profile/demographics/service.rb', line 60

def post_or_put_data(method, model, path, response_class)
  with_monitoring do
    raise 'User does not have a valid CSP ID' unless DemographicsPolicy.new(@user).access_update?

    model.set_defaults(@user)
    response = perform(method, identity_path(path), model.in_json)

    return response_class.new(200, "#{model.model_name.element}": model) if response_successful?(response)

    response_class.from(response)
  end
rescue => e
  handle_error(e)
end

#response_successful?(response) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/va_profile/demographics/service.rb', line 75

def response_successful?(response)
  response&.status == 200 && response&.body == {}
end

#save_gender_identity(gender_identity) ⇒ Object

PUTs an updated gender_identity to the VAProfile API

Parameters:



56
57
58
# File 'lib/va_profile/demographics/service.rb', line 56

def save_gender_identity(gender_identity)
  post_or_put_data(:post, gender_identity, 'gender-identity', GenderIdentityResponse)
end

#save_preferred_name(preferred_name) ⇒ Object

PUTs an updated preferred_name to the VAProfile API

Parameters:



50
51
52
# File 'lib/va_profile/demographics/service.rb', line 50

def save_preferred_name(preferred_name)
  post_or_put_data(:post, preferred_name, 'preferred-name', PreferredNameResponse)
end