feat: Add agent capacity controllers (#12200)

## Linear reference:
https://linear.app/chatwoot/issue/CW-4649/re-imagine-assignments

## Description
This PR introduces the foundation for Assignment V2 system by
implementing agent_capacity and their association with inboxes and
users.

## Type of change

- [ ] New feature (non-breaking change which adds functionality)

## How Has This Been Tested?

Test Coverage:
-  Controller specs for assignment policies CRUD operations
-  Enterprise-specific specs for balanced assignment order
-  Model specs for community/enterprise separation

## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

---------

Co-authored-by: Pranav <pranav@chatwoot.com>
This commit is contained in:
Tanmay Deep Sharma
2025-08-27 09:12:58 +07:00
committed by GitHub
parent 39dfa35229
commit ad90deb709
26 changed files with 679 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
class Api::V1::Accounts::AgentCapacityPolicies::InboxLimitsController < Api::V1::Accounts::EnterpriseAccountsController
before_action -> { check_authorization(AgentCapacityPolicy) }
before_action :fetch_policy
before_action :fetch_inbox, only: [:create]
before_action :fetch_inbox_limit, only: [:update, :destroy]
before_action :validate_no_duplicate, only: [:create]
def create
@inbox_limit = @agent_capacity_policy.inbox_capacity_limits.create!(
inbox: @inbox,
conversation_limit: permitted_params[:conversation_limit]
)
end
def update
@inbox_limit.update!(conversation_limit: permitted_params[:conversation_limit])
end
def destroy
@inbox_limit.destroy!
head :no_content
end
private
def fetch_policy
@agent_capacity_policy = Current.account.agent_capacity_policies.find(params[:agent_capacity_policy_id])
end
def fetch_inbox
@inbox = Current.account.inboxes.find(permitted_params[:inbox_id])
end
def fetch_inbox_limit
@inbox_limit = @agent_capacity_policy.inbox_capacity_limits.find(params[:id])
end
def validate_no_duplicate
return unless @agent_capacity_policy.inbox_capacity_limits.exists?(inbox: @inbox)
render_could_not_create_error(I18n.t('agent_capacity_policy.inbox_already_assigned'))
end
def permitted_params
params.permit(:inbox_id, :conversation_limit)
end
end

View File

@@ -0,0 +1,35 @@
class Api::V1::Accounts::AgentCapacityPolicies::UsersController < Api::V1::Accounts::EnterpriseAccountsController
before_action -> { check_authorization(AgentCapacityPolicy) }
before_action :fetch_policy
before_action :fetch_user, only: [:destroy]
def index
@users = Current.account.users.joins(:account_users)
.where(account_users: { agent_capacity_policy_id: @agent_capacity_policy.id })
end
def create
@account_user = Current.account.account_users.find_by!(user_id: permitted_params[:user_id])
@account_user.update!(agent_capacity_policy: @agent_capacity_policy)
@user = @account_user.user
end
def destroy
@account_user.update!(agent_capacity_policy: nil)
head :ok
end
private
def fetch_policy
@agent_capacity_policy = Current.account.agent_capacity_policies.find(params[:agent_capacity_policy_id])
end
def fetch_user
@account_user = Current.account.account_users.find_by!(user_id: params[:id])
end
def permitted_params
params.permit(:user_id)
end
end

View File

@@ -0,0 +1,37 @@
class Api::V1::Accounts::AgentCapacityPoliciesController < Api::V1::Accounts::EnterpriseAccountsController
before_action :check_authorization
before_action :fetch_policy, only: [:show, :update, :destroy]
def index
@agent_capacity_policies = Current.account.agent_capacity_policies
end
def show; end
def create
@agent_capacity_policy = Current.account.agent_capacity_policies.create!(permitted_params)
end
def update
@agent_capacity_policy.update!(permitted_params)
end
def destroy
@agent_capacity_policy.destroy!
head :ok
end
private
def permitted_params
params.require(:agent_capacity_policy).permit(
:name,
:description,
exclusion_rules: [:overall_capacity, { hours: [], days: [] }]
)
end
def fetch_policy
@agent_capacity_policy = Current.account.agent_capacity_policies.find(params[:id])
end
end

View File

@@ -0,0 +1,27 @@
# == Schema Information
#
# Table name: agent_capacity_policies
#
# id :bigint not null, primary key
# description :text
# exclusion_rules :jsonb not null
# name :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
#
# Indexes
#
# index_agent_capacity_policies_on_account_id (account_id)
#
class AgentCapacityPolicy < ApplicationRecord
MAX_NAME_LENGTH = 255
belongs_to :account
has_many :inbox_capacity_limits, dependent: :destroy
has_many :inboxes, through: :inbox_capacity_limits
has_many :account_users, dependent: :nullify
validates :name, presence: true, length: { maximum: MAX_NAME_LENGTH }
validates :account, presence: true
end

View File

@@ -5,6 +5,7 @@ module Enterprise::Concerns::Account
has_many :sla_policies, dependent: :destroy_async
has_many :applied_slas, dependent: :destroy_async
has_many :custom_roles, dependent: :destroy_async
has_many :agent_capacity_policies, dependent: :destroy_async
has_many :captain_assistants, dependent: :destroy_async, class_name: 'Captain::Assistant'
has_many :captain_assistant_responses, dependent: :destroy_async, class_name: 'Captain::AssistantResponse'

View File

@@ -3,5 +3,6 @@ module Enterprise::Concerns::AccountUser
included do
belongs_to :custom_role, optional: true
belongs_to :agent_capacity_policy, optional: true
end
end

View File

@@ -0,0 +1,24 @@
# == Schema Information
#
# Table name: inbox_capacity_limits
#
# id :bigint not null, primary key
# conversation_limit :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# agent_capacity_policy_id :bigint not null
# inbox_id :bigint not null
#
# Indexes
#
# idx_on_agent_capacity_policy_id_inbox_id_71c7ec4caf (agent_capacity_policy_id,inbox_id) UNIQUE
# index_inbox_capacity_limits_on_agent_capacity_policy_id (agent_capacity_policy_id)
# index_inbox_capacity_limits_on_inbox_id (inbox_id)
#
class InboxCapacityLimit < ApplicationRecord
belongs_to :agent_capacity_policy
belongs_to :inbox
validates :conversation_limit, presence: true, numericality: { greater_than: 0, only_integer: true }
validates :inbox_id, uniqueness: { scope: :agent_capacity_policy_id }
end

View File

@@ -0,0 +1,21 @@
class AgentCapacityPolicyPolicy < ApplicationPolicy
def index?
@account_user.administrator?
end
def create?
@account_user.administrator?
end
def show?
@account_user.administrator?
end
def update?
@account_user.administrator?
end
def destroy?
@account_user.administrator?
end
end

View File

@@ -0,0 +1,2 @@
json.partial! 'api/v1/models/agent_capacity_policy', formats: [:json],
agent_capacity_policy: @agent_capacity_policy

View File

@@ -0,0 +1,6 @@
json.id @inbox_limit.id
json.inbox_id @inbox_limit.inbox_id
json.agent_capacity_policy_id @inbox_limit.agent_capacity_policy_id
json.conversation_limit @inbox_limit.conversation_limit
json.created_at @inbox_limit.created_at.to_i
json.updated_at @inbox_limit.updated_at.to_i

View File

@@ -0,0 +1,7 @@
json.id @inbox_limit.id
json.inbox_id @inbox_limit.inbox_id
json.inbox_name @inbox_limit.inbox.name
json.agent_capacity_policy_id @agent_capacity_policy.id
json.conversation_limit @inbox_limit.conversation_limit
json.created_at @inbox_limit.created_at.to_i
json.updated_at @inbox_limit.updated_at.to_i

View File

@@ -0,0 +1,4 @@
json.array! @agent_capacity_policies do |policy|
json.partial! 'api/v1/models/agent_capacity_policy', formats: [:json],
agent_capacity_policy: policy
end

View File

@@ -0,0 +1,2 @@
json.partial! 'api/v1/models/agent_capacity_policy', formats: [:json],
agent_capacity_policy: @agent_capacity_policy

View File

@@ -0,0 +1,2 @@
json.partial! 'api/v1/models/agent_capacity_policy', formats: [:json],
agent_capacity_policy: @agent_capacity_policy

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/user', resource: @user

View File

@@ -0,0 +1,3 @@
json.array! @users do |user|
json.partial! 'api/v1/models/user', resource: user
end

View File

@@ -0,0 +1,13 @@
json.id agent_capacity_policy.id
json.name agent_capacity_policy.name
json.description agent_capacity_policy.description
json.exclusion_rules agent_capacity_policy.exclusion_rules
json.created_at agent_capacity_policy.created_at.to_i
json.updated_at agent_capacity_policy.updated_at.to_i
json.account_id agent_capacity_policy.account_id
json.inbox_capacity_limits agent_capacity_policy.inbox_capacity_limits do |limit|
json.id limit.id
json.inbox_id limit.inbox_id
json.conversation_limit limit.conversation_limit
end