forked from maestrano/mno-enterprise-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request maestrano#82 from x4d3/feature/intercom
Add Intercom backend integration
- Loading branch information
Showing
12 changed files
with
227 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'httparty' | ||
|
||
module MnoEnterprise | ||
class AuditEventsListener | ||
include HTTParty | ||
base_uri "#{MnoEnterprise.mno_api_private_host || MnoEnterprise.mno_api_host}/api/mnoe/v1/audit_events" | ||
read_timeout 0.1 | ||
basic_auth MnoEnterprise.tenant_id, MnoEnterprise.tenant_key | ||
|
||
def info(key, current_user_id, description, metadata, object) | ||
self.class.post('', body: { | ||
data: { | ||
key: key, | ||
user_id: current_user_id, | ||
description: description, | ||
metadata: metadata, | ||
subject_type: object.class.name, | ||
subject_id: object.id | ||
}}) | ||
rescue Net::ReadTimeout | ||
# Meant to fail | ||
end | ||
|
||
end | ||
|
||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require 'intercom' | ||
|
||
module MnoEnterprise | ||
|
||
class IntercomEventsListener | ||
|
||
attr_accessor :intercom | ||
|
||
def initialize | ||
self.intercom = ::Intercom::Client.new(app_id: MnoEnterprise.intercom_app_id, api_key: MnoEnterprise.intercom_api_key) | ||
end | ||
|
||
def info(key, current_user_id, description, metadata, object) | ||
u = User.find(current_user_id) | ||
begin | ||
intercom.users.find(:user_id => current_user_id) | ||
rescue Intercom::ResourceNotFound | ||
self.update_intercom_user(u) | ||
end | ||
data = {created_at: Time.now.to_i, email: u.email} | ||
case key | ||
when 'user_update' | ||
self.update_intercom_user(u) | ||
return | ||
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: object.name} | ||
when 'app_destroy' | ||
data[:event_name] = 'deleted-app-' + object.app.nid | ||
data[:metadata] = {type: 'single', app_list: object.app.nid} | ||
when 'app_add' | ||
data[:event_name] = 'added-app-' + object.app.nid | ||
data[:metadata] = {type: 'single', app_list: object.app.nid} | ||
else | ||
data[:event_name] = key.tr!('_', '-') | ||
end | ||
|
||
self.intercom.events.create(data) | ||
|
||
rescue Intercom::IntercomError => e | ||
Rails.logger.debug '[INTERCOM] Could not call intercom: ' + e.message | ||
end | ||
|
||
def update_intercom_user(user) | ||
data = { | ||
user_id: user.id, | ||
name: [user.name, user.surname].join(' '), | ||
email: user.email, | ||
created_at: user.created_at.to_i, | ||
last_seen_ip: user.last_sign_in_ip, | ||
custom_attributes: {} | ||
} | ||
data[:custom_attributes][:phone]= user.phone if user.phone | ||
|
||
data[:companies] = user.organizations.map do |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.credit_card?, | ||
app_count: organization.app_instances.count, | ||
app_list: organization.app_instances.map { |app| app.name }.to_sentence | ||
} | ||
} | ||
end | ||
|
||
intercom.users.create(data) | ||
end | ||
|
||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'rails_helper' | ||
|
||
module MnoEnterprise | ||
RSpec.describe IntercomEventsListener do | ||
let(:app) { build(:app) } | ||
let(:app_instance) { build(:app_instance, app: app, organization_id: organization.id) } | ||
let(:user) { build(:user) } | ||
let(:organization) { build(:organization) } | ||
before do | ||
api_stub_for(get: "/users/#{user.id}", response: from_api(user)) | ||
api_stub_for(get: "/users/#{user.id}/organizations", response: from_api([organization])) | ||
api_stub_for(get: "/organizations/#{organization.id}/app_instances", response: from_api([app_instance])) | ||
end | ||
|
||
|
||
describe '#info' do | ||
let(:events) { double('events') } | ||
context 'when the user already exist in intercom' do | ||
before do | ||
users = double('users') | ||
|
||
client = double('client', users: users, events: events) | ||
expect(Intercom::Client).to receive(:new).with(app_id: MnoEnterprise.intercom_app_id, api_key: MnoEnterprise.intercom_api_key).and_return(client) | ||
|
||
expect(users).to receive(:find) | ||
end | ||
|
||
subject { MnoEnterprise::IntercomEventsListener.new } | ||
|
||
it 'add an event when an password is changed' do | ||
expect(events).to receive(:create).with(hash_including(email: user.email, event_name: 'user-update-password')) | ||
subject.info('user_update_password', user.id, 'User password change', user.email, user) | ||
end | ||
|
||
it 'add an event when an app is added' do | ||
expect(events).to receive(:create).with(hash_including(email: user.email, event_name: 'added-app-' + app.nid, metadata: {type: 'single', app_list: app.nid})) | ||
subject.info('app_add', user.id, 'App Added', user.email, app_instance) | ||
end | ||
|
||
end | ||
context 'when the user does not exist in intercom' do | ||
before do | ||
users = double('users') | ||
|
||
client = double('client', users: users, events: events) | ||
expect(Intercom::Client).to receive(:new).with(app_id: MnoEnterprise.intercom_app_id, api_key: MnoEnterprise.intercom_api_key).and_return(client) | ||
|
||
expect(users).to receive(:find).and_raise(Intercom::ResourceNotFound.new('not found')) | ||
user_data = { | ||
user_id: user.id, | ||
name: [user.name, user.surname].join(' '), | ||
email: user.email, | ||
created_at: user.created_at.to_i, | ||
last_seen_ip: user.last_sign_in_ip, | ||
custom_attributes: {phone: user.phone}, | ||
companies:[ | ||
{ | ||
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.credit_card?, | ||
app_count: organization.app_instances.count, | ||
app_list: organization.app_instances.map { |app| app.name }.to_sentence | ||
} | ||
} | ||
|
||
] | ||
} | ||
expect(users).to receive(:create).with(user_data) | ||
end | ||
it 'add an event when an password is changed' do | ||
expect(events).to receive(:create).with(hash_including(email: user.email, event_name: 'user-update-password')) | ||
subject.info('user_update_password', user.id, 'User password change', user.email, user) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters