From b3264a0d7b4b3e96e2e766f13a4b7771743ed435 Mon Sep 17 00:00:00 2001 From: Subin T P Date: Sun, 2 Feb 2020 16:29:18 +0530 Subject: [PATCH] Feature: API to get most used labels (#447) * Add most-used labels API * Filter conversation by labels * Move match_all to any query Co-authored-by: Pranav Raj S --- app/controllers/api/v1/labels_controller.rb | 4 +++ app/finders/conversation_finder.rb | 6 ++++- .../api/v1/labels/most_used.json.jbuilder | 3 +++ config/routes.rb | 6 ++++- .../api/v1/labels_controller_spec.rb | 25 +++++++++++++++++++ spec/finders/conversation_finder_spec.rb | 12 +++++++++ 6 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/views/api/v1/labels/most_used.json.jbuilder diff --git a/app/controllers/api/v1/labels_controller.rb b/app/controllers/api/v1/labels_controller.rb index 5a06ae314..4426b7018 100644 --- a/app/controllers/api/v1/labels_controller.rb +++ b/app/controllers/api/v1/labels_controller.rb @@ -2,4 +2,8 @@ class Api::V1::LabelsController < Api::BaseController def index # list all labels in account @labels = current_account.all_conversation_tags end + + def most_used + @labels = ActsAsTaggableOn::Tag.most_used(params[:count] || 10) + end end diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index b6c6f966c..f4e114bf4 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -31,6 +31,7 @@ class ConversationFinder find_all_conversations filter_by_status + filter_by_labels if params[:labels] mine_count, unassigned_count, all_count = set_count_for_all_conversations @@ -62,7 +63,6 @@ class ConversationFinder def set_assignee_type @assignee_type_id = ASSIGNEE_TYPES[ASSIGNEE_TYPES_BY_ID[params[:assignee_type_id].to_i]] - # ente budhiparamaya neekam kandit enthu tonunu? ;) end def find_all_conversations @@ -86,6 +86,10 @@ class ConversationFinder @conversations = @conversations.where(status: params[:status] || DEFAULT_STATUS) end + def filter_by_labels + @conversations = @conversations.tagged_with(params[:labels], any: true) + end + def set_count_for_all_conversations [ @conversations.assigned_to(current_user).count, diff --git a/app/views/api/v1/labels/most_used.json.jbuilder b/app/views/api/v1/labels/most_used.json.jbuilder new file mode 100644 index 000000000..c411b47ed --- /dev/null +++ b/app/views/api/v1/labels/most_used.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.labels @labels +end diff --git a/config/routes.rb b/config/routes.rb index be73c62e7..1b67f0bd4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,7 +39,11 @@ Rails.application.routes.draw do resources :accounts, only: [:create] resources :inboxes, only: [:index, :destroy] resources :agents, except: [:show, :edit, :new] - resources :labels, only: [:index] + resources :labels, only: [:index] do + collection do + get :most_used + end + end resources :canned_responses, except: [:show, :edit, :new] resources :inbox_members, only: [:create, :show], param: :inbox_id resources :facebook_indicators, only: [] do diff --git a/spec/controllers/api/v1/labels_controller_spec.rb b/spec/controllers/api/v1/labels_controller_spec.rb index ab26b048f..dbee1e19b 100644 --- a/spec/controllers/api/v1/labels_controller_spec.rb +++ b/spec/controllers/api/v1/labels_controller_spec.rb @@ -31,4 +31,29 @@ RSpec.describe 'Label API', type: :request do end end end + + describe 'GET /api/v1/labels/most_used' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get '/api/v1/labels' + + 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) } + + it 'returns most used labels' do + get '/api/v1/labels/most_used', + headers: agent.create_new_auth_token, + params: { count: 1 }, + as: :json + + expect(response).to have_http_status(:success) + expect(response.body).to include('label1') + expect(response.body).not_to include('label2') + end + end + end end diff --git a/spec/finders/conversation_finder_spec.rb b/spec/finders/conversation_finder_spec.rb index 24529efb8..f85615f28 100644 --- a/spec/finders/conversation_finder_spec.rb +++ b/spec/finders/conversation_finder_spec.rb @@ -36,6 +36,18 @@ describe ::ConversationFinder do end end + context 'with labels' do + let(:params) { { labels: ['resolved'] } } + + it 'filter conversations by labels' do + conversation = inbox.conversations.first + conversation.update_labels('resolved') + + result = conversation_finder.perform + expect(result[:conversations].count).to be 1 + end + end + context 'with pagination' do let(:params) { { status: 'open', assignee_type_id: 0, page: 1 } }