From cb4ad28a131949d8597d302da682f2cc3a5f0c72 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 6 Aug 2024 16:15:11 -0700 Subject: [PATCH] chore: Auto resolution job for captain (#9898) - Add a conversation auto-resolution job for the captain integration --- ...ox_pending_conversations_resolution_job.rb | 2 +- .../conversations_resolution_scheduler_job.rb | 25 +++++++++++- ...nding_conversations_resolution_job_spec.rb | 2 +- ...ersations_resolution_scheduler_job_spec.rb | 40 +++++++++++++++---- 4 files changed, 58 insertions(+), 11 deletions(-) rename enterprise/app/jobs/{response_bot => captain}/inbox_pending_conversations_resolution_job.rb (90%) rename spec/enterprise/jobs/{response_bot => captain}/inbox_pending_conversations_resolution_job_spec.rb (95%) diff --git a/enterprise/app/jobs/response_bot/inbox_pending_conversations_resolution_job.rb b/enterprise/app/jobs/captain/inbox_pending_conversations_resolution_job.rb similarity index 90% rename from enterprise/app/jobs/response_bot/inbox_pending_conversations_resolution_job.rb rename to enterprise/app/jobs/captain/inbox_pending_conversations_resolution_job.rb index f6333d446..39c08af64 100644 --- a/enterprise/app/jobs/response_bot/inbox_pending_conversations_resolution_job.rb +++ b/enterprise/app/jobs/captain/inbox_pending_conversations_resolution_job.rb @@ -1,4 +1,4 @@ -class ResponseBot::InboxPendingConversationsResolutionJob < ApplicationJob +class Captain::InboxPendingConversationsResolutionJob < ApplicationJob queue_as :low def perform(inbox) diff --git a/enterprise/app/jobs/enterprise/account/conversations_resolution_scheduler_job.rb b/enterprise/app/jobs/enterprise/account/conversations_resolution_scheduler_job.rb index 4f80424f8..ae3eb3ba0 100644 --- a/enterprise/app/jobs/enterprise/account/conversations_resolution_scheduler_job.rb +++ b/enterprise/app/jobs/enterprise/account/conversations_resolution_scheduler_job.rb @@ -1,9 +1,32 @@ module Enterprise::Account::ConversationsResolutionSchedulerJob def perform super + + # TODO: remove this when response bot is remove in favor of captain + resolve_response_bot_conversations + + # This is responsible for resolving captain conversations + resolve_captain_conversations + end + + private + + def resolve_response_bot_conversations + # This is responsible for resolving response bot conversations Account.feature_response_bot.all.find_each(batch_size: 100) do |account| account.inboxes.each do |inbox| - ResponseBot::InboxPendingConversationsResolutionJob.perform_later(inbox) if inbox.response_bot_enabled? + Captain::InboxPendingConversationsResolutionJob.perform_later(inbox) if inbox.response_bot_enabled? + end + end + end + + def resolve_captain_conversations + Integrations::Hook.where(app_id: 'captain').all.find_each(batch_size: 100) do |hook| + next unless hook.enabled? + + inboxes = Inbox.where(id: hook.settings['inbox_ids'].split(',')) + inboxes.each do |inbox| + Captain::InboxPendingConversationsResolutionJob.perform_later(inbox) end end end diff --git a/spec/enterprise/jobs/response_bot/inbox_pending_conversations_resolution_job_spec.rb b/spec/enterprise/jobs/captain/inbox_pending_conversations_resolution_job_spec.rb similarity index 95% rename from spec/enterprise/jobs/response_bot/inbox_pending_conversations_resolution_job_spec.rb rename to spec/enterprise/jobs/captain/inbox_pending_conversations_resolution_job_spec.rb index 268d758cd..90e9360d3 100644 --- a/spec/enterprise/jobs/response_bot/inbox_pending_conversations_resolution_job_spec.rb +++ b/spec/enterprise/jobs/captain/inbox_pending_conversations_resolution_job_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe ResponseBot::InboxPendingConversationsResolutionJob, type: :job do +RSpec.describe Captain::InboxPendingConversationsResolutionJob, type: :job do include ActiveJob::TestHelper let!(:inbox) { create(:inbox) } diff --git a/spec/enterprise/jobs/enterprise/account/conversations_resolution_scheduler_job_spec.rb b/spec/enterprise/jobs/enterprise/account/conversations_resolution_scheduler_job_spec.rb index 642752c5b..187649591 100644 --- a/spec/enterprise/jobs/enterprise/account/conversations_resolution_scheduler_job_spec.rb +++ b/spec/enterprise/jobs/enterprise/account/conversations_resolution_scheduler_job_spec.rb @@ -7,23 +7,47 @@ RSpec.describe Account::ConversationsResolutionSchedulerJob, type: :job do let!(:inbox_without_bot) { create(:inbox, account: account_without_bot) } let(:response_source) { create(:response_source, account: account_with_bot) } - before do - skip_unless_response_bot_enabled_test_environment - account_with_bot.enable_features!(:response_bot) - create(:inbox_response_source, inbox: inbox_with_bot, response_source: response_source) - end + describe '#perform - response bot resolutions' do + before do + skip_unless_response_bot_enabled_test_environment + account_with_bot.enable_features!(:response_bot) + create(:inbox_response_source, inbox: inbox_with_bot, response_source: response_source) + end - describe '#perform' do it 'enqueues resolution jobs only for inboxes with response bot enabled' do expect do described_class.perform_now - end.to have_enqueued_job(ResponseBot::InboxPendingConversationsResolutionJob).with(inbox_with_bot).and have_enqueued_job.exactly(:once) + end.to have_enqueued_job(Captain::InboxPendingConversationsResolutionJob).with(inbox_with_bot).and have_enqueued_job.exactly(:once) end it 'does not enqueue resolution jobs for inboxes without response bot enabled' do expect do described_class.perform_now - end.not_to have_enqueued_job(ResponseBot::InboxPendingConversationsResolutionJob).with(inbox_without_bot) + end.not_to have_enqueued_job(Captain::InboxPendingConversationsResolutionJob).with(inbox_without_bot) + end + end + + describe '#perform - captain resolutions' do + before do + create(:integrations_hook, app_id: 'captain', account: account_with_bot, settings: { + inbox_ids: inbox_with_bot.id.to_s, + access_token: SecureRandom.hex, + account_id: Faker::Alphanumeric.alpha(number: 10), + account_email: Faker::Internet.email, + assistant_id: Faker::Alphanumeric.alpha(number: 10) + }) + end + + it 'enqueues resolution jobs only for inboxes with captain enabled' do + expect do + described_class.perform_now + end.to have_enqueued_job(Captain::InboxPendingConversationsResolutionJob).with(inbox_with_bot).and have_enqueued_job.exactly(:once) + end + + it 'does not enqueue resolution jobs for inboxes without captain enabled' do + expect do + described_class.perform_now + end.not_to have_enqueued_job(Captain::InboxPendingConversationsResolutionJob).with(inbox_without_bot) end end end