diff --git a/app/models/concerns/sort_handler.rb b/app/models/concerns/sort_handler.rb index 135912aad..61950f32e 100644 --- a/app/models/concerns/sort_handler.rb +++ b/app/models/concerns/sort_handler.rb @@ -21,5 +21,13 @@ module SortHandler 'grouped_conversations.message_type', 'grouped_conversations.created_at ASC' ) end + + def self.sort_on_priority + order( + Arel::Nodes::SqlLiteral.new( + sanitize_sql_for_order('CASE WHEN priority IS NULL THEN 0 ELSE priority END DESC, last_activity_at DESC') + ) + ) + end end end diff --git a/lib/seeders/account_seeder.rb b/lib/seeders/account_seeder.rb index 7af6110d2..db8d31c37 100644 --- a/lib/seeders/account_seeder.rb +++ b/lib/seeders/account_seeder.rb @@ -90,6 +90,7 @@ class Seeders::AccountSeeder inbox: contact_inbox.inbox, assignee: assignee) create_messages(conversation: conversation, messages: conversation_data['messages']) conversation.update_labels(conversation_data[:labels]) if conversation_data[:labels].present? + conversation.update!(priority: conversation_data[:priority]) if conversation_data[:priority].present? end def create_messages(conversation:, messages:) diff --git a/lib/seeders/seed_data.yml b/lib/seeders/seed_data.yml index 5426f4c9a..8390aa48b 100644 --- a/lib/seeders/seed_data.yml +++ b/lib/seeders/seed_data.yml @@ -308,6 +308,7 @@ contacts: conversations: - channel: Channel::Email source_id: "mpetrukd@wunderground.test" + priority: urgent messages: - message_type: incoming content: hello world @@ -316,6 +317,7 @@ contacts: gender: 'male' conversations: - channel: Channel::FacebookPage + priority: high messages: - message_type: incoming content: "Hey there,I need some help with billing, my card is not working on the website." @@ -324,6 +326,7 @@ contacts: gender: 'female' conversations: - channel: Channel::WebWidget + priority: high assignee: michael_scott@paperlayer.test messages: - message_type: incoming @@ -333,6 +336,7 @@ contacts: gender: 'female' conversations: - channel: Channel::Whatsapp + priority: high source_id: "1223423567" labels: - billing @@ -347,6 +351,7 @@ contacts: gender: 'male' conversations: - channel: Channel::WebWidget + priority: medium labels: - software - ops-handover @@ -358,6 +363,7 @@ contacts: gender: 'female' conversations: - channel: Channel::WebWidget + priority: low assignee: michael_scott@paperlayer.test labels: - billing @@ -371,6 +377,7 @@ contacts: gender: 'female' conversations: - channel: Channel::Email + priority: urgent source_id: "cmathersonj@va.test" assignee: michael_scott@paperlayer.test labels: diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 163c27452..bf7031cc3 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -691,15 +691,17 @@ RSpec.describe Conversation, type: :model do 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) } + let!(:conversation_7) { create(:conversation, created_at: DateTime.now - 10.days, last_activity_at: DateTime.now - 13.days) } + let!(:conversation_6) { create(:conversation, created_at: DateTime.now - 10.days, last_activity_at: DateTime.now - 10.days) } + let!(:conversation_5) { create(:conversation, created_at: DateTime.now - 10.days, last_activity_at: DateTime.now - 12.days, priority: :urgent) } + let!(:conversation_4) { create(:conversation, created_at: DateTime.now - 10.days, last_activity_at: DateTime.now - 10.days, priority: :urgent) } + let!(:conversation_3) { create(:conversation, created_at: DateTime.now - 9.days, last_activity_at: DateTime.now - 9.days, priority: :low) } + let!(:conversation_2) { create(:conversation, created_at: DateTime.now - 6.days, last_activity_at: DateTime.now - 6.days, priority: :high) } + let!(:conversation_1) { create(:conversation, created_at: DateTime.now - 8.days, last_activity_at: DateTime.now - 8.days, priority: :medium) } 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.first.id).to eq(conversation_7.id) expect(records.last.id).to eq(conversation_2.id) end @@ -778,5 +780,28 @@ RSpec.describe Conversation, type: :model do expect(records.first.id).to eq(conversation_3.id) end end + + context 'when sort on priority' do + it 'Sort conversations with the following order high > medium > low > nil' do + # ensure they are not pre-sorted + records = described_class.sort_on_created_at + expect(records.pluck(:priority)).not_to eq(['urgent', 'urgent', 'high', 'medium', 'low', nil, nil]) + + records = described_class.sort_on_priority + expect(records.pluck(:priority)).to eq(['urgent', 'urgent', 'high', 'medium', 'low', nil, nil]) + end + + it 'sorts conversation with last_activity for the same priority' do + records = described_class.where(priority: 'urgent').sort_on_priority + # ensure that the conversation 4 last_activity_at is more recent than conversation 5 + expect(conversation_4.last_activity_at > conversation_5.last_activity_at).to be(true) + expect(records.pluck(:priority, :id)).to eq([['urgent', conversation_4.id], ['urgent', conversation_5.id]]) + + records = described_class.where(priority: nil).sort_on_priority + # ensure that the conversation 6 last_activity_at is more recent than conversation 7 + expect(conversation_6.last_activity_at > conversation_7.last_activity_at).to be(true) + expect(records.pluck(:priority, :id)).to eq([[nil, conversation_6.id], [nil, conversation_7.id]]) + end + end end end