From 84799fd0a1763bfe36ffecf379fc23c95c8b0107 Mon Sep 17 00:00:00 2001 From: Subin T P Date: Sun, 1 Dec 2019 10:16:51 +0530 Subject: [PATCH] [#247] Filters conversation by status and paginate conversations (#284) * [#247] Filters conversation by status * Fixes conversation finder specs * [#248] Paginates conversation * Use method name in description * Move page to default param, add filters on frontend * Fix code climate issues --- .../api/v1/conversations_controller.rb | 1 - app/finders/conversation_finder.rb | 41 ++++++++++--- .../dashboard/api/inbox/conversation.js | 6 +- .../dashboard/components/ChatList.vue | 61 ++++++++----------- .../components/widgets/ChatTypeTabs.vue | 6 +- .../widgets/conversation/ChatFilter.vue | 14 +++-- .../dashboard/i18n/locale/en/chatlist.json | 2 +- .../store/modules/conversations/getters.js | 1 + .../store/modules/conversations/index.js | 27 ++++---- .../api/v1/conversations/index.json.jbuilder | 6 +- spec/factories/conversations.rb | 2 +- spec/factories/inbox_members.rb | 8 +++ spec/finders/conversation_finder_spec.rb | 49 +++++++++++++++ 13 files changed, 155 insertions(+), 69 deletions(-) create mode 100644 spec/factories/inbox_members.rb create mode 100644 spec/finders/conversation_finder_spec.rb diff --git a/app/controllers/api/v1/conversations_controller.rb b/app/controllers/api/v1/conversations_controller.rb index 1c03e8ee0..cc179a75d 100644 --- a/app/controllers/api/v1/conversations_controller.rb +++ b/app/controllers/api/v1/conversations_controller.rb @@ -11,7 +11,6 @@ class Api::V1::ConversationsController < Api::BaseController result = conversation_finder.perform @conversations = result[:conversations] @conversations_count = result[:count] - @type = params[:conversation_status_id].to_i end def show diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 026b9658f..cc41bb557 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -6,6 +6,8 @@ class ConversationFinder ASSIGNEE_TYPES_BY_ID = ASSIGNEE_TYPES.invert ASSIGNEE_TYPES_BY_ID.default = :me + DEFAULT_STATUS = 'open'.freeze + # assumptions # inbox_id if not given, take from all conversations, else specific to inbox # assignee_type if not given, take 'me' @@ -15,7 +17,7 @@ class ConversationFinder # {conversations: [array of conversations], count: {open: count, resolved: count}} # params - # assignee_type_id, inbox_id, :conversation_status_id, + # assignee_type_id, inbox_id, :status def initialize(current_user, params) @current_user = current_user @@ -27,12 +29,21 @@ class ConversationFinder set_inboxes set_assignee_type - find_all_conversations # find all with the inbox - filter_by_assignee_type # filter by assignee - open_count, resolved_count = set_count_for_all_conversations # fetch count for both before filtering by status + find_all_conversations + filter_by_status - { conversations: @conversations.latest, - count: { open: open_count, resolved: resolved_count } } + mine_count, unassigned_count, all_count = set_count_for_all_conversations + + filter_by_assignee_type + + { + conversations: conversations, + count: { + mine_count: mine_count, + unassigned_count: unassigned_count, + all_count: all_count + } + } end private @@ -69,7 +80,23 @@ class ConversationFinder @conversations end + def filter_by_status + @conversations = @conversations.where(status: params[:status] || DEFAULT_STATUS) + end + def set_count_for_all_conversations - [@conversations.open.count, @conversations.resolved.count] + [ + @conversations.assigned_to(current_user).count, + @conversations.unassigned.count, + @conversations.count + ] + end + + def current_page + params[:page] + end + + def conversations + current_page ? @conversations.latest.page(current_page) : @conversations.latest end end diff --git a/app/javascript/dashboard/api/inbox/conversation.js b/app/javascript/dashboard/api/inbox/conversation.js index c43f41c70..b444d0e17 100644 --- a/app/javascript/dashboard/api/inbox/conversation.js +++ b/app/javascript/dashboard/api/inbox/conversation.js @@ -6,12 +6,12 @@ class ConversationApi extends ApiClient { super('conversations'); } - get({ inboxId, convStatus, assigneeStatus }) { + get({ inboxId, status, assigneeType }) { return axios.get(this.url, { params: { inbox_id: inboxId, - conversation_status_id: convStatus, - assignee_type_id: assigneeStatus, + status, + assignee_type_id: assigneeType, }, }); } diff --git a/app/javascript/dashboard/components/ChatList.vue b/app/javascript/dashboard/components/ChatList.vue index f3b357110..89106b915 100644 --- a/app/javascript/dashboard/components/ChatList.vue +++ b/app/javascript/dashboard/components/ChatList.vue @@ -1,7 +1,5 @@