feat: Add a sort option for conversations waiting for a reply from an agent (#7364)

This commit is contained in:
Pranav Raj S
2023-06-21 13:20:39 -07:00
committed by GitHub
parent e6a49b5800
commit 93daaea19b
15 changed files with 166 additions and 39 deletions

View File

@@ -29,5 +29,13 @@ module SortHandler
)
)
end
def self.sort_on_waiting_since
order(
Arel::Nodes::SqlLiteral.new(
sanitize_sql_for_order('CASE WHEN waiting_since IS NULL THEN now() ELSE waiting_since END ASC, created_at ASC')
)
)
end
end
end

View File

@@ -15,6 +15,7 @@
# snoozed_until :datetime
# status :integer default("open"), not null
# uuid :uuid not null
# waiting_since :datetime
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
@@ -45,6 +46,7 @@
# index_conversations_on_status_and_priority (status,priority)
# index_conversations_on_team_id (team_id)
# index_conversations_on_uuid (uuid) UNIQUE
# index_conversations_on_waiting_since (waiting_since)
#
class Conversation < ApplicationRecord
@@ -101,6 +103,7 @@ class Conversation < ApplicationRecord
before_save :ensure_snooze_until_reset
before_create :mark_conversation_pending_if_bot
before_create :ensure_waiting_since
after_update_commit :execute_after_update_commit_callbacks
after_create_commit :notify_conversation_creation
@@ -214,6 +217,10 @@ class Conversation < ApplicationRecord
self.snoozed_until = nil unless snoozed?
end
def ensure_waiting_since
self.waiting_since = Time.now.utc
end
def validate_additional_attributes
self.additional_attributes = {} unless additional_attributes.is_a?(Hash)
end

View File

@@ -201,7 +201,14 @@ class Message < ApplicationRecord
end
def valid_first_reply?
outgoing? && human_response? && not_created_by_automation? && !private?
return false unless outgoing? && human_response? && !private?
return false if conversation.first_reply_created_at.present?
return false if conversation.messages.outgoing
.where.not(sender_type: 'AgentBot')
.where.not(private: true)
.where("(additional_attributes->'campaign_id') is null").count > 1
true
end
def save_story_info(story_info)
@@ -238,39 +245,27 @@ class Message < ApplicationRecord
send_reply
execute_message_template_hooks
update_contact_activity
update_waiting_since
end
def update_contact_activity
sender.update(last_activity_at: DateTime.now) if sender.is_a?(Contact)
end
def human_response?
# given the checks are already in place, we need not query
# the database again to check if the message is created by a human
# we can just see if the first_reply is recorded or not
# if it is record, we can just return false
return false if conversation.first_reply_created_at.present?
def update_waiting_since
conversation.update(waiting_since: nil) if human_response? && !private && conversation.waiting_since.present?
# if the sender is not a user, it's not a human response
return false unless sender.is_a?(User)
# if automation rule id is present, it's not a human response
# if campaign id is present, it's not a human response
# this check already happens in `not_created_by_automation` but added here for the sake of brevity
# also the purity of this method is intact, and can be relied on this solely
return false if content_attributes['automation_rule_id'].present? || additional_attributes['campaign_id'].present?
# adding this condition again to ensure if the first_reply_created_at is not present
return false if conversation.messages.outgoing
.where.not(sender_type: 'AgentBot')
.where.not(private: true)
.where("(additional_attributes->'campaign_id') is null").count > 1
true
conversation.update(waiting_since: Time.now.utc) if incoming? && conversation.waiting_since.blank?
end
def not_created_by_automation?
content_attributes['automation_rule_id'].blank?
def human_response?
# if the sender is not a user, it's not a human response
# if automation rule id is present, it's not a human response
# if campaign id is present, it's not a human response
outgoing? &&
content_attributes['automation_rule_id'].blank? &&
additional_attributes['campaign_id'].blank? &&
sender.is_a?(User)
end
def dispatch_create_events