From db6e3fb43e425e99b3d22e37fe0ae84be1074f3b Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Sun, 3 May 2020 12:15:10 +0530 Subject: [PATCH] Chore: API to get conversations count (#808) Addresses: #497 --- .../v1/accounts/conversations_controller.rb | 5 ++++ .../accounts/conversations/meta.json.jbuilder | 7 +++++ config/routes.rb | 1 + .../accounts/conversations_controller_spec.rb | 28 +++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 app/views/api/v1/accounts/conversations/meta.json.jbuilder diff --git a/app/controllers/api/v1/accounts/conversations_controller.rb b/app/controllers/api/v1/accounts/conversations_controller.rb index 3050c8ab7..dea9c0849 100644 --- a/app/controllers/api/v1/accounts/conversations_controller.rb +++ b/app/controllers/api/v1/accounts/conversations_controller.rb @@ -8,6 +8,11 @@ class Api::V1::Accounts::ConversationsController < Api::BaseController @conversations_count = result[:count] end + def meta + result = conversation_finder.perform + @conversations_count = result[:count] + end + def create @conversation = ::Conversation.create!(conversation_params) end diff --git a/app/views/api/v1/accounts/conversations/meta.json.jbuilder b/app/views/api/v1/accounts/conversations/meta.json.jbuilder new file mode 100644 index 000000000..8895ad24d --- /dev/null +++ b/app/views/api/v1/accounts/conversations/meta.json.jbuilder @@ -0,0 +1,7 @@ +json.data do + json.meta do + json.mine_count @conversations_count[:mine_count] + json.unassigned_count @conversations_count[:unassigned_count] + json.all_count @conversations_count[:all_count] + end +end diff --git a/config/routes.rb b/config/routes.rb index abf31fbad..051aca254 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,7 @@ Rails.application.routes.draw do resource :twilio_channel, only: [:create] end resources :conversations, only: [:index, :create, :show] do + get 'meta', on: :collection scope module: :conversations do resources :messages, only: [:index, :create] resources :assignments, only: [:create] diff --git a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb index 58c1f60b6..4d3397047 100644 --- a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb @@ -31,6 +31,34 @@ RSpec.describe 'Conversations API', type: :request do end end + describe 'GET /api/v1/accounts/{account.id}/conversations/meta' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get "/api/v1/accounts/#{account.id}/conversations/meta" + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:agent) { create(:user, account: account, role: :agent) } + + before do + conversation = create(:conversation, account: account) + create(:inbox_member, user: agent, inbox: conversation.inbox) + end + + it 'returns all conversations counts' do + get "/api/v1/accounts/#{account.id}/conversations/meta", + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(JSON.parse(response.body, symbolize_names: true)[:data][:meta][:all_count]).to eq(1) + end + end + end + describe 'GET /api/v1/accounts/{account.id}/conversations/:id' do let(:conversation) { create(:conversation, account: account) }