Class: MHV::AccountCreation::Service

Inherits:
Common::Client::Base show all
Defined in:
lib/mhv/account_creation/service.rb

Instance Method Summary collapse

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

Instance Method Details

#authenticated_header(icn:) ⇒ Object (private)



53
54
55
56
57
58
# File 'lib/mhv/account_creation/service.rb', line 53

def authenticated_header(icn:)
  {
    'Authorization' => "Bearer #{config.sts_token(user_identifier: icn)}",
    'x-api-key' => config.access_key
  }
end

#build_create_account_params(icn:, email:, tou_occurred_at:) ⇒ Object (private)



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mhv/account_creation/service.rb', line 41

def (icn:, email:, tou_occurred_at:)
  {
    icn:,
    email:,
    vaTermsOfUseDateTime: tou_occurred_at.iso8601,
    vaTermsOfUseStatus: config.tou_status,
    vaTermsOfUseRevision: config.tou_revision,
    vaTermsOfUseLegalVersion: config.tou_legal_version,
    vaTermsOfUseDocTitle: config.tou_doc_title
  }.to_json
end

#create_account(icn:, email:, tou_occurred_at:, break_cache: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mhv/account_creation/service.rb', line 10

def (icn:, email:, tou_occurred_at:, break_cache: false)
  params = (icn:, email:, tou_occurred_at:)

  (icn:, force: break_cache, expires_in: 1.day) do
    Rails.logger.info("#{config.logging_prefix} create_account request", { icn: })
    response = perform(:post, config., params, authenticated_header(icn:))
    normalize_response_body(response.body)
  end
rescue Common::Client::Errors::ParsingError, Common::Client::Errors::ClientError => e
  Rails.logger.error("#{config.logging_prefix} create_account #{e.class.name.demodulize.underscore}",
                     { error_message: e.message, body: e.body, status: e.status, icn: })
  raise
end

#create_account_with_cache(icn:, force:, expires_in:, &request) ⇒ Object (private)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mhv/account_creation/service.rb', line 26

def (icn:, force:, expires_in:, &request)
  cache_hit = true
  start = nil
   = Rails.cache.fetch("#{config.service_name}_#{icn}", force:, expires_in:) do
    cache_hit = false
    start = Time.current
    request.call
  end
  duration_ms = cache_hit ? nil : (Time.current - start).seconds.in_milliseconds

  Rails.logger.info("#{config.logging_prefix} create_account success",
                    { icn:, account:, duration_ms:, from_cache: cache_hit }.compact)
  
end

#normalize_response_body(response_body) ⇒ Object (private)



60
61
62
63
64
65
66
67
68
69
# File 'lib/mhv/account_creation/service.rb', line 60

def normalize_response_body(response_body)
  {
    user_profile_id: response_body['mhv_userProfileId'],
    premium: response_body['isPremium'],
    champ_va: response_body['isChampVABeneficiary'],
    patient: response_body['isPatient'],
    sm_account_created: response_body['isSMAccountCreated'],
    message: response_body['message']
  }
end