feat: APIs to assign agents_bots as assignee in conversations (#12836)

## Summary
- add an assignee_agent_bot_id column as an initital step to prototype
this before fully switching to polymorphic assignee
- update assignment APIs and conversation list / show endpoints to
reflect assignee as agent bot
- ensure webhook payloads contains agent bot assignee


[Codex
Task](https://chatgpt.com/codex/tasks/task_e_6912833377e48326b6641b9eee32d50f)

---------

Co-authored-by: Pranav <pranav@chatwoot.com>
This commit is contained in:
Sojan Jose
2025-11-18 18:20:58 -08:00
committed by GitHub
co-authored by Pranav
parent 70c183ea6e
commit 5f2b2f4221
23 changed files with 316 additions and 52 deletions
@@ -15,7 +15,7 @@ RSpec.describe 'Conversation Assignment API', type: :request do
end
context 'when it is an authenticated bot with out access to the inbox' do
let(:agent_bot) { create(:agent_bot, account: account) }
let(:agent_bot) { create(:agent_bot) }
let(:agent) { create(:user, account: account, role: :agent) }
before do
@@ -36,6 +36,7 @@ RSpec.describe 'Conversation Assignment API', type: :request do
context 'when it is an authenticated user with access to the inbox' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:agent_bot) { create(:agent_bot, account: account) }
let(:team) { create(:team, account: account) }
before do
@@ -54,6 +55,25 @@ RSpec.describe 'Conversation Assignment API', type: :request do
expect(conversation.reload.assignee).to eq(agent)
end
it 'assigns an agent bot to the conversation' do
params = { assignee_id: agent_bot.id, assignee_type: 'AgentBot' }
expect(Conversations::AssignmentService).to receive(:new)
.with(hash_including(conversation: conversation, assignee_id: agent_bot.id, assignee_type: 'AgentBot'))
.and_call_original
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
params: params,
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(response.parsed_body['name']).to eq(agent_bot.name)
conversation.reload
expect(conversation.assignee_agent_bot).to eq(agent_bot)
expect(conversation.assignee).to be_nil
end
it 'assigns a team to the conversation' do
team_member = create(:user, account: account, role: :agent, auto_offline: false)
create(:inbox_member, inbox: conversation.inbox, user: team_member)
@@ -125,7 +145,7 @@ RSpec.describe 'Conversation Assignment API', type: :request do
end
it 'unassigns the assignee from the conversation' do
params = { assignee_id: 0 }
params = { assignee_id: nil }
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
params: params,
headers: agent.create_new_auth_token,
+18
View File
@@ -36,6 +36,24 @@ describe AgentBotListener do
expect(AgentBots::WebhookJob).not_to receive(:perform_later)
listener.message_created(event)
end
context 'when conversation has a different assignee agent bot' do
let!(:conversation_bot) { create(:agent_bot) }
before do
create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
conversation.update!(assignee_agent_bot: conversation_bot, assignee: nil)
end
it 'sends message to both bots exactly once' do
payload = message.webhook_data.merge(event: 'message_created')
expect(AgentBots::WebhookJob).to receive(:perform_later).with(agent_bot.outgoing_url, payload).once
expect(AgentBots::WebhookJob).to receive(:perform_later).with(conversation_bot.outgoing_url, payload).once
listener.message_created(event)
end
end
end
end
+3 -2
View File
@@ -525,8 +525,9 @@ RSpec.describe Conversation do
additional_attributes: {},
meta: {
sender: conversation.contact.push_event_data,
assignee: conversation.assignee,
team: conversation.team,
assignee: conversation.assigned_entity&.push_event_data,
assignee_type: conversation.assignee_type,
team: conversation.team&.push_event_data,
hmac_verified: conversation.contact_inbox.hmac_verified
},
id: conversation.display_id,
@@ -12,8 +12,9 @@ RSpec.describe Conversations::EventDataPresenter do
additional_attributes: {},
meta: {
sender: conversation.contact.push_event_data,
assignee: conversation.assignee,
team: conversation.team,
assignee: conversation.assigned_entity&.push_event_data,
assignee_type: conversation.assignee_type,
team: conversation.team&.push_event_data,
hmac_verified: conversation.contact_inbox.hmac_verified
},
id: conversation.display_id,
@@ -0,0 +1,63 @@
require 'rails_helper'
RSpec.describe 'Api::V1::Accounts::BaseController', type: :request do
let(:account) { create(:account) }
let(:inbox) { create(:inbox, account: account) }
let!(:conversation) { create(:conversation, account: account, inbox: inbox) }
let(:agent) { create(:user, account: account, role: :agent) }
before do
create(:inbox_member, inbox: inbox, user: agent)
end
context 'when agent bot belongs to the account' do
let(:agent_bot) { create(:agent_bot, account: account) }
it 'allows assignments via API' do
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
headers: { api_access_token: agent_bot.access_token.token },
params: { assignee_id: agent.id },
as: :json
expect(response).to have_http_status(:success)
end
end
context 'when agent bot belongs to another account' do
let(:other_account) { create(:account) }
let(:external_bot) { create(:agent_bot, account: other_account) }
it 'rejects assignment' do
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
headers: { api_access_token: external_bot.access_token.token },
params: { assignee_id: agent.id },
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when agent bot is global' do
let(:global_bot) { create(:agent_bot, account: nil) }
it 'rejects requests without inbox mapping' do
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
headers: { api_access_token: global_bot.access_token.token },
params: { assignee_id: agent.id },
as: :json
expect(response).to have_http_status(:unauthorized)
end
it 'allows requests when inbox mapping exists' do
create(:agent_bot_inbox, agent_bot: global_bot, inbox: inbox)
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
headers: { api_access_token: global_bot.access_token.token },
params: { assignee_id: agent.id },
as: :json
expect(response).to have_http_status(:success)
end
end
end
@@ -0,0 +1,60 @@
require 'rails_helper'
describe Conversations::AssignmentService do
let(:account) { create(:account) }
let(:agent) { create(:user, account: account) }
let(:agent_bot) { create(:agent_bot, account: account) }
let(:conversation) { create(:conversation, account: account) }
describe '#perform' do
context 'when assignee_id is blank' do
before do
conversation.update!(assignee: agent, assignee_agent_bot: agent_bot)
end
it 'clears both human and bot assignees' do
described_class.new(conversation: conversation, assignee_id: nil).perform
conversation.reload
expect(conversation.assignee_id).to be_nil
expect(conversation.assignee_agent_bot_id).to be_nil
end
end
context 'when assigning a user' do
before do
conversation.update!(assignee_agent_bot: agent_bot, assignee: nil)
end
it 'sets the agent and clears agent bot' do
result = described_class.new(conversation: conversation, assignee_id: agent.id).perform
conversation.reload
expect(result).to eq(agent)
expect(conversation.assignee_id).to eq(agent.id)
expect(conversation.assignee_agent_bot_id).to be_nil
end
end
context 'when assigning an agent bot' do
let(:service) do
described_class.new(
conversation: conversation,
assignee_id: agent_bot.id,
assignee_type: 'AgentBot'
)
end
it 'sets the agent bot and clears human assignee' do
conversation.update!(assignee: agent, assignee_agent_bot: nil)
result = service.perform
conversation.reload
expect(result).to eq(agent_bot)
expect(conversation.assignee_agent_bot_id).to eq(agent_bot.id)
expect(conversation.assignee_id).to be_nil
end
end
end
end