2022-08-16 11:28:23 +00:00
|
|
|
module AutoAssignmentHandler
|
2021-03-12 09:43:58 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include Events::Types
|
|
|
|
|
|
|
|
included do
|
2022-08-16 11:28:23 +00:00
|
|
|
after_save :run_auto_assignment
|
2021-03-12 09:43:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-08-16 11:28:23 +00:00
|
|
|
def run_auto_assignment
|
2021-03-12 09:43:58 +00:00
|
|
|
# Round robin kicks in on conversation create & update
|
|
|
|
# run it only when conversation status changes to open
|
|
|
|
return unless conversation_status_changed_to_open?
|
2022-08-16 11:28:23 +00:00
|
|
|
return unless should_run_auto_assignment?
|
2021-03-12 09:43:58 +00:00
|
|
|
|
2022-08-16 11:28:23 +00:00
|
|
|
::AutoAssignment::AgentAssignmentService.new(conversation: self, allowed_agent_ids: inbox.member_ids_with_assignment_capacity).perform
|
2021-03-12 09:43:58 +00:00
|
|
|
end
|
|
|
|
|
2022-08-16 11:28:23 +00:00
|
|
|
def should_run_auto_assignment?
|
2021-03-12 09:43:58 +00:00
|
|
|
return false unless inbox.enable_auto_assignment?
|
|
|
|
|
|
|
|
# run only if assignee is blank or doesn't have access to inbox
|
|
|
|
assignee.blank? || inbox.members.exclude?(assignee)
|
|
|
|
end
|
|
|
|
end
|