Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNOE-328] Add external_id and source tagging to Intercom #248

Merged
merged 1 commit into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/lib/mno_enterprise/intercom_events_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def update_intercom_user(user, update_last_request_at = true)
update_last_request_at: update_last_request_at
}
data[:custom_attributes][:phone]= user.phone if user.phone
data[:custom_attributes][:external_id]= user.external_id if user.external_id

data[:companies] = user.organizations.map do |organization|
{
Expand All @@ -81,6 +82,14 @@ def update_intercom_user(user, update_last_request_at = true)
}
end
intercom.users.create(data)
tag_user(user)
end

# If a source is set, tag the user with it
def tag_user(user)
if user.meta_data && user.meta_data[:source].present?
intercom.tags.tag(name: user.meta_data[:source], users: [{user_id: user.id}])
end
end
end
end
Expand Down
106 changes: 64 additions & 42 deletions api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,45 @@ module MnoEnterprise
api_stub_for(get: "/organizations/#{organization.id}/app_instances", response: from_api([app_instance]))
end

# Stub Intercom client
let(:tags) { double('tags') }
let(:events) { double('events') }
let(:users) { double('users') }
let(:client) { double('client', users: users, events: events, tags: tags) }
before do
expect(Intercom::Client).to receive(:new).with(app_id: MnoEnterprise.intercom_app_id, api_key: MnoEnterprise.intercom_api_key).and_return(client)
end

let(:expected_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},
update_last_request_at: true,
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
}
}
]
}
}

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 }
before { allow(users).to receive(:find) }
subject { described_class.new }

it 'add an event when an password is changed' do
expect(events).to receive(:create).with(hash_including(email: user.email, user_id: user.id, event_name: 'user-update-password'))
Expand All @@ -45,44 +70,41 @@ module MnoEnterprise
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},
update_last_request_at: true,
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)
allow(users).to receive(:find).and_raise(Intercom::ResourceNotFound.new('not found'))
expect(users).to receive(:create).with(expected_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.class.name, user.id, user.email)
end
end
end

describe '#update_intercom_user' do
subject { described_class.new.update_intercom_user(user) }
before { allow(users).to receive(:create) }

it 'updates the user' do
expect(users).to receive(:create).with(expected_user_data)
subject
end

context 'when the user has an external_id' do
before { user.external_id = '132456' }
it 'updates the user' do
expect(users).to receive(:create).with(hash_including(custom_attributes: { phone: user.phone, external_id: '132456' }))
subject
end
end

context 'when the user has a source' do
before { user.meta_data = {source: 'acme'}}
it 'tags the user' do
expect(tags).to receive(:tag).with(name: 'acme', users: [{user_id: user.id}])
subject
end
end
end
end
end