Skip to content

Commit

Permalink
Chore: API to get conversations count (#808)
Browse files Browse the repository at this point in the history
Addresses: #497
  • Loading branch information
sojan-official committed May 3, 2020
1 parent d8131e4 commit db6e3fb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/api/v1/accounts/conversations_controller.rb
Expand Up @@ -8,6 +8,11 @@ def index
@conversations_count = result[:count]
end

def meta
result = conversation_finder.perform
@conversations_count = result[:count]
end

def create
@conversation = ::Conversation.create!(conversation_params)
end
Expand Down
7 changes: 7 additions & 0 deletions 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
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -41,6 +41,7 @@
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]
Expand Down
28 changes: 28 additions & 0 deletions spec/controllers/api/v1/accounts/conversations_controller_spec.rb
Expand Up @@ -31,6 +31,34 @@
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) }

Expand Down

0 comments on commit db6e3fb

Please sign in to comment.