From d99997d17de90739e8b0ab4b559d195c99480f53 Mon Sep 17 00:00:00 2001 From: Jamie Wood Date: Thu, 11 May 2023 13:02:29 +0100 Subject: [PATCH] feat: Add ability to filter Conversations by underlying `source_id` (#6979) This change adds the ability to include a `source_id` param when querying the `/api/v1/accounts/{account_id}/conversations/search` endpoint. It restricts to results to only conversations related to a contact_inbox with the provided parameter. My motivation for adding this feature was to allow an external API to communicate with a specific conversation with only an awareness of the conversation `source_id` from the client. Co-authored-by: Sojan Jose --- app/finders/conversation_finder.rb | 20 +++++++++++++++++--- spec/finders/conversation_finder_spec.rb | 21 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 074eed461..7ec0a6d30 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -54,9 +54,10 @@ class ConversationFinder find_all_conversations filter_by_status unless params[:q] - filter_by_team if @team - filter_by_labels if params[:labels] - filter_by_query if params[:q] + filter_by_team + filter_by_labels + filter_by_query + filter_by_source_id end def set_inboxes @@ -107,6 +108,8 @@ class ConversationFinder end def filter_by_query + return unless params[:q] + allowed_message_types = [Message.message_types[:incoming], Message.message_types[:outgoing]] @conversations = conversations.joins(:messages).where('messages.content ILIKE :search', search: "%#{params[:q]}%") .where(messages: { message_type: allowed_message_types }).includes(:messages) @@ -121,13 +124,24 @@ class ConversationFinder end def filter_by_team + return unless @team + @conversations = @conversations.where(team: @team) end def filter_by_labels + return unless params[:labels] + @conversations = @conversations.tagged_with(params[:labels], any: true) end + def filter_by_source_id + return unless params[:source_id] + + @conversations = @conversations.joins(:contact_inbox) + @conversations = @conversations.where(contact_inboxes: { source_id: params[:source_id] }) + end + def set_count_for_all_conversations [ @conversations.assigned_to(current_user).count, diff --git a/spec/finders/conversation_finder_spec.rb b/spec/finders/conversation_finder_spec.rb index 15e4ff127..91a257e13 100644 --- a/spec/finders/conversation_finder_spec.rb +++ b/spec/finders/conversation_finder_spec.rb @@ -8,6 +8,7 @@ describe ::ConversationFinder do let!(:user_2) { create(:user, account: account) } let!(:admin) { create(:user, account: account, role: :administrator) } let!(:inbox) { create(:inbox, account: account, enable_auto_assignment: false) } + let!(:contact_inbox) { create(:contact_inbox, inbox: inbox, source_id: 'testing_source_id') } let!(:restricted_inbox) { create(:inbox, account: account) } before do @@ -16,7 +17,7 @@ describe ::ConversationFinder do create(:conversation, account: account, inbox: inbox, assignee: user_1) create(:conversation, account: account, inbox: inbox, assignee: user_1) create(:conversation, account: account, inbox: inbox, assignee: user_1, status: 'resolved') - create(:conversation, account: account, inbox: inbox, assignee: user_2) + create(:conversation, account: account, inbox: inbox, assignee: user_2, contact_inbox: contact_inbox) # unassigned conversation create(:conversation, account: account, inbox: inbox) Current.account = account @@ -127,6 +128,24 @@ describe ::ConversationFinder do end end + context 'with source_id' do + let(:params) { { source_id: 'testing_source_id' } } + + it 'filter conversations by source id' do + result = conversation_finder.perform + expect(result[:conversations].length).to be 1 + end + end + + context 'without source' do + let(:params) { {} } + + it 'returns conversations with any source' do + result = conversation_finder.perform + expect(result[:conversations].length).to be 4 + end + end + context 'with pagination' do let(:params) { { status: 'open', assignee_type: 'me', page: 1 } }