Class: MHV::UserAccount::Creator

Inherits:
Object
  • Object
show all
Defined in:
app/services/mhv/user_account/creator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_verification:, break_cache: false) ⇒ Creator

Returns a new instance of Creator.



10
11
12
13
# File 'app/services/mhv/user_account/creator.rb', line 10

def initialize(user_verification:, break_cache: false)
  @user_verification = user_verification
  @break_cache = break_cache
end

Instance Attribute Details

#break_cacheObject (readonly)

Returns the value of attribute break_cache.



8
9
10
# File 'app/services/mhv/user_account/creator.rb', line 8

def break_cache
  @break_cache
end

#user_verificationObject (readonly)

Returns the value of attribute user_verification.



8
9
10
# File 'app/services/mhv/user_account/creator.rb', line 8

def user_verification
  @user_verification
end

Instance Method Details

#create_mhv_user_account!Object (private)



32
33
34
35
36
37
# File 'app/services/mhv/user_account/creator.rb', line 32

def create_mhv_user_account!
   = MHVUserAccount.new()
  .validate!
  MPIData.find(icn)&.destroy
  
end

#current_tou_agreementObject (private)



53
54
55
# File 'app/services/mhv/user_account/creator.rb', line 53

def current_tou_agreement
  @current_tou_agreement ||= .terms_of_use_agreements.current.last
end

#emailObject (private)



49
50
51
# File 'app/services/mhv/user_account/creator.rb', line 49

def email
  @email ||= user_verification.user_credential_email&.credential_email
end

#icnObject (private)



45
46
47
# File 'app/services/mhv/user_account/creator.rb', line 45

def icn
  @icn ||= .icn
end

#log_error(error, type) ⇒ Object (private)



76
77
78
# File 'app/services/mhv/user_account/creator.rb', line 76

def log_error(error, type)
  Rails.logger.error("[MHV][UserAccount][Creator] #{type} error", error_message: error.message, icn:)
end

#mhv_account_creation_responseObject (private)



39
40
41
42
43
# File 'app/services/mhv/user_account/creator.rb', line 39

def 
  tou_occurred_at = current_tou_agreement.created_at

  mhv_client.(icn:, email:, tou_occurred_at:, break_cache:)
end

#mhv_clientObject (private)



61
62
63
# File 'app/services/mhv/user_account/creator.rb', line 61

def mhv_client
  MHV::AccountCreation::Service.new
end

#performObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/mhv/user_account/creator.rb', line 15

def perform
  validate!
  create_mhv_user_account!
rescue ActiveModel::ValidationError, Errors::ValidationError => e
  log_error(e, :validation)
  raise Errors::ValidationError, e.message
rescue Common::Client::Errors::Error => e
  log_error(e, :client)

  raise Errors::MHVClientError.new(e.message, e.body)
rescue => e
  log_error(e, :creator)
  raise Errors::CreatorError, e.message
end

#user_accountObject (private)



57
58
59
# File 'app/services/mhv/user_account/creator.rb', line 57

def 
  @user_account ||= user_verification.
end

#validate!Object (private)



65
66
67
68
69
70
71
72
73
74
# File 'app/services/mhv/user_account/creator.rb', line 65

def validate!
  errors = [
    ('ICN must be present' if icn.blank?),
    ('Email must be present' if email.blank?),
    ('Current terms of use agreement must be present' if current_tou_agreement.blank?),
    ("Current terms of use agreement must be 'accepted'" unless current_tou_agreement&.accepted?)
  ].compact

  raise Errors::ValidationError, errors.join(', ') if errors.present?
end