Class: MHVAccountTypeService
- Inherits:
-
Object
- Object
- MHVAccountTypeService
- Defined in:
- app/services/mhv_account_type_service.rb
Overview
Models logic pertaining to the verification and logging of MHV accounts
Constant Summary collapse
- ELIGIBLE_DATA_CLASS_COUNT_TO_ACCOUNT_LEVEL =
{ 32 => 'Premium', 18 => 'Advanced', 16 => 'Basic' }.freeze
- DEFAULT_ACCOUNT_LEVEL =
'Unknown'
- MHV_DOWN_MESSAGE =
'MHVAccountTypeService: could not fetch eligible data classes'
- UNEXPECTED_DATA_CLASS_COUNT_MESSAGE =
'MHVAccountTypeService: eligible data class mapping inconsistency'
Instance Attribute Summary collapse
-
#eligible_data_classes ⇒ Object
readonly
Returns the value of attribute eligible_data_classes.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#account_type_known? ⇒ Boolean
Is the user MHV account type known?.
- #cached_eligible_data_class ⇒ Object private
- #fetch_eligible_data_classes ⇒ Object private
-
#initialize(user) ⇒ MHVAccountTypeService
constructor
A new instance of MHVAccountTypeService.
- #log_account_type_heuristic(message, extra_context = {}) ⇒ Object private
-
#mhv_account? ⇒ Boolean
Does the user have an MHV correlation ID?.
-
#mhv_account_type ⇒ NilClass, String
Retrieve the MHV account type.
Constructor Details
#initialize(user) ⇒ MHVAccountTypeService
Returns a new instance of MHVAccountTypeService.
20 21 22 23 |
# File 'app/services/mhv_account_type_service.rb', line 20 def initialize(user) @user = user @eligible_data_classes = fetch_eligible_data_classes if mhv_account? end |
Instance Attribute Details
#eligible_data_classes ⇒ Object (readonly)
Returns the value of attribute eligible_data_classes.
25 26 27 |
# File 'app/services/mhv_account_type_service.rb', line 25 def eligible_data_classes @eligible_data_classes end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
25 26 27 |
# File 'app/services/mhv_account_type_service.rb', line 25 def user @user end |
Instance Method Details
#account_type_known? ⇒ Boolean
Returns is the user MHV account type known?.
58 59 60 |
# File 'app/services/mhv_account_type_service.rb', line 58 def account_type_known? user.identity.mhv_account_type.present? end |
#cached_eligible_data_class ⇒ Object (private)
78 79 80 81 82 |
# File 'app/services/mhv_account_type_service.rb', line 78 def cached_eligible_data_class namespace = Redis::Namespace.new('common_collection', redis: $redis) cache_key = "#{user.mhv_correlation_id}:geteligibledataclass" namespace.get(cache_key) end |
#fetch_eligible_data_classes ⇒ Object (private)
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/services/mhv_account_type_service.rb', line 64 def fetch_eligible_data_classes if cached_eligible_data_class json = Oj.load(cached_eligible_data_class) Common::Collection.new(::EligibleDataClass, **json.symbolize_keys).members.map(&:name) else bb_client = BB::Client.new(session: { user_id: @user.mhv_correlation_id }) bb_client.authenticate bb_client.get_eligible_data_classes.members.map(&:name) end rescue => e log_account_type_heuristic(MHV_DOWN_MESSAGE, error_message: e.) nil end |
#log_account_type_heuristic(message, extra_context = {}) ⇒ Object (private)
84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/services/mhv_account_type_service.rb', line 84 def log_account_type_heuristic(, extra_context = {}) extra_context.merge!( uuid: user.uuid, mhv_correlation_id: user.mhv_correlation_id, eligible_data_classes:, authn_context: user.authn_context, va_patient: user.va_patient?, mhv_acct_type: user.identity.mhv_account_type ) Rails.logger.warn("#{}, #{extra_context}") end |
#mhv_account? ⇒ Boolean
Returns does the user have an MHV correlation ID?.
51 52 53 |
# File 'app/services/mhv_account_type_service.rb', line 51 def mhv_account? user.mhv_correlation_id.present? end |
#mhv_account_type ⇒ NilClass, String
Retrieve the MHV account type
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/services/mhv_account_type_service.rb', line 33 def mhv_account_type return nil unless mhv_account? if account_type_known? user.identity.mhv_account_type elsif eligible_data_classes.nil? 'Error' else ELIGIBLE_DATA_CLASS_COUNT_TO_ACCOUNT_LEVEL.fetch(eligible_data_classes.size) end rescue KeyError log_account_type_heuristic(UNEXPECTED_DATA_CLASS_COUNT_MESSAGE) DEFAULT_ACCOUNT_LEVEL end |