Class: MHVAccountTypeService

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_classesObject (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

#userObject (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?.

Returns:

  • (Boolean)

    is the user MHV account type known?



58
59
60
# File 'app/services/mhv_account_type_service.rb', line 58

def 
  user.identity..present?
end

#cached_eligible_data_classObject (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_classesObject (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
  (MHV_DOWN_MESSAGE, error_message: e.message)
  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 (message, 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.
  )
  Rails.logger.warn("#{message}, #{extra_context}")
end

#mhv_account?Boolean

Returns does the user have an MHV correlation ID?.

Returns:

  • (Boolean)

    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_typeNilClass, String

Retrieve the MHV account type

Returns:

  • (NilClass)

    if the account is not an MHV account

  • (String)

    if the account is an MHV account, returns the 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 
  return nil unless mhv_account?

  if 
    user.identity.
  elsif eligible_data_classes.nil?
    'Error'
  else
    ELIGIBLE_DATA_CLASS_COUNT_TO_ACCOUNT_LEVEL.fetch(eligible_data_classes.size)
  end
rescue KeyError
  (UNEXPECTED_DATA_CLASS_COUNT_MESSAGE)
  DEFAULT_ACCOUNT_LEVEL
end