Class: MnoEnterprise::IntercomEventsListener

Inherits:
Object
  • Object
show all
Defined in:
lib/mno_enterprise/intercom_events_listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIntercomEventsListener



9
10
11
12
13
14
15
16
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 9

def initialize
  args = if MnoEnterprise.intercom_token
    {token: MnoEnterprise.intercom_token}
  else
    {app_id: MnoEnterprise.intercom_app_id, api_key: MnoEnterprise.intercom_api_key}
  end
  self.intercom = ::Intercom::Client.new(args)
end

Instance Attribute Details

#intercomObject

Returns the value of attribute intercom.



7
8
9
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 7

def intercom
  @intercom
end

Instance Method Details

#format_company(organization) ⇒ Object

Formatting TODO: extract to a CRM service



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 73

def format_company(organization)
  {
    company_id: organization.id,
    name: organization.name,
    created_at: organization.created_at.to_i,
    custom_attributes: {
      industry: organization.industry,
      size: organization.size,
      credit_card_details: organization.has_credit_card_details?,
      credit_card_expiry: organization.credit_card.expiry_date,
      app_count: organization.app_instances.count,
      app_list: organization.app_instances.map(&:name).sort.to_sentence,
      user_count: organization.users.count
    }
  }
end

#info(key, current_user_id, description, subject_type, subject_id, metadata) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 18

def info(key, current_user_id, description, subject_type, subject_id, )
  u = User.find(current_user_id)
  data = {created_at: Time.now.to_i, email: u.email, user_id: u.id, event_name: key.tr('_', '-')}
  case key
    when 'user_update', 'organization_update'
      # convert values to string
      data[:metadata] = Hash[ .collect {|k,v| [k, v.to_s] } ]
    when 'user_confirm'
      data[:event_name] = 'finished-sign-up'
    when 'dashboard_create'
      data[:event_name] = 'added-dashboard'
    when 'dashboard_delete'
      data[:event_name] = 'removed-dashboard'
    when 'widget_delete'
      data[:event_name] = 'removed-widget'
    when 'widget_create'
      data[:event_name] = 'added-widget'
      data[:metadata] = {widget: [:name]}
    when 'app_launch'
      data[:event_name] = 'launched-app-' + [:app_nid]
    when 'app_destroy'
      data[:event_name] = 'deleted-app-'  + [:app_nid]
      data[:metadata] = {type: 'single', app_list: [:app_nid]}
    when 'app_add'
      data[:event_name] = 'added-app-' + [:app_nid]
      data[:metadata] = {type: 'single', app_list: [:app_nid]}
  end
  # Update user data in intercom
  # OPTIMIZE: we could fetch the user for intercom and only update fields that have changed
  self.update_intercom_user(u)
  # Push the event to intercom
  self.intercom.events.create(data)

rescue Intercom::IntercomError => e
  Rails.logger.tagged('Intercom') { Rails.logger.warn 'Error while calling intercom ' + e.message}
end

#tag_user(user) ⇒ Object

If a source is set, tag the user with it



65
66
67
68
69
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 65

def tag_user(user)
  if user. && user.[:source].present?
    intercom.tags.tag(name: user.[:source], users: [{user_id: user.id}])
  end
end

#update_intercom_user(user, update_last_request_at = true) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 55

def update_intercom_user(user, update_last_request_at = true)
  data = user.intercom_data(update_last_request_at)
  data[:companies] = user.organizations.map do |organization|
    format_company(organization)
  end
  intercom.users.create(data)
  tag_user(user)
end