Fix abs rubocop metric of create_activity (#101)

* Fix abs rubocop metric of create_activity

* Fix anothers rubocop issues

* Remove comment that was removed on master
This commit is contained in:
Allan Klaus 2019-10-08 16:45:04 -03:00 committed by Sojan Jose
parent e066a9cff5
commit 0621fa66e4

View file

@ -4,11 +4,11 @@ class Conversation < ApplicationRecord
validates :account_id, presence: true validates :account_id, presence: true
validates :inbox_id, presence: true validates :inbox_id, presence: true
enum status: [ :open, :resolved ] enum status: [:open, :resolved]
scope :latest, -> { order(created_at: :desc) } scope :latest, -> { order(created_at: :desc) }
scope :unassigned, -> { where(assignee_id: nil) } scope :unassigned, -> { where(assignee_id: nil) }
scope :assigned_to, -> (agent) { where(assignee_id: agent.id) } scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
belongs_to :account belongs_to :account
belongs_to :inbox belongs_to :inbox
@ -25,62 +25,48 @@ class Conversation < ApplicationRecord
after_create :send_events, :run_round_robin after_create :send_events, :run_round_robin
acts_as_taggable_on :labels acts_as_taggable_on :labels
def update_assignee(agent=nil) def update_assignee(agent = nil)
self.assignee = agent self.assignee = agent
self.save! save!
end end
def update_labels(labels=nil) def update_labels(labels = nil)
self.label_list = labels self.label_list = labels
self.save! save!
end end
def toggle_status def toggle_status
if open? self.status = open? ? :resolved : :open
self.status = :resolved save! ? true : false
else
self.status = :open
end
self.save! ? true : false
end end
def lock! def lock!
self.locked = true self.locked = true
self.save! save!
end end
def unlock! def unlock!
self.locked = false self.locked = false
self.save! save!
end end
def unread_messages def unread_messages
# +1 is a hack to avoid https://makandracards.com/makandra/1057-why-two-ruby-time-objects-are-not-equal-although-they-appear-to-be # +1 is a hack to avoid https://makandracards.com/makandra/1057-why-two-ruby-time-objects-are-not-equal-although-they-appear-to-be
# ente budhiparamaya neekam kandit entu tonunu? # ente budhiparamaya neekam kandit entu tonunu?
messages.where("EXTRACT(EPOCH FROM created_at) > (?)", agent_last_seen_at.to_i + 1) messages.where('EXTRACT(EPOCH FROM created_at) > (?)', agent_last_seen_at.to_i + 1)
end end
def unread_incoming_messages def unread_incoming_messages
messages.incoming.where("EXTRACT(EPOCH FROM created_at) > (?)", agent_last_seen_at.to_i + 1) messages.incoming.where('EXTRACT(EPOCH FROM created_at) > (?)', agent_last_seen_at.to_i + 1)
end end
def push_event_data def push_event_data
last_message = messages.chat.last
{ {
meta: { meta: { sender: sender.push_event_data, assignee: assignee }, id: display_id,
sender: sender.push_event_data, messages: [messages.chat.last&.push_event_data], inbox_id: inbox_id, status: status_before_type_cast.to_i,
assignee: assignee timestamp: created_at.to_i, user_last_seen_at: user_last_seen_at.to_i, agent_last_seen_at: agent_last_seen_at.to_i,
},
id: display_id,
messages: [last_message.try(:push_event_data) ],
inbox_id: inbox_id,
status: status_before_type_cast.to_i,
timestamp: created_at.to_i,
user_last_seen_at: user_last_seen_at.to_i,
agent_last_seen_at: agent_last_seen_at.to_i,
unread_count: unread_incoming_messages.count unread_count: unread_incoming_messages.count
} }
end end
@ -103,19 +89,18 @@ class Conversation < ApplicationRecord
end end
def send_email_notification_to_assignee def send_email_notification_to_assignee
if assignee_id_changed? && assignee_id.present? && !self_assign?(assignee_id) AssignmentMailer.conversation_assigned(self, assignee).deliver if assignee_id_changed? && assignee_id.present? && !self_assign?(assignee_id)
AssignmentMailer.conversation_assigned(self, self.assignee).deliver
end
end end
def self_assign?(assignee_id) def self_assign?(assignee_id)
return false unless Current.user return false unless Current.user
Current.user.id == assignee_id Current.user.id == assignee_id
end end
def set_display_id def set_display_id
self.display_id = loop do self.display_id = loop do
disp_id = self.account.conversations.maximum("display_id").to_i + 1 disp_id = account.conversations.maximum('display_id').to_i + 1
break disp_id unless account.conversations.exists?(display_id: disp_id) break disp_id unless account.conversations.exists?(display_id: disp_id)
end end
end end
@ -123,8 +108,10 @@ class Conversation < ApplicationRecord
def create_activity def create_activity
return unless Current.user return unless Current.user
self.messages.create(activity_message_params(status_changed_message)) if status_changed? user_name = Current.user&.name
self.messages.create(activity_message_params(assignee_changed_message)) if assignee_id_changed?
create_status_change_message(user_name) if status_changed?
create_assignee_change(username) if assignee_id_changed?
end end
def status_changed_message def status_changed_message
@ -139,7 +126,7 @@ class Conversation < ApplicationRecord
"Conversation unassigned by #{Current.user.try(:name)}" "Conversation unassigned by #{Current.user.try(:name)}"
end end
def activity_message_params content def activity_message_params(content)
{ {
account_id: account_id, account_id: account_id,
inbox_id: inbox_id, inbox_id: inbox_id,
@ -166,13 +153,35 @@ class Conversation < ApplicationRecord
end end
def run_round_robin def run_round_robin
if true #conversation.account.has_feature?(round_robin) return unless true # conversation.account.has_feature?(round_robin)
if true #conversation.account.round_robin_enabled? return unless true # conversation.account.round_robin_enabled?
unless self.assignee #if not already assigned return if assignee
new_assignee = self.inbox.next_available_agent
self.update_assignee(new_assignee) if new_assignee new_assignee = inbox.next_available_agent
end update_assignee(new_assignee) if new_assignee
end end
end
def create_status_change_message(user_name)
content = if resolved?
"Conversation was marked resolved by #{user_name}"
else
"Conversation was reopened by #{user_name}"
end
messages.create(activity_message_params(content))
end
def create_assignee_change(username)
content = if assignee_id
"Assigned to #{assignee.name} by #{username}"
else
"Conversation unassigned by #{username}"
end
messages.create(activity_message_params(content))
end
def resolved_and_assignee?
resolved? && assignee.present?
end end
end end