From 12a64f1b104b3be22513214d2ecc8f646d96b844 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Thu, 28 Sep 2023 15:26:28 -0700 Subject: [PATCH 1/2] chore: Add an API to find the contacts using contact inbox sourceId (#8012) Fixes: https://linear.app/chatwoot/issue/CW-2578/search-by-facebook-id --- .../v1/accounts/contact_inboxes_controller.rb | 21 ++++++ .../contact_inboxes/filter.json.jbuilder | 1 + config/routes.rb | 5 ++ .../contact_inboxes_controller_spec.rb | 71 +++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 app/controllers/api/v1/accounts/contact_inboxes_controller.rb create mode 100644 app/views/api/v1/accounts/contact_inboxes/filter.json.jbuilder create mode 100644 spec/controllers/api/v1/accounts/contact_inboxes_controller_spec.rb diff --git a/app/controllers/api/v1/accounts/contact_inboxes_controller.rb b/app/controllers/api/v1/accounts/contact_inboxes_controller.rb new file mode 100644 index 000000000..ed7066895 --- /dev/null +++ b/app/controllers/api/v1/accounts/contact_inboxes_controller.rb @@ -0,0 +1,21 @@ +class Api::V1::Accounts::ContactInboxesController < Api::V1::Accounts::BaseController + before_action :ensure_inbox + + def filter + contact_inbox = @inbox.contact_inboxes.where(inbox_id: permitted_params[:inbox_id], source_id: permitted_params[:source_id]) + return head :not_found if contact_inbox.empty? + + @contact = contact_inbox.first.contact + end + + private + + def ensure_inbox + @inbox = Current.account.inboxes.find(permitted_params[:inbox_id]) + authorize @inbox, :show? + end + + def permitted_params + params.permit(:inbox_id, :source_id) + end +end diff --git a/app/views/api/v1/accounts/contact_inboxes/filter.json.jbuilder b/app/views/api/v1/accounts/contact_inboxes/filter.json.jbuilder new file mode 100644 index 000000000..9b6c30d59 --- /dev/null +++ b/app/views/api/v1/accounts/contact_inboxes/filter.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'api/v1/models/contact', formats: [:json], resource: @contact, with_contact_inboxes: true diff --git a/config/routes.rb b/config/routes.rb index 7a3479439..ff3cbdd46 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -46,6 +46,11 @@ Rails.application.routes.draw do resource :bulk_actions, only: [:create] resources :agents, only: [:index, :create, :update, :destroy] resources :agent_bots, only: [:index, :create, :show, :update, :destroy] + resources :contact_inboxes, only: [] do + collection do + post :filter + end + end resources :assignable_agents, only: [:index] resource :audit_logs, only: [:show] resources :callbacks, only: [] do diff --git a/spec/controllers/api/v1/accounts/contact_inboxes_controller_spec.rb b/spec/controllers/api/v1/accounts/contact_inboxes_controller_spec.rb new file mode 100644 index 000000000..86deab707 --- /dev/null +++ b/spec/controllers/api/v1/accounts/contact_inboxes_controller_spec.rb @@ -0,0 +1,71 @@ +require 'rails_helper' + +RSpec.describe 'Contact Inboxes API', type: :request do + let(:account) { create(:account) } + + let(:inbox) { create(:inbox, account: account) } + let(:contact) { create(:contact, account: account) } + let!(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) } + + describe 'POST /api/v1/accounts/{account.id}/contact_inboxes/filter' do + let(:admin) { create(:user, account: account, role: :administrator) } + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + post "/api/v1/accounts/#{account.id}/contact_inboxes/filter" + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated admin user' do + it 'returns not found if the params are invalid' do + post "/api/v1/accounts/#{account.id}/contact_inboxes/filter", + headers: admin.create_new_auth_token, + params: { inbox_id: inbox.id, source_id: 'random_source_id' }, + as: :json + + expect(response).to have_http_status(:not_found) + end + + it 'returns the contact if the params are valid' do + post "/api/v1/accounts/#{account.id}/contact_inboxes/filter", + headers: admin.create_new_auth_token, + params: { inbox_id: inbox.id, source_id: contact_inbox.source_id }, + as: :json + + expect(response).to have_http_status(:success) + response_body = response.parsed_body + expect(response_body['id']).to eq(contact.id) + expect(response_body['contact_inboxes'].first['source_id']).to eq(contact_inbox.source_id) + end + end + + context 'when it is an authenticated agent user' do + let(:agent_with_inbox_access) { create(:user, account: account, role: :agent) } + let(:agent_without_inbox_access) { create(:user, account: account, role: :agent) } + + before do + create(:inbox_member, user: agent_with_inbox_access, inbox: inbox) + end + + it 'returns unauthorized if agent does not have inbox access' do + post "/api/v1/accounts/#{account.id}/contact_inboxes/filter", + headers: agent_without_inbox_access.create_new_auth_token, + params: { inbox_id: inbox.id, source_id: contact_inbox.source_id }, + as: :json + + expect(response).to have_http_status(:unauthorized) + end + + it 'returns success if agent have inbox access' do + post "/api/v1/accounts/#{account.id}/contact_inboxes/filter", + headers: agent_with_inbox_access.create_new_auth_token, + params: { inbox_id: inbox.id, source_id: contact_inbox.source_id }, + as: :json + + expect(response).to have_http_status(:success) + end + end + end +end From fd633e16131755e52db34ead382dd884b09c5d56 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Thu, 28 Sep 2023 15:28:10 -0700 Subject: [PATCH 2/2] 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. --- app/listeners/base_listener.rb | 5 ++ app/listeners/webhook_listener.rb | 17 ++++++ app/models/inbox.rb | 15 +++++ app/models/webhook.rb | 2 +- app/presenters/inbox/event_data_presenter.rb | 35 ++++++++++++ lib/events/types.rb | 4 ++ spec/listeners/webhook_listener_spec.rb | 60 ++++++++++++++++++++ spec/models/inbox_spec.rb | 28 +++++++++ 8 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 app/presenters/inbox/event_data_presenter.rb diff --git a/app/listeners/base_listener.rb b/app/listeners/base_listener.rb index ffd57d2a3..acfbc4b18 100644 --- a/app/listeners/base_listener.rb +++ b/app/listeners/base_listener.rb @@ -24,6 +24,11 @@ class BaseListener [contact, contact.account] end + def extract_inbox_and_account(event) + inbox = event.data[:inbox] + [inbox, inbox.account] + end + def extract_changed_attributes(event) changed_attributes = event.data[:changed_attributes] diff --git a/app/listeners/webhook_listener.rb b/app/listeners/webhook_listener.rb index d8f4c88fb..45c04629f 100644 --- a/app/listeners/webhook_listener.rb +++ b/app/listeners/webhook_listener.rb @@ -66,6 +66,23 @@ class WebhookListener < BaseListener deliver_account_webhooks(payload, account) 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 def deliver_account_webhooks(payload, account) diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 890e8b151..5a13b114f 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -77,6 +77,9 @@ class Inbox < ApplicationRecord 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') } def add_member(user_id) @@ -159,6 +162,18 @@ class Inbox < ApplicationRecord 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 # overridden in enterprise/app/models/enterprise/inbox.rb end diff --git a/app/models/webhook.rb b/app/models/webhook.rb index 4f0e32a43..540dbd9b7 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -26,7 +26,7 @@ class Webhook < ApplicationRecord enum webhook_type: { account_type: 0, inbox_type: 1 } 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 diff --git a/app/presenters/inbox/event_data_presenter.rb b/app/presenters/inbox/event_data_presenter.rb new file mode 100644 index 000000000..cbff8894c --- /dev/null +++ b/app/presenters/inbox/event_data_presenter.rb @@ -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 diff --git a/lib/events/types.rb b/lib/events/types.rb index 40c88fa18..2693f5216 100644 --- a/lib/events/types.rb +++ b/lib/events/types.rb @@ -42,6 +42,10 @@ module Events::Types CONTACT_MERGED = 'contact.merged' CONTACT_DELETED = 'contact.deleted' + # contact events + INBOX_CREATED = 'inbox.created' + INBOX_UPDATED = 'inbox.updated' + # notification events NOTIFICATION_CREATED = 'notification.created' diff --git a/spec/listeners/webhook_listener_spec.rb b/spec/listeners/webhook_listener_spec.rb index f1a39d932..332cde731 100644 --- a/spec/listeners/webhook_listener_spec.rb +++ b/spec/listeners/webhook_listener_spec.rb @@ -217,4 +217,64 @@ describe WebhookListener do 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 diff --git a/spec/models/inbox_spec.rb b/spec/models/inbox_spec.rb index 02122709c..30c6ef235 100644 --- a/spec/models/inbox_spec.rb +++ b/spec/models/inbox_spec.rb @@ -210,6 +210,34 @@ RSpec.describe Inbox do expect(inbox.portal).to eq(portal) 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 channel = inbox.channel channel.update(widget_color: '#fff')