Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

4.0 KB
Raw Blame
require 'rails_helper'

RSpec.describe 'Captain assistant conversation assignment API', type: :request do
  let(:account) { create(:account) }
  let(:agent) { create(:user, account: account, role: :agent) }
  let(:inbox) { create(:inbox, account: account) }
  let(:conversation) { create(:conversation, account: account, inbox: inbox) }
  let(:assistant) { create(:captain_assistant, account: account) }

  before do
    account.enable_features!('captain_integration')
    create(:inbox_member, inbox: inbox, user: agent)
    conversation
    create(:captain_inbox, captain_assistant: assistant, inbox: inbox)
  end

  it 'assigns the connected Captain assistant and marks the conversation pending' do
    conversation.update!(assignee: agent, status: :open)

    post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
         params: { assignee_id: assistant.id, assignee_type: 'Captain::Assistant' },
         headers: agent.create_new_auth_token,
         as: :json

    expect(response).to have_http_status(:success)
    expect(response.parsed_body).to include('id' => assistant.id, 'name' => assistant.name)
    expect(conversation.reload).to have_attributes(
      ai_assignee: assistant,
      ai_assignee_type: 'Captain::Assistant',
      assignee: nil,
      status: 'pending'
    )

    get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}",
        headers: agent.create_new_auth_token,
        as: :json

    expect(response.parsed_body.dig('meta', 'assignee')).to include('id' => assistant.id, 'name' => assistant.name)
    expect(response.parsed_body.dig('meta', 'assignee_type')).to eq('Captain::Assistant')
  end

  it 'does not assign an unconnected Captain assistant' do
    unconnected_assistant = create(:captain_assistant, account: account)

    post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
         params: { assignee_id: unconnected_assistant.id, assignee_type: 'Captain::Assistant' },
         headers: agent.create_new_auth_token,
         as: :json

    expect(response).to have_http_status(:success)
    expect(response.parsed_body).to be_nil
    expect(conversation.reload.ai_assignee).to be_nil
  end

  it 'preserves the human assignment when the account entitlement is disabled' do
    conversation.update!(assignee: agent, status: :open)
    account.disable_features!('captain_integration')

    post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
         params: { assignee_id: assistant.id, assignee_type: 'Captain::Assistant' },
         headers: agent.create_new_auth_token,
         as: :json

    expect(response).to have_http_status(:success)
    expect(response.parsed_body).to be_nil
    expect(conversation.reload).to have_attributes(ai_assignee: nil, assignee: agent, status: 'open')
  end

  it 'opens the conversation when Captain assistant ownership is cleared without a replacement' do
    conversation.update!(ai_assignee: assistant, status: :pending)

    post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
         params: { assignee_id: nil },
         headers: agent.create_new_auth_token,
         as: :json

    expect(response).to have_http_status(:success)
    expect(conversation.reload).to have_attributes(ai_assignee: nil, assignee: nil, status: 'open')
  end

  it 'clears Captain assistant ownership when a human takes over' do
    conversation.update!(ai_assignee: assistant, status: :pending)

    post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
         params: { assignee_id: agent.id },
         headers: agent.create_new_auth_token,
         as: :json

    expect(response).to have_http_status(:success)
    expect(conversation.reload).to have_attributes(
      ai_assignee: nil,
      ai_assignee_type: nil,
      assignee: agent,
      status: 'open'
    )
  end
end