chore: migrate max_assignment_limit to agent capacity policies
Adds a data migration and rake task to convert inbox max_assignment_limit settings into AgentCapacityPolicy records with per-inbox conversation limits, preserving the exact capacity-based semantics. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
class MigrateMaxAssignmentLimitToPolicies < ActiveRecord::Migration[7.1]
|
||||
def up
|
||||
Account.find_each do |account|
|
||||
migrate_account(account)
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
AgentCapacityPolicy.where(name: 'Auto (Migrated)').destroy_all
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def migrate_account(account)
|
||||
inboxes_with_limit = account.inboxes
|
||||
.where("(auto_assignment_config->>'max_assignment_limit')::int > 0")
|
||||
|
||||
return if inboxes_with_limit.empty?
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
policy = AgentCapacityPolicy.find_or_create_by!(account: account, name: 'Auto (Migrated)') do |p|
|
||||
p.description = 'Migrated from max_assignment_limit on inbox auto_assignment_config'
|
||||
end
|
||||
|
||||
create_inbox_limits(policy, inboxes_with_limit)
|
||||
assign_policy_to_inbox_members(account, policy, inboxes_with_limit)
|
||||
end
|
||||
end
|
||||
|
||||
def create_inbox_limits(policy, inboxes)
|
||||
inboxes.each do |inbox|
|
||||
next if InboxCapacityLimit.exists?(agent_capacity_policy_id: policy.id, inbox_id: inbox.id)
|
||||
|
||||
limit = inbox.auto_assignment_config['max_assignment_limit'].to_i
|
||||
|
||||
InboxCapacityLimit.create!(
|
||||
agent_capacity_policy: policy,
|
||||
inbox: inbox,
|
||||
conversation_limit: limit
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def assign_policy_to_inbox_members(account, policy, inboxes)
|
||||
member_user_ids = InboxMember.where(inbox_id: inboxes.select(:id)).distinct.pluck(:user_id)
|
||||
|
||||
account.account_users
|
||||
.where(user_id: member_user_ids, agent_capacity_policy_id: nil)
|
||||
.find_each { |account_user| account_user.update!(agent_capacity_policy_id: policy.id) }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
# Manually run the max_assignment_limit to agent capacity policy migration.
|
||||
# This is idempotent and safe to run multiple times.
|
||||
#
|
||||
# Usage:
|
||||
# bundle exec rake assignment:migrate_max_limit
|
||||
namespace :assignment do
|
||||
desc 'Migrate max_assignment_limit from inbox config to agent capacity policies'
|
||||
task migrate_max_limit: :environment do
|
||||
MigrateMaxAssignmentLimitToPolicies.new.up
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user