Feat: custom sort (#4864)
This commit is contained in:
25
app/models/concerns/sort_handler.rb
Normal file
25
app/models/concerns/sort_handler.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
module SortHandler
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
def self.latest
|
||||
order(last_activity_at: :desc)
|
||||
end
|
||||
|
||||
def self.sort_on_created_at
|
||||
order(created_at: :asc)
|
||||
end
|
||||
|
||||
def self.last_messaged_conversations
|
||||
Message.except(:order).select('DISTINCT ON (conversation_id) *').order('conversation_id, created_at DESC')
|
||||
end
|
||||
|
||||
def self.sort_on_last_user_message_at
|
||||
where(
|
||||
'grouped_conversations.message_type = 0'
|
||||
).order(
|
||||
'grouped_conversations.created_at ASC'
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -50,6 +50,7 @@ class Conversation < ApplicationRecord
|
||||
include RoundRobinHandler
|
||||
include ActivityMessageHandler
|
||||
include UrlHelper
|
||||
include SortHandler
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
@@ -60,7 +61,6 @@ class Conversation < ApplicationRecord
|
||||
|
||||
enum status: { open: 0, resolved: 1, pending: 2, snoozed: 3 }
|
||||
|
||||
scope :latest, -> { order(last_activity_at: :desc) }
|
||||
scope :unassigned, -> { where(assignee_id: nil) }
|
||||
scope :assigned, -> { where.not(assignee_id: nil) }
|
||||
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
|
||||
@@ -70,6 +70,13 @@ class Conversation < ApplicationRecord
|
||||
open.where('last_activity_at < ? ', Time.now.utc - auto_resolve_duration.days)
|
||||
}
|
||||
|
||||
scope :last_user_message_at, lambda {
|
||||
joins(
|
||||
"INNER JOIN (#{last_messaged_conversations.to_sql}) grouped_conversations
|
||||
ON grouped_conversations.conversation_id = conversations.id"
|
||||
).sort_on_last_user_message_at
|
||||
}
|
||||
|
||||
belongs_to :account
|
||||
belongs_to :inbox
|
||||
belongs_to :assignee, class_name: 'User', optional: true
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
# fk_rails_... (user_id => users.id) ON DELETE => cascade
|
||||
#
|
||||
class Mention < ApplicationRecord
|
||||
include SortHandler
|
||||
|
||||
before_validation :ensure_account_id
|
||||
validates :mentioned_at, presence: true
|
||||
validates :account_id, presence: true
|
||||
@@ -38,6 +40,17 @@ class Mention < ApplicationRecord
|
||||
|
||||
scope :latest, -> { order(mentioned_at: :desc) }
|
||||
|
||||
def self.last_user_message_at
|
||||
# INNER query finds the last message created in the conversation group
|
||||
# The outer query JOINS with the latest created message conversations
|
||||
# Then select only latest incoming message from the conversations which doesn't have last message as outgoing
|
||||
# Order by message created_at
|
||||
Mention.joins(
|
||||
"INNER JOIN (#{last_messaged_conversations.to_sql}) grouped_conversations
|
||||
ON grouped_conversations.conversation_id = mentions.conversation_id"
|
||||
).sort_on_last_user_message_at
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_account_id
|
||||
|
||||
Reference in New Issue
Block a user