Class: UcbRailsUser::UserUcPathService

Inherits:
Object
  • Object
show all
Defined in:
app/models/ucb_rails_user/user_uc_path_service.rb

Defined Under Namespace

Classes: UcPathClient

Class Method Summary collapse

Class Method Details

.create_or_update_user_from_employee_id(employee_id) ⇒ Object



7
8
9
10
11
12
# File 'app/models/ucb_rails_user/user_uc_path_service.rb', line 7

def create_or_update_user_from_employee_id(employee_id)
  ucpath_entry = ucpath_client.fetch_employee_data_with_employee_id(employee_id)
  return nil unless ucpath_entry.present?
  user = UcbRailsUser.user_class.find_or_initialize_by(employee_id: employee_id)
  update_user_record_from_ucpath_entry!(user, ucpath_entry)
end

.create_or_update_user_from_ldap_uid(ldap_uid) ⇒ Object



14
15
16
17
18
19
# File 'app/models/ucb_rails_user/user_uc_path_service.rb', line 14

def create_or_update_user_from_ldap_uid(ldap_uid)
  ucpath_entry = ucpath_client.fetch_employee_data_with_ldap_uid(ldap_uid)
  return nil unless ucpath_entry.present?
  user = UcbRailsUser.user_class.find_or_initialize_by(ldap_uid: ldap_uid)
  update_user_record_from_ucpath_entry!(user, ucpath_entry)
end

.find_name_by_type(names, type) ⇒ Object



48
49
50
51
52
# File 'app/models/ucb_rails_user/user_uc_path_service.rb', line 48

def find_name_by_type(names, type)
  names&.detect do |n|
    n.dig("type", "code") == type
  end
end

.parse_email(entry) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/models/ucb_rails_user/user_uc_path_service.rb', line 54

def parse_email(entry)
  email_entry =
    entry["emails"]&.detect do |email|
      email["primary"] == true
    end
  email_entry ||= entry["emails"]&.first # if there's no primary email, grab whatever we can
  email_entry&.fetch("emailAddress")
end

.parse_name(entry) ⇒ Object



42
43
44
45
46
# File 'app/models/ucb_rails_user/user_uc_path_service.rb', line 42

def parse_name(entry)
  return nil unless entry.present?
  find_name_by_type(entry["names"], "PRF") ||
    find_name_by_type(entry["names"], "PRI")
end

.ucpath_clientObject



21
22
23
# File 'app/models/ucb_rails_user/user_uc_path_service.rb', line 21

def ucpath_client
  UcPathClient.new
end

.update_user_record_from_ucpath_entry!(user, ucpath_entry) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/ucb_rails_user/user_uc_path_service.rb', line 25

def update_user_record_from_ucpath_entry!(user, ucpath_entry)
  user.tap do |u|
    name_entry = parse_name(ucpath_entry)
    u.first_name = name_entry["givenName"]
    u.last_name = name_entry["familyName"]
    u.employee_id ||= ucpath_entry["identifiers"]&.detect do |id|
      id["type"] == "hr-employee-id"
    end&.fetch("id")
    u.ldap_uid ||= ucpath_entry["identifiers"]&.detect do |id|
      id["type"] == "campus-uid"
    end&.fetch("id")
    u.email = parse_email(ucpath_entry)
    u.inactive_flag = false # any way to pull this from the API?
    u.save!
  end
end