2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: conversations
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# additional_attributes :jsonb
|
|
|
|
# agent_last_seen_at :datetime
|
|
|
|
# locked :boolean default(FALSE)
|
|
|
|
# status :integer default("open"), not null
|
|
|
|
# user_last_seen_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# assignee_id :integer
|
|
|
|
# contact_id :bigint
|
|
|
|
# display_id :integer not null
|
|
|
|
# inbox_id :integer not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_conversations_on_account_id (account_id)
|
|
|
|
# index_conversations_on_account_id_and_display_id (account_id,display_id) UNIQUE
|
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Conversation < ApplicationRecord
|
|
|
|
include Events::Types
|
|
|
|
|
|
|
|
validates :account_id, presence: true
|
|
|
|
validates :inbox_id, presence: true
|
|
|
|
|
2019-10-08 19:45:04 +00:00
|
|
|
enum status: [:open, :resolved]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
scope :latest, -> { order(created_at: :desc) }
|
|
|
|
scope :unassigned, -> { where(assignee_id: nil) }
|
2019-10-08 19:45:04 +00:00
|
|
|
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
belongs_to :account
|
|
|
|
belongs_to :inbox
|
|
|
|
belongs_to :assignee, class_name: 'User', optional: true
|
2019-10-20 19:10:18 +00:00
|
|
|
belongs_to :contact
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
has_many :messages, dependent: :destroy, autosave: true
|
|
|
|
|
|
|
|
before_create :set_display_id, unless: :display_id?
|
|
|
|
|
2019-10-12 18:08:41 +00:00
|
|
|
after_update :notify_status_change, :create_activity, :send_email_notification_to_assignee
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-08-19 08:19:57 +00:00
|
|
|
after_create :send_events, :run_round_robin
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
acts_as_taggable_on :labels
|
|
|
|
|
2019-10-08 19:45:04 +00:00
|
|
|
def update_assignee(agent = nil)
|
2019-10-12 18:08:41 +00:00
|
|
|
update!(assignee: agent)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2019-10-08 19:45:04 +00:00
|
|
|
def update_labels(labels = nil)
|
2019-10-12 18:08:41 +00:00
|
|
|
update!(label_list: labels)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def toggle_status
|
2019-10-08 19:45:04 +00:00
|
|
|
self.status = open? ? :resolved : :open
|
2019-10-12 18:08:41 +00:00
|
|
|
save
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def lock!
|
2019-10-12 18:08:41 +00:00
|
|
|
update!(locked: true)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unlock!
|
2019-10-12 18:08:41 +00:00
|
|
|
update!(locked: false)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_messages
|
2019-10-12 18:08:41 +00:00
|
|
|
messages.unread_since(agent_last_seen_at)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_incoming_messages
|
2019-10-12 18:08:41 +00:00
|
|
|
messages.incoming.unread_since(agent_last_seen_at)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def push_event_data
|
2019-10-12 18:08:41 +00:00
|
|
|
Conversations::EventDataPresenter.new(self).push_data
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def lock_event_data
|
2019-10-12 18:08:41 +00:00
|
|
|
Conversations::EventDataPresenter.new(self).lock_data
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def dispatch_events
|
2019-10-12 18:08:41 +00:00
|
|
|
dispatcher_dispatch(CONVERSATION_RESOLVED)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_events
|
2019-10-12 18:08:41 +00:00
|
|
|
dispatcher_dispatch(CONVERSATION_CREATED)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_email_notification_to_assignee
|
2019-10-12 18:08:41 +00:00
|
|
|
return if self_assign?(assignee_id)
|
|
|
|
|
2019-12-24 21:33:02 +00:00
|
|
|
AssignmentMailer.conversation_assigned(self, assignee).deliver_later if saved_change_to_assignee_id? && assignee_id.present?
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self_assign?(assignee_id)
|
2019-10-12 18:08:41 +00:00
|
|
|
assignee_id.present? && Current.user&.id == assignee_id
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_display_id
|
|
|
|
self.display_id = loop do
|
2019-10-12 18:08:41 +00:00
|
|
|
next_display_id = account.conversations.maximum('display_id').to_i + 1
|
|
|
|
break next_display_id unless account.conversations.exists?(display_id: next_display_id)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_activity
|
2019-10-02 13:03:07 +00:00
|
|
|
return unless Current.user
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-08 19:45:04 +00:00
|
|
|
user_name = Current.user&.name
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
create_status_change_message(user_name) if saved_change_to_status?
|
2019-10-12 18:08:41 +00:00
|
|
|
create_assignee_change(user_name) if saved_change_to_assignee_id?
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2019-10-08 19:45:04 +00:00
|
|
|
def activity_message_params(content)
|
2019-10-12 18:08:41 +00:00
|
|
|
{ account_id: account_id, inbox_id: inbox_id, message_type: :activity, content: content }
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_status_change
|
2019-10-12 18:08:41 +00:00
|
|
|
{
|
|
|
|
CONVERSATION_RESOLVED => -> { saved_change_to_status? && resolved? && assignee.present? },
|
|
|
|
CONVERSATION_READ => -> { saved_change_to_user_last_seen_at? },
|
|
|
|
CONVERSATION_LOCK_TOGGLE => -> { saved_change_to_locked? },
|
|
|
|
ASSIGNEE_CHANGED => -> { saved_change_to_assignee_id? }
|
|
|
|
}.each do |event, condition|
|
|
|
|
condition.call && dispatcher_dispatch(event)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-05 09:12:50 +00:00
|
|
|
def dispatcher_dispatch(event_name)
|
2019-10-12 18:08:41 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(event_name, Time.zone.now, conversation: self)
|
2019-10-05 09:12:50 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def run_round_robin
|
2019-10-12 18:08:41 +00:00
|
|
|
# return unless conversation.account.has_feature?(round_robin)
|
|
|
|
# return unless conversation.account.round_robin_enabled?
|
2019-10-08 19:45:04 +00:00
|
|
|
return if assignee
|
|
|
|
|
2019-10-12 18:08:41 +00:00
|
|
|
inbox.next_available_agent.then { |new_assignee| update_assignee(new_assignee) }
|
2019-10-08 19:45:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_status_change_message(user_name)
|
2019-10-12 18:08:41 +00:00
|
|
|
content = I18n.t("conversations.activity.status.#{status}", user_name: user_name)
|
2019-10-08 19:45:04 +00:00
|
|
|
|
|
|
|
messages.create(activity_message_params(content))
|
|
|
|
end
|
|
|
|
|
2019-10-12 18:08:41 +00:00
|
|
|
def create_assignee_change(user_name)
|
|
|
|
params = { assignee_name: assignee&.name, user_name: user_name }.compact
|
|
|
|
key = assignee_id ? 'assigned' : 'removed'
|
|
|
|
content = I18n.t("conversations.activity.assignee.#{key}", params)
|
2019-10-08 19:45:04 +00:00
|
|
|
|
|
|
|
messages.create(activity_message_params(content))
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|