Feat: custom sort (#4864)
This commit is contained in:
@@ -621,4 +621,73 @@ RSpec.describe Conversation, type: :model do
|
||||
expect(conversation['additional_attributes']['referer']).to eq('https://www.chatwoot.com/')
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user