diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 58013f128..ecbccfb88 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -51,6 +51,7 @@ class ConversationFinder filter_by_team if @team filter_by_labels if params[:labels] filter_by_query if params[:q] + filter_by_reply_status end def set_inboxes @@ -90,6 +91,10 @@ class ConversationFinder @conversations end + def filter_by_reply_status + @conversations = @conversations.where(first_reply_created_at: nil) if params[:reply_status] == 'unattended' + end + def filter_by_query allowed_message_types = [Message.message_types[:incoming], Message.message_types[:outgoing]] @conversations = conversations.joins(:messages).where('messages.content ILIKE :search', search: "%#{params[:q]}%") diff --git a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb index 8a874fc56..102e12477 100644 --- a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb @@ -15,6 +15,8 @@ RSpec.describe 'Conversations API', type: :request do context 'when it is an authenticated user' do let(:agent) { create(:user, account: account, role: :agent) } let(:conversation) { create(:conversation, account: account) } + let(:attended_conversation) { create(:conversation, account: account, first_reply_created_at: Time.now.utc) } + let(:unattended_conversation) { create(:conversation, account: account, first_reply_created_at: nil) } before do create(:inbox_member, user: agent, inbox: conversation.inbox) @@ -43,6 +45,22 @@ RSpec.describe 'Conversations API', type: :request do expect(body[:data][:meta][:all_count]).to eq(1) expect(body[:data][:payload].first[:messages]).to eq([]) end + + it 'returns unattended conversations' do + agent_1 = create(:user, account: account, role: :agent) + create(:inbox_member, user: agent_1, inbox: attended_conversation.inbox) + create(:inbox_member, user: agent_1, inbox: unattended_conversation.inbox) + + get "/api/v1/accounts/#{account.id}/conversations", + headers: agent_1.create_new_auth_token, + params: { reply_status: 'unattended' }, + as: :json + + expect(response).to have_http_status(:success) + body = JSON.parse(response.body, symbolize_names: true) + expect(body[:data][:meta][:all_count]).to eq(1) + expect(body[:data][:payload].count).to eq(1) + end end end