diff --git a/app/jobs/auto_assignment/assignment_job.rb b/app/jobs/auto_assignment/assignment_job.rb index 1aecbda64..9c6760ecc 100644 --- a/app/jobs/auto_assignment/assignment_job.rb +++ b/app/jobs/auto_assignment/assignment_job.rb @@ -8,15 +8,9 @@ class AutoAssignment::AssignmentJob < ApplicationJob service = AutoAssignment::AssignmentService.new(inbox: inbox) assigned_count = service.perform_bulk_assignment(limit: bulk_assignment_limit) - success_message = I18n.t('jobs.auto_assignment.assignment_job.bulk_assignment_success', - assigned_count: assigned_count, - inbox_id: inbox.id) - Rails.logger.info success_message + Rails.logger.info "Assigned #{assigned_count} conversations for inbox #{inbox.id}" rescue StandardError => e - error_message = I18n.t('jobs.auto_assignment.assignment_job.bulk_assignment_failed', - inbox_id: inbox.id, - error_message: e.message) - Rails.logger.error error_message + Rails.logger.error "Bulk assignment failed for inbox #{inbox_id}: #{e.message}" raise e if Rails.env.test? end diff --git a/app/jobs/auto_assignment/periodic_assignment_job.rb b/app/jobs/auto_assignment/periodic_assignment_job.rb index 310b5d91f..63500507e 100644 --- a/app/jobs/auto_assignment/periodic_assignment_job.rb +++ b/app/jobs/auto_assignment/periodic_assignment_job.rb @@ -2,13 +2,17 @@ class AutoAssignment::PeriodicAssignmentJob < ApplicationJob queue_as :scheduled_jobs def perform - Account.find_each do |account| - next unless account.feature_enabled?('assignment_v2') + Account.find_in_batches do |accounts| + accounts.each do |account| + next unless account.feature_enabled?('assignment_v2') - account.inboxes.joins(:assignment_policy).find_each do |inbox| - next unless inbox.auto_assignment_enabled? + account.inboxes.joins(:assignment_policy).find_in_batches do |inboxes| + inboxes.each do |inbox| + next unless inbox.auto_assignment_v2_enabled? - AutoAssignment::AssignmentJob.perform_later(inbox_id: inbox.id) + AutoAssignment::AssignmentJob.perform_later(inbox_id: inbox.id) + end + end end end end diff --git a/app/models/concerns/auto_assignment_handler.rb b/app/models/concerns/auto_assignment_handler.rb index 5e7adbc84..a1198200a 100644 --- a/app/models/concerns/auto_assignment_handler.rb +++ b/app/models/concerns/auto_assignment_handler.rb @@ -14,7 +14,7 @@ module AutoAssignmentHandler return unless conversation_status_changed_to_open? return unless should_run_auto_assignment? - if inbox.auto_assignment_enabled? + if inbox.auto_assignment_v2_enabled? # Use new assignment system AutoAssignment::AssignmentJob.perform_later(inbox_id: inbox.id) else diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 9e25e82f7..0ad9686b9 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -191,7 +191,7 @@ class Inbox < ApplicationRecord members.ids end - def auto_assignment_enabled? + def auto_assignment_v2_enabled? account.feature_enabled?('assignment_v2') && assignment_policy.present? && assignment_policy.enabled? end diff --git a/app/services/auto_assignment/assignment_service.rb b/app/services/auto_assignment/assignment_service.rb index ebe7e334c..48c8b1fba 100644 --- a/app/services/auto_assignment/assignment_service.rb +++ b/app/services/auto_assignment/assignment_service.rb @@ -1,15 +1,6 @@ class AutoAssignment::AssignmentService pattr_initialize [:inbox!] - def perform_for_conversation(conversation) - return false unless assignable?(conversation) - - agent = find_available_agent - return false unless agent - - assign_conversation(conversation, agent) - end - def perform_bulk_assignment(limit: 100) return 0 unless inbox.enable_auto_assignment? @@ -24,27 +15,30 @@ class AutoAssignment::AssignmentService private + def perform_for_conversation(conversation) + return false unless assignable?(conversation) + + agent = find_available_agent + return false unless agent + + assign_conversation(conversation, agent) + end + def assignable?(conversation) - inbox.enable_auto_assignment? && - conversation.status == 'open' && + conversation.status == 'open' && conversation.assignee_id.nil? end def unassigned_conversations(limit) scope = inbox.conversations.unassigned.open - # Apply conversation priority from config - scope = apply_conversation_priority(scope) - scope.limit(limit) - end - - def apply_conversation_priority(scope) - case assignment_config['conversation_priority'] - when 'longest_waiting' - scope.order(last_activity_at: :asc, created_at: :asc) + if assignment_config['conversation_priority'] == 'longest_waiting' + scope = scope.order(last_activity_at: :asc, created_at: :asc) else scope.order(created_at: :asc) end + + scope.limit(limit) end def find_available_agent @@ -69,9 +63,6 @@ class AutoAssignment::AssignmentService dispatch_assignment_event(conversation, agent) true - rescue ActiveRecord::RecordInvalid => e - Rails.logger.error "AutoAssignment failed for conversation #{conversation.id}: #{e.message}" - false end def dispatch_assignment_event(conversation, agent) diff --git a/config/features.yml b/config/features.yml index c7c227789..c64584190 100644 --- a/config/features.yml +++ b/config/features.yml @@ -194,12 +194,12 @@ - name: twilio_content_templates display_name: Twilio Content Templates enabled: false +- name: assignment_v2 + display_name: Assignment V2 + enabled: false + chatwoot_internal: true - name: advanced_search display_name: Advanced Search enabled: false premium: true chatwoot_internal: true -- name: assignment_v2 - display_name: Assignment V2 - enabled: false - chatwoot_internal: true diff --git a/config/locales/en.yml b/config/locales/en.yml index 0dddaad88..cb72bb699 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -394,11 +394,6 @@ en: %{format_messages} agent_capacity_policy: inbox_already_assigned: 'Inbox has already been assigned to this policy' - jobs: - auto_assignment: - assignment_job: - bulk_assignment_success: 'Assigned %{assigned_count} conversations for inbox %{inbox_id}' - bulk_assignment_failed: 'Bulk assignment failed for inbox %{inbox_id}: %{error_message}' portals: send_instructions: email_required: 'Email is required' diff --git a/enterprise/app/models/enterprise/assignment_policy.rb b/enterprise/app/models/enterprise/assignment_policy.rb deleted file mode 100644 index 490efcc39..000000000 --- a/enterprise/app/models/enterprise/assignment_policy.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Enterprise::AssignmentPolicy - def assignment_order=(value) - if value.to_s == 'balanced' - write_attribute(:assignment_order, 1) - else - super - end - end - - def assignment_order - value = read_attribute(:assignment_order) - return 'balanced' if value == 1 - - super - end - - def balanced? - self[:assignment_order] == 1 - end -end