Feat: custom sort (#4864)
This commit is contained in:
@@ -2,6 +2,11 @@ class ConversationFinder
|
|||||||
attr_reader :current_user, :current_account, :params
|
attr_reader :current_user, :current_account, :params
|
||||||
|
|
||||||
DEFAULT_STATUS = 'open'.freeze
|
DEFAULT_STATUS = 'open'.freeze
|
||||||
|
SORT_OPTIONS = {
|
||||||
|
latest: 'latest',
|
||||||
|
sort_on_created_at: 'sort_on_created_at',
|
||||||
|
last_user_message_at: 'last_user_message_at'
|
||||||
|
}.with_indifferent_access
|
||||||
|
|
||||||
# assumptions
|
# assumptions
|
||||||
# inbox_id if not given, take from all conversations, else specific to inbox
|
# inbox_id if not given, take from all conversations, else specific to inbox
|
||||||
@@ -133,10 +138,7 @@ class ConversationFinder
|
|||||||
@conversations = @conversations.includes(
|
@conversations = @conversations.includes(
|
||||||
:taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox
|
:taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox
|
||||||
)
|
)
|
||||||
if params[:conversation_type] == 'mention'
|
sort_by = SORT_OPTIONS[params[:sort_by]] || SORT_OPTIONS['latest']
|
||||||
@conversations.page(current_page)
|
@conversations.send(sort_by).page(current_page)
|
||||||
else
|
|
||||||
@conversations.latest.page(current_page)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
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 RoundRobinHandler
|
||||||
include ActivityMessageHandler
|
include ActivityMessageHandler
|
||||||
include UrlHelper
|
include UrlHelper
|
||||||
|
include SortHandler
|
||||||
|
|
||||||
validates :account_id, presence: true
|
validates :account_id, presence: true
|
||||||
validates :inbox_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 }
|
enum status: { open: 0, resolved: 1, pending: 2, snoozed: 3 }
|
||||||
|
|
||||||
scope :latest, -> { order(last_activity_at: :desc) }
|
|
||||||
scope :unassigned, -> { where(assignee_id: nil) }
|
scope :unassigned, -> { where(assignee_id: nil) }
|
||||||
scope :assigned, -> { where.not(assignee_id: nil) }
|
scope :assigned, -> { where.not(assignee_id: nil) }
|
||||||
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
|
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)
|
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 :account
|
||||||
belongs_to :inbox
|
belongs_to :inbox
|
||||||
belongs_to :assignee, class_name: 'User', optional: true
|
belongs_to :assignee, class_name: 'User', optional: true
|
||||||
|
|||||||
@@ -23,6 +23,8 @@
|
|||||||
# fk_rails_... (user_id => users.id) ON DELETE => cascade
|
# fk_rails_... (user_id => users.id) ON DELETE => cascade
|
||||||
#
|
#
|
||||||
class Mention < ApplicationRecord
|
class Mention < ApplicationRecord
|
||||||
|
include SortHandler
|
||||||
|
|
||||||
before_validation :ensure_account_id
|
before_validation :ensure_account_id
|
||||||
validates :mentioned_at, presence: true
|
validates :mentioned_at, presence: true
|
||||||
validates :account_id, presence: true
|
validates :account_id, presence: true
|
||||||
@@ -38,6 +40,17 @@ class Mention < ApplicationRecord
|
|||||||
|
|
||||||
scope :latest, -> { order(mentioned_at: :desc) }
|
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
|
private
|
||||||
|
|
||||||
def ensure_account_id
|
def ensure_account_id
|
||||||
|
|||||||
10
spec/factories/mentions.rb
Normal file
10
spec/factories/mentions.rb
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
FactoryBot.define do
|
||||||
|
factory :mention do
|
||||||
|
mentioned_at { Time.current }
|
||||||
|
account
|
||||||
|
conversation
|
||||||
|
user
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -621,4 +621,73 @@ RSpec.describe Conversation, type: :model do
|
|||||||
expect(conversation['additional_attributes']['referer']).to eq('https://www.chatwoot.com/')
|
expect(conversation['additional_attributes']['referer']).to eq('https://www.chatwoot.com/')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'Custom Sort' do
|
||||||
|
include ActiveJob::TestHelper
|
||||||
|
|
||||||
|
let!(:conversation_4) { create(:conversation, created_at: DateTime.now - 10.days, last_activity_at: DateTime.now - 10.days) }
|
||||||
|
let!(:conversation_3) { create(:conversation, created_at: DateTime.now - 9.days, last_activity_at: DateTime.now - 9.days) }
|
||||||
|
let!(:conversation_1) { create(:conversation, created_at: DateTime.now - 8.days, last_activity_at: DateTime.now - 8.days) }
|
||||||
|
let!(:conversation_2) { create(:conversation, created_at: DateTime.now - 6.days, last_activity_at: DateTime.now - 6.days) }
|
||||||
|
|
||||||
|
it 'Sort conversations based on created_at' do
|
||||||
|
records = described_class.sort_on_created_at
|
||||||
|
|
||||||
|
expect(records.first.id).to eq(conversation_4.id)
|
||||||
|
expect(records.last.id).to eq(conversation_2.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'Sort conversations based on last_user_message_at' do
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :outgoing, created_at: DateTime.now - 9.days)
|
||||||
|
create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days)
|
||||||
|
create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days)
|
||||||
|
create(:message, conversation_id: conversation_1.id, message_type: :outgoing, created_at: DateTime.now - 7.days)
|
||||||
|
create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 2.days)
|
||||||
|
|
||||||
|
records = described_class.last_user_message_at
|
||||||
|
|
||||||
|
expect(records[0]['id']).to eq(conversation_2.id)
|
||||||
|
expect(records[1]['id']).to eq(conversation_3.id)
|
||||||
|
expect(records.pluck(:id)).not_to include(conversation_4.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when last_activity_at updated by some actions' do
|
||||||
|
before do
|
||||||
|
create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days)
|
||||||
|
create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 2.days)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sort conversations with latest resolved conversation at first' do
|
||||||
|
records = described_class.latest
|
||||||
|
|
||||||
|
expect(records.first.id).to eq(conversation_3.id)
|
||||||
|
|
||||||
|
conversation_1.toggle_status
|
||||||
|
perform_enqueued_jobs do
|
||||||
|
Conversations::ActivityMessageJob.perform_later(
|
||||||
|
conversation_1,
|
||||||
|
account_id: conversation_1.account_id,
|
||||||
|
inbox_id: conversation_1.inbox_id,
|
||||||
|
message_type: :activity,
|
||||||
|
content: 'Conversation was marked resolved by system due to days of inactivity'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
records = described_class.latest
|
||||||
|
|
||||||
|
expect(records.first.id).to eq(conversation_1.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'Sort conversations with latest message' do
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now)
|
||||||
|
records = described_class.latest
|
||||||
|
|
||||||
|
expect(records.first.id).to eq(conversation_3.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
67
spec/models/mention_spec.rb
Normal file
67
spec/models/mention_spec.rb
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Mention, type: :model do
|
||||||
|
describe 'associations' do
|
||||||
|
it { is_expected.to belong_to(:account) }
|
||||||
|
it { is_expected.to belong_to(:user) }
|
||||||
|
it { is_expected.to belong_to(:conversation) }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'Custom Sort' do
|
||||||
|
let!(:account) { create(:account) }
|
||||||
|
let!(:user_1) { create(:user, email: 'agent2@example.com', account: account) }
|
||||||
|
let!(:user_2) { create(:user, email: 'agent11@example.com', account: account) }
|
||||||
|
let!(:conversation_1) { create(:conversation, created_at: DateTime.now - 8.days) }
|
||||||
|
let!(:conversation_2) { create(:conversation, created_at: DateTime.now - 6.days) }
|
||||||
|
let!(:conversation_3) { create(:conversation, created_at: DateTime.now - 9.days) }
|
||||||
|
let!(:conversation_4) { create(:conversation, created_at: DateTime.now - 10.days) }
|
||||||
|
|
||||||
|
let!(:mention_1) { create(:mention, account: account, conversation: conversation_1, user: user_1) }
|
||||||
|
let!(:mention_2) { create(:mention, account: account, conversation: conversation_2, user: user_1) }
|
||||||
|
let!(:mention_3) { create(:mention, account: account, conversation: conversation_3, user: user_1) }
|
||||||
|
|
||||||
|
it 'Sort mentioned conversations based on created_at' do
|
||||||
|
records = described_class.sort_on_created_at
|
||||||
|
|
||||||
|
expect(records.first.id).to eq(mention_1.id)
|
||||||
|
expect(records.first.conversation_id).to eq(conversation_1.id)
|
||||||
|
expect(records.last.conversation_id).to eq(conversation_3.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'Sort mentioned conversations based on last_user_message_at' do
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 2.days)
|
||||||
|
create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 6.days)
|
||||||
|
create(:message, conversation_id: conversation_1.id, message_type: :outgoing, created_at: DateTime.now - 7.days)
|
||||||
|
create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days)
|
||||||
|
create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days)
|
||||||
|
create(:message, conversation_id: conversation_3.id, message_type: :outgoing, created_at: DateTime.now - 9.days)
|
||||||
|
|
||||||
|
records = described_class.last_user_message_at
|
||||||
|
|
||||||
|
expect(records.first.id).to eq(mention_2.id)
|
||||||
|
expect(records.first.conversation_id).to eq(conversation_2.id)
|
||||||
|
expect(records.last.conversation_id).to eq(conversation_3.id)
|
||||||
|
expect(records.pluck(:id)).not_to include(conversation_4.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'Sort conversations based on mentioned_at' do
|
||||||
|
records = described_class.latest
|
||||||
|
|
||||||
|
expect(records.first.id).to eq(mention_3.id)
|
||||||
|
expect(records.first.conversation_id).to eq(conversation_3.id)
|
||||||
|
expect(records.last.conversation_id).to eq(conversation_1.id)
|
||||||
|
|
||||||
|
travel_to DateTime.now + 1.day
|
||||||
|
mention = create(:mention, account: account, conversation: conversation_2, user: user_2)
|
||||||
|
records = described_class.latest
|
||||||
|
|
||||||
|
expect(records.first.conversation_id).to eq(conversation_2.id)
|
||||||
|
expect(mention.created_at).to eq(DateTime.now)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user