feat: Add inbox webhook events (#8006)
- Add webhook events for inbox creation/updation. - Right now, the feature is added under a feature_flag. It is not available by default on all installations.
This commit is contained in:
@@ -24,6 +24,11 @@ class BaseListener
|
|||||||
[contact, contact.account]
|
[contact, contact.account]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def extract_inbox_and_account(event)
|
||||||
|
inbox = event.data[:inbox]
|
||||||
|
[inbox, inbox.account]
|
||||||
|
end
|
||||||
|
|
||||||
def extract_changed_attributes(event)
|
def extract_changed_attributes(event)
|
||||||
changed_attributes = event.data[:changed_attributes]
|
changed_attributes = event.data[:changed_attributes]
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,23 @@ class WebhookListener < BaseListener
|
|||||||
deliver_account_webhooks(payload, account)
|
deliver_account_webhooks(payload, account)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inbox_created(event)
|
||||||
|
inbox, account = extract_inbox_and_account(event)
|
||||||
|
inbox_webhook_data = Inbox::EventDataPresenter.new(inbox).push_data
|
||||||
|
payload = inbox_webhook_data.merge(event: __method__.to_s)
|
||||||
|
deliver_account_webhooks(payload, account)
|
||||||
|
end
|
||||||
|
|
||||||
|
def inbox_updated(event)
|
||||||
|
inbox, account = extract_inbox_and_account(event)
|
||||||
|
changed_attributes = extract_changed_attributes(event)
|
||||||
|
return if changed_attributes.blank?
|
||||||
|
|
||||||
|
inbox_webhook_data = Inbox::EventDataPresenter.new(inbox).push_data
|
||||||
|
payload = inbox_webhook_data.merge(event: __method__.to_s, changed_attributes: changed_attributes)
|
||||||
|
deliver_account_webhooks(payload, account)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def deliver_account_webhooks(payload, account)
|
def deliver_account_webhooks(payload, account)
|
||||||
|
|||||||
@@ -77,6 +77,9 @@ class Inbox < ApplicationRecord
|
|||||||
|
|
||||||
after_destroy :delete_round_robin_agents
|
after_destroy :delete_round_robin_agents
|
||||||
|
|
||||||
|
after_create_commit :dispatch_create_event
|
||||||
|
after_update_commit :dispatch_update_event
|
||||||
|
|
||||||
scope :order_by_name, -> { order('lower(name) ASC') }
|
scope :order_by_name, -> { order('lower(name) ASC') }
|
||||||
|
|
||||||
def add_member(user_id)
|
def add_member(user_id)
|
||||||
@@ -159,6 +162,18 @@ class Inbox < ApplicationRecord
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def dispatch_create_event
|
||||||
|
return if ENV['ENABLE_INBOX_EVENTS'].blank?
|
||||||
|
|
||||||
|
Rails.configuration.dispatcher.dispatch(INBOX_CREATED, Time.zone.now, inbox: self)
|
||||||
|
end
|
||||||
|
|
||||||
|
def dispatch_update_event
|
||||||
|
return if ENV['ENABLE_INBOX_EVENTS'].blank?
|
||||||
|
|
||||||
|
Rails.configuration.dispatcher.dispatch(INBOX_UPDATED, Time.zone.now, inbox: self, changed_attributes: previous_changes)
|
||||||
|
end
|
||||||
|
|
||||||
def ensure_valid_max_assignment_limit
|
def ensure_valid_max_assignment_limit
|
||||||
# overridden in enterprise/app/models/enterprise/inbox.rb
|
# overridden in enterprise/app/models/enterprise/inbox.rb
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class Webhook < ApplicationRecord
|
|||||||
enum webhook_type: { account_type: 0, inbox_type: 1 }
|
enum webhook_type: { account_type: 0, inbox_type: 1 }
|
||||||
|
|
||||||
ALLOWED_WEBHOOK_EVENTS = %w[conversation_status_changed conversation_updated conversation_created contact_created contact_updated
|
ALLOWED_WEBHOOK_EVENTS = %w[conversation_status_changed conversation_updated conversation_created contact_created contact_updated
|
||||||
message_created message_updated webwidget_triggered].freeze
|
message_created message_updated webwidget_triggered inbox_created inbox_updated].freeze
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|||||||
35
app/presenters/inbox/event_data_presenter.rb
Normal file
35
app/presenters/inbox/event_data_presenter.rb
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
class Inbox::EventDataPresenter < SimpleDelegator
|
||||||
|
def push_data
|
||||||
|
{
|
||||||
|
# Conversation thread config
|
||||||
|
allow_messages_after_resolved: allow_messages_after_resolved,
|
||||||
|
lock_to_single_conversation: lock_to_single_conversation,
|
||||||
|
|
||||||
|
# Auto Assignment config
|
||||||
|
auto_assignment_config: auto_assignment_config,
|
||||||
|
enable_auto_assignment: enable_auto_assignment,
|
||||||
|
|
||||||
|
# Feature flag for message events
|
||||||
|
enable_email_collect: enable_email_collect,
|
||||||
|
greeting_enabled: greeting_enabled,
|
||||||
|
greeting_message: greeting_message,
|
||||||
|
csat_survey_enabled: csat_survey_enabled,
|
||||||
|
|
||||||
|
# Outbound email sender config
|
||||||
|
business_name: business_name,
|
||||||
|
sender_name_type: sender_name_type,
|
||||||
|
|
||||||
|
# Business hour config
|
||||||
|
timezone: timezone,
|
||||||
|
out_of_office_message: out_of_office_message,
|
||||||
|
working_hours_enabled: working_hours_enabled,
|
||||||
|
working_hours: working_hours,
|
||||||
|
|
||||||
|
created_at: created_at,
|
||||||
|
updated_at: updated_at,
|
||||||
|
|
||||||
|
# Associated channel attributes
|
||||||
|
channel: channel
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -42,6 +42,10 @@ module Events::Types
|
|||||||
CONTACT_MERGED = 'contact.merged'
|
CONTACT_MERGED = 'contact.merged'
|
||||||
CONTACT_DELETED = 'contact.deleted'
|
CONTACT_DELETED = 'contact.deleted'
|
||||||
|
|
||||||
|
# contact events
|
||||||
|
INBOX_CREATED = 'inbox.created'
|
||||||
|
INBOX_UPDATED = 'inbox.updated'
|
||||||
|
|
||||||
# notification events
|
# notification events
|
||||||
NOTIFICATION_CREATED = 'notification.created'
|
NOTIFICATION_CREATED = 'notification.created'
|
||||||
|
|
||||||
|
|||||||
@@ -217,4 +217,64 @@ describe WebhookListener do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#inbox_created' do
|
||||||
|
let(:event_name) { :'inbox.created' }
|
||||||
|
let!(:inbox_created_event) { Events::Base.new(event_name, Time.zone.now, inbox: inbox) }
|
||||||
|
|
||||||
|
context 'when webhook is not configured' do
|
||||||
|
it 'does not trigger webhook' do
|
||||||
|
expect(WebhookJob).to receive(:perform_later).exactly(0).times
|
||||||
|
listener.inbox_created(inbox_created_event)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when webhook is configured' do
|
||||||
|
it 'triggers webhook' do
|
||||||
|
inbox_data = Inbox::EventDataPresenter.new(inbox).push_data
|
||||||
|
webhook = create(:webhook, account: account, subscriptions: ['inbox_created'])
|
||||||
|
expect(WebhookJob).to receive(:perform_later).with(webhook.url, inbox_data.merge(event: 'inbox_created')).once
|
||||||
|
listener.inbox_created(inbox_created_event)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#inbox_updated' do
|
||||||
|
let(:event_name) { :'inbox.updated' }
|
||||||
|
let!(:inbox_updated_event) { Events::Base.new(event_name, Time.zone.now, inbox: inbox, changed_attributes: changed_attributes) }
|
||||||
|
let(:changed_attributes) { {} }
|
||||||
|
|
||||||
|
context 'when webhook is not configured' do
|
||||||
|
it 'does not trigger webhook' do
|
||||||
|
expect(WebhookJob).to receive(:perform_later).exactly(0).times
|
||||||
|
listener.inbox_updated(inbox_updated_event)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when webhook is configured and there are no changed attributes' do
|
||||||
|
it 'triggers webhook' do
|
||||||
|
create(:webhook, account: account, subscriptions: ['inbox_updated'])
|
||||||
|
expect(WebhookJob).to receive(:perform_later).exactly(0).times
|
||||||
|
listener.inbox_updated(inbox_updated_event)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when webhook is configured' do
|
||||||
|
let(:changed_attributes) { { 'name' => ['Inbox 1', inbox.name] } }
|
||||||
|
|
||||||
|
it 'triggers webhook' do
|
||||||
|
webhook = create(:webhook, account: account, subscriptions: ['inbox_updated'])
|
||||||
|
|
||||||
|
inbox_data = Inbox::EventDataPresenter.new(inbox).push_data
|
||||||
|
changed_attributes_data = [{ 'name' => { 'previous_value': 'Inbox 1', 'current_value': inbox.name } }]
|
||||||
|
|
||||||
|
expect(WebhookJob).to receive(:perform_later).with(
|
||||||
|
webhook.url,
|
||||||
|
inbox_data.merge(event: 'inbox_updated', changed_attributes: changed_attributes_data)
|
||||||
|
).once
|
||||||
|
|
||||||
|
listener.inbox_updated(inbox_updated_event)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -210,6 +210,34 @@ RSpec.describe Inbox do
|
|||||||
expect(inbox.portal).to eq(portal)
|
expect(inbox.portal).to eq(portal)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'sends the inbox_created event if ENABLE_INBOX_EVENTS is true' do
|
||||||
|
with_modified_env ENABLE_INBOX_EVENTS: 'true' do
|
||||||
|
channel = inbox.channel
|
||||||
|
channel.update(widget_color: '#fff')
|
||||||
|
|
||||||
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
|
||||||
|
.with(
|
||||||
|
'inbox.updated',
|
||||||
|
kind_of(Time),
|
||||||
|
inbox: inbox,
|
||||||
|
changed_attributes: kind_of(Object)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sends the inbox_created event if ENABLE_INBOX_EVENTS is false' do
|
||||||
|
channel = inbox.channel
|
||||||
|
channel.update(widget_color: '#fff')
|
||||||
|
|
||||||
|
expect(Rails.configuration.dispatcher).not_to have_received(:dispatch)
|
||||||
|
.with(
|
||||||
|
'inbox.updated',
|
||||||
|
kind_of(Time),
|
||||||
|
inbox: inbox,
|
||||||
|
changed_attributes: kind_of(Object)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
it 'resets cache key if there is an update in the channel' do
|
it 'resets cache key if there is an update in the channel' do
|
||||||
channel = inbox.channel
|
channel = inbox.channel
|
||||||
channel.update(widget_color: '#fff')
|
channel.update(widget_color: '#fff')
|
||||||
|
|||||||
Reference in New Issue
Block a user