refactor: route assignment handoff through service
This commit is contained in:
+12
-17
@@ -66,6 +66,7 @@ class Conversation < ApplicationRecord
|
||||
validates :inbox_id, presence: true
|
||||
validates :contact_id, presence: true
|
||||
before_validation :validate_additional_attributes
|
||||
before_validation :reset_agent_bot_when_assignee_present
|
||||
validates :additional_attributes, jsonb_attributes_length: true
|
||||
validates :custom_attributes, jsonb_attributes_length: true
|
||||
validates :uuid, uniqueness: true
|
||||
@@ -116,7 +117,6 @@ class Conversation < ApplicationRecord
|
||||
has_many :attachments, through: :messages
|
||||
has_many :reporting_events, dependent: :destroy_async
|
||||
|
||||
before_save :handle_agent_bot_takeover_by_assignee
|
||||
before_save :ensure_snooze_until_reset
|
||||
before_create :determine_conversation_status
|
||||
before_create :ensure_waiting_since
|
||||
@@ -166,9 +166,18 @@ class Conversation < ApplicationRecord
|
||||
def bot_handoff!
|
||||
mark_bot_handoff
|
||||
save!
|
||||
dispatch_bot_handoff
|
||||
end
|
||||
|
||||
def dispatch_bot_handoff
|
||||
dispatcher_dispatch(CONVERSATION_BOT_HANDOFF)
|
||||
end
|
||||
|
||||
def mark_bot_handoff
|
||||
self.waiting_since ||= Time.current
|
||||
self.status = :open
|
||||
end
|
||||
|
||||
def unread_messages
|
||||
agent_last_seen_at.present? ? messages.created_since(agent_last_seen_at) : messages
|
||||
end
|
||||
@@ -271,18 +280,12 @@ class Conversation < ApplicationRecord
|
||||
self.additional_attributes = {} unless additional_attributes.is_a?(Hash)
|
||||
end
|
||||
|
||||
def handle_agent_bot_takeover_by_assignee
|
||||
return if assignee_id.blank? || assignee_agent_bot_id.blank?
|
||||
def reset_agent_bot_when_assignee_present
|
||||
return if assignee_id.blank?
|
||||
|
||||
mark_bot_handoff if pending?
|
||||
self.assignee_agent_bot_id = nil
|
||||
end
|
||||
|
||||
def mark_bot_handoff
|
||||
self.waiting_since ||= Time.current
|
||||
self.status = :open
|
||||
end
|
||||
|
||||
def determine_conversation_status
|
||||
self.status = :resolved and return if contact.blocked?
|
||||
|
||||
@@ -339,7 +342,6 @@ class Conversation < ApplicationRecord
|
||||
CONVERSATION_OPENED => -> { saved_change_to_status? && open? },
|
||||
CONVERSATION_RESOLVED => -> { saved_change_to_status? && resolved? },
|
||||
CONVERSATION_STATUS_CHANGED => -> { saved_change_to_status? },
|
||||
CONVERSATION_BOT_HANDOFF => -> { agent_bot_takeover_by_assignee? },
|
||||
CONVERSATION_READ => -> { saved_change_to_contact_last_seen_at? },
|
||||
CONVERSATION_CONTACT_CHANGED => -> { saved_change_to_contact_id? }
|
||||
}.each do |event, condition|
|
||||
@@ -347,13 +349,6 @@ class Conversation < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def agent_bot_takeover_by_assignee?
|
||||
assignee_id.present? &&
|
||||
saved_change_to_status?(from: 'pending', to: 'open') &&
|
||||
saved_change_to_assignee_agent_bot_id? &&
|
||||
assignee_agent_bot_id.blank?
|
||||
end
|
||||
|
||||
def dispatcher_dispatch(event_name, changed_attributes = nil)
|
||||
Rails.configuration.dispatcher.dispatch(event_name, Time.zone.now, conversation: self, notifiable_assignee_change: notifiable_assignee_change?,
|
||||
changed_attributes: changed_attributes,
|
||||
|
||||
Reference in New Issue
Block a user