Class: Killbill::Zendesk::UserUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/zendesk/user_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, kb_apis, logger) ⇒ UserUpdater

Returns a new instance of UserUpdater.



3
4
5
6
7
# File 'lib/zendesk/user_updater.rb', line 3

def initialize(client, kb_apis, logger)
  @client = client
  @kb_apis = kb_apis
  @logger = logger
end

Instance Method Details

#build_details_field(kb_account) ⇒ Object

Build the Zendesk details field (address information)



49
50
51
52
# File 'lib/zendesk/user_updater.rb', line 49

def build_details_field()
  details = [.address1, .address2, .city, .state_or_province, .postal_code, .country]
  (details.reject { |detail| detail.blank? }).join(', ')
end

#create_user(kb_account) ⇒ Object

Create a user in Zendesk



55
56
57
# File 'lib/zendesk/user_updater.rb', line 55

def create_user()
  @client.users.create(:name => .name)
end

#find_all_by_email(email) ⇒ Object



112
113
114
# File 'lib/zendesk/user_updater.rb', line 112

def find_all_by_email(email)
  @client.search(:query => "type:user email:#{email}", :reload => true)
end

#find_all_by_external_id(external_id) ⇒ Object



104
105
106
# File 'lib/zendesk/user_updater.rb', line 104

def find_all_by_external_id(external_id)
  @client.search(:query => "type:user external_id:#{external_id}", :reload => true)
end

#find_by_email(email) ⇒ Object



108
109
110
# File 'lib/zendesk/user_updater.rb', line 108

def find_by_email(email)
  find_all_by_email(email).first
end

#find_by_external_id(external_id) ⇒ Object



100
101
102
# File 'lib/zendesk/user_updater.rb', line 100

def find_by_external_id(external_id)
  find_all_by_external_id(external_id).first
end

#find_by_id(kb_account_id) ⇒ Object



95
96
97
98
# File 'lib/zendesk/user_updater.rb', line 95

def find_by_id()
  zd_user = ZendeskUser.()
  zd_user ? @client.users.find(:id => zd_user.zd_user_id) : nil
end

#find_by_kb_account(kb_account) ⇒ Object

Find the Zendesk user associated with that Kill Bill account



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zendesk/user_updater.rb', line 70

def ()
  # Do we have already a mapping for that user?
   = find_by_id(.id)
  return  if 

  # TODO In the search results below, should we worry about potential dups?

  # First search by external_id, which is the safest method.
  # The external_id is either the account external key...
   = find_by_external_id(.external_key) if .external_key
  return  if 

  # ...or the Kill Bill account id
   = find_by_external_id(.id)
  return  if 

  # At this point, we haven't matched this user yet. To reconcile it, use the email address which is guaranteed
  # to exist on the Zendesk side
   = find_by_email(.email) if .email
  return  if 

  # We couldn't find a match - the account will be created
  nil
end

#lookup_kb_account(lookup_key) ⇒ Object

Find the Kill Bill account associated with that lookup_key (account id or external key)



40
41
42
43
44
45
46
# File 'lib/zendesk/user_updater.rb', line 40

def (lookup_key)
  if lookup_key =~ /[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/
    @kb_apis..(lookup_key, @kb_apis.create_context)
  else
    @kb_apis..(lookup_key, @kb_apis.create_context)
  end
end

#save_kb_zd_mapping(kb_account, user) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/zendesk/user_updater.rb', line 59

def save_kb_zd_mapping(, user)
  # Save the mapping locally - this is required due to the indexing lag on the Zendesk side,
  # see https://support.zendesk.com/entries/20239737:
  #   When you add new data to your Zendesk, it typically takes about 2 to 3 minutes before it's indexed and can be searched.
  # This is unacceptable for us: if an account creation event is quickly followed by a account update event,
  # we wouldn't be able to retrieve the user, potentially causing duplicates and/or triggering validation errors, e.g.
  #   Email [email protected] is already being used by another user
  ZendeskUser.where(:kb_account_id => .id, :zd_user_id => user.id).first_or_create!
end

#update(lookup_key) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zendesk/user_updater.rb', line 9

def update(lookup_key)
   = (lookup_key)

  user = ()
  user = create_user() if user.nil?
  if user.nil?
    @logger.warn "Failed to create user in Zendesk for account #{lookup_key}"
    return
  end


  save_kb_zd_mapping(, user)

  user.name = .name
  user.external_id = .external_key || .id
  user.locale = .locale
  user.timezone = .time_zone
  user.email = .email
  user.phone = .phone
  user.details = build_details_field()

  if user.save
    @logger.info "Successfully updated #{user.name} in Zendesk: #{user.url}"
    user.url
  else
    @logger.warn "Unable to update #{user.name} in Zendesk: #{user.url}"
    nil
  end
end