From cf5f6d5a74625bcd4a1dc4b8d430552eb092d6ae Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Fri, 12 Nov 2021 17:53:03 +0530 Subject: [PATCH] feat: enable Dialogflow integration all inboxes except email (#3285) Fixes #2461 Co-authored-by: Sojan Jose --- .../dashboard/settings/integrationapps/NewHook.vue | 3 ++- app/javascript/dashboard/store/modules/inboxes.js | 5 +++++ .../store/modules/specs/inboxes/getters.spec.js | 5 +++++ app/listeners/hook_listener.rb | 14 +++++++++++--- spec/factories/integrations/hooks.rb | 1 - spec/jobs/hook_job_spec.rb | 3 ++- .../dialogflow/processor_service_spec.rb | 3 ++- spec/models/conversation_spec.rb | 3 ++- 8 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/javascript/dashboard/routes/dashboard/settings/integrationapps/NewHook.vue b/app/javascript/dashboard/routes/dashboard/settings/integrationapps/NewHook.vue index 3d343ab14..acfb17818 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/integrationapps/NewHook.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/integrationapps/NewHook.vue @@ -59,9 +59,10 @@ export default { ...mapGetters({ uiFlags: 'integrations/getUIFlags', websiteInboxes: 'inboxes/getWebsiteInboxes', + dialogFlowEnabledInboxes: 'inboxes/dialogFlowEnabledInboxes', }), inboxes() { - return this.websiteInboxes + return this.dialogFlowEnabledInboxes .filter(inbox => { if (!this.isIntegrationDialogflow) { return true; diff --git a/app/javascript/dashboard/store/modules/inboxes.js b/app/javascript/dashboard/store/modules/inboxes.js index b02d35f3f..2bb554ba3 100644 --- a/app/javascript/dashboard/store/modules/inboxes.js +++ b/app/javascript/dashboard/store/modules/inboxes.js @@ -78,6 +78,11 @@ export const getters = { item => item.channel_type === INBOX_TYPES.TWILIO && item.medium === 'sms' ); }, + dialogFlowEnabledInboxes($state) { + return $state.records.filter( + item => item.channel_type !== INBOX_TYPES.EMAIL + ); + }, }; export const actions = { diff --git a/app/javascript/dashboard/store/modules/specs/inboxes/getters.spec.js b/app/javascript/dashboard/store/modules/specs/inboxes/getters.spec.js index f49d68e89..73d6624e0 100644 --- a/app/javascript/dashboard/store/modules/specs/inboxes/getters.spec.js +++ b/app/javascript/dashboard/store/modules/specs/inboxes/getters.spec.js @@ -24,6 +24,11 @@ describe('#getters', () => { expect(getters.getTwilioSMSInboxes(state).length).toEqual(1); }); + it('dialogFlowEnabledInboxes', () => { + const state = { records: inboxList }; + expect(getters.dialogFlowEnabledInboxes(state).length).toEqual(5); + }); + it('getInbox', () => { const state = { records: inboxList, diff --git a/app/listeners/hook_listener.rb b/app/listeners/hook_listener.rb index 92099d21a..2a6286bb1 100644 --- a/app/listeners/hook_listener.rb +++ b/app/listeners/hook_listener.rb @@ -3,16 +3,24 @@ class HookListener < BaseListener message = extract_message_and_account(event)[0] return unless message.webhook_sendable? - message.account.hooks.each do |hook| - HookJob.perform_later(hook, event.name, message: message) - end + execute_hooks(event, message) end def message_updated(event) message = extract_message_and_account(event)[0] return unless message.webhook_sendable? + execute_hooks(event, message) + end + + private + + def execute_hooks(event, message) message.account.hooks.each do |hook| + # In case of dialogflow, we would have a hook for each inbox. + # Which means we will execute the same hook multiple times if the below filter isn't there + next if hook.inbox.present? && hook.inbox != message.inbox + HookJob.perform_later(hook, event.name, message: message) end end diff --git a/spec/factories/integrations/hooks.rb b/spec/factories/integrations/hooks.rb index 598d68c11..2d635015a 100644 --- a/spec/factories/integrations/hooks.rb +++ b/spec/factories/integrations/hooks.rb @@ -1,7 +1,6 @@ FactoryBot.define do factory :integrations_hook, class: 'Integrations::Hook' do app_id { 'slack' } - inbox account settings { { test: 'test' } } status { Integrations::Hook.statuses['enabled'] } diff --git a/spec/jobs/hook_job_spec.rb b/spec/jobs/hook_job_spec.rb index 225a9c183..614789e84 100644 --- a/spec/jobs/hook_job_spec.rb +++ b/spec/jobs/hook_job_spec.rb @@ -5,6 +5,7 @@ RSpec.describe HookJob, type: :job do let(:account) { create(:account) } let(:hook) { create(:integrations_hook, account: account) } + let(:inbox) { create(:inbox, account: account) } let(:event_name) { 'message.created' } let(:event_data) { { message: create(:message, account: account) } } @@ -29,7 +30,7 @@ RSpec.describe HookJob, type: :job do end it 'calls Integrations::Dialogflow::ProcessorService when its a dialogflow intergation' do - hook = create(:integrations_hook, :dialogflow, account: account) + hook = create(:integrations_hook, :dialogflow, inbox: inbox, account: account) allow(Integrations::Dialogflow::ProcessorService).to receive(:new).and_return(process_service) expect(Integrations::Dialogflow::ProcessorService).to receive(:new) described_class.perform_now(hook, event_name, event_data) diff --git a/spec/lib/integrations/dialogflow/processor_service_spec.rb b/spec/lib/integrations/dialogflow/processor_service_spec.rb index dc2208cd0..3971482d9 100644 --- a/spec/lib/integrations/dialogflow/processor_service_spec.rb +++ b/spec/lib/integrations/dialogflow/processor_service_spec.rb @@ -2,7 +2,8 @@ require 'rails_helper' describe Integrations::Dialogflow::ProcessorService do let(:account) { create(:account) } - let(:hook) { create(:integrations_hook, :dialogflow, account: account) } + let(:inbox) { create(:inbox, account: account) } + let(:hook) { create(:integrations_hook, :dialogflow, inbox: inbox, account: account) } let(:conversation) { create(:conversation, account: account, status: :pending) } let(:message) { create(:message, account: account, conversation: conversation) } let(:event_name) { 'message.created' } diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 5b84397db..9d7fa2c8c 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -407,7 +407,8 @@ RSpec.describe Conversation, type: :model do end describe '#botintegration: when conversation created in inbox with dialogflow integration' do - let(:hook) { create(:integrations_hook, :dialogflow) } + let(:inbox) { create(:inbox) } + let(:hook) { create(:integrations_hook, :dialogflow, inbox: inbox) } let(:conversation) { create(:conversation, inbox: hook.inbox) } it 'returns conversation status as pending' do