Class: Identity::AccountCreator

Inherits:
Object
  • Object
show all
Defined in:
app/services/identity/account_creator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ AccountCreator

Returns a new instance of AccountCreator.



5
6
7
# File 'app/services/identity/account_creator.rb', line 5

def initialize(user)
  @user = user
end

Instance Attribute Details

#userObject

Returns the value of attribute user.



9
10
11
# File 'app/services/identity/account_creator.rb', line 9

def user
  @user
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
# File 'app/services/identity/account_creator.rb', line 11

def call
  return unless user.idme_uuid || user.logingov_uuid || user.sec_id

   = create_if_needed!

  update_if_needed!()
end

#clean_up_deprecated_accounts(current_account_id, uuid, identifier) ⇒ Object (private)



81
82
83
84
85
86
87
# File 'app/services/identity/account_creator.rb', line 81

def clean_up_deprecated_accounts(, uuid, identifier)
  return unless uuid

   = Account.find_by(identifier => uuid)

  .destroy if  && .id != 
end

#create_account_attribute_hash(identity) ⇒ Object (private)



47
48
49
50
51
52
53
54
55
# File 'app/services/identity/account_creator.rb', line 47

def (identity)
  {
    idme_uuid: identity.idme_uuid,
    logingov_uuid: identity.logingov_uuid,
    sec_id: identity.sec_id,
    edipi: identity.edipi,
    icn: identity.icn
  }.compact
end

#create_if_needed!Object (private)



21
22
23
24
25
# File 'app/services/identity/account_creator.rb', line 21

def create_if_needed!
  accounts = get_accounts_for_user
   = accounts.length > 1 ? (accounts) : accounts.first
  .presence || Account.create(**(user))
end

#find_matching_account(accounts) ⇒ Object (private)



33
34
35
36
37
38
# File 'app/services/identity/account_creator.rb', line 33

def (accounts)
  (accounts, :idme_uuid) ||
    (accounts, :logingov_uuid) ||
    (accounts, :sec_id) ||
    accounts.first
end

#get_accounts_for_userObject (private)



27
28
29
30
31
# File 'app/services/identity/account_creator.rb', line 27

def get_accounts_for_user
  Account.idme_uuid_match(user.idme_uuid)
         .or(Account.sec_id_match(user.sec_id))
         .or(Account.logingov_uuid_match(user.logingov_uuid))
end

#hash_diff(first_hash, second_hash) ⇒ Object (private)



77
78
79
# File 'app/services/identity/account_creator.rb', line 77

def hash_diff(first_hash, second_hash)
  (first_hash.to_a - second_hash.to_a).to_h
end

#match_account_for_identifier(accounts, identifier) ⇒ Object (private)



40
41
42
43
44
45
# File 'app/services/identity/account_creator.rb', line 40

def (accounts, identifier)
  user_identifier_id = user.send(identifier)
  return unless user_identifier_id

  accounts.find { || .send(identifier) == user_identifier_id }
end

#update_if_needed!(account) ⇒ Object (private)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/identity/account_creator.rb', line 57

def update_if_needed!()
  # account has yet to be saved, no need to update
  return  unless .persisted?

  # return account as is if all non-nil user attributes match up to be the same
   = ()
  user_attributes = (user)
  attribute_diff = hash_diff(user_attributes, )

  return  if attribute_diff.blank?

  if attribute_diff[:logingov_uuid]
    clean_up_deprecated_accounts(.id,
                                 attribute_diff[:logingov_uuid],
                                 :logingov_uuid)
  end

  Account.update(.id, **user_attributes)
end