chore: Auto resolution job for captain (#9898)

- Add a conversation auto-resolution job for the captain integration
This commit is contained in:
Sojan Jose
2024-08-06 16:15:11 -07:00
committed by GitHub
parent 91b713f6f5
commit cb4ad28a13
4 changed files with 58 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
class ResponseBot::InboxPendingConversationsResolutionJob < ApplicationJob
class Captain::InboxPendingConversationsResolutionJob < ApplicationJob
queue_as :low
def perform(inbox)

View File

@@ -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

View File

@@ -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) }

View File

@@ -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