2019-08-14 09:48:44 +00:00
|
|
|
class ConversationFinder
|
|
|
|
attr_reader :current_user, :current_account, :params
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
ASSIGNEE_TYPES = { me: 0, unassigned: 1, all: 2 }.freeze
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
ASSIGNEE_TYPES_BY_ID = ASSIGNEE_TYPES.invert
|
|
|
|
ASSIGNEE_TYPES_BY_ID.default = :me
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
# assumptions
|
2019-08-14 09:48:44 +00:00
|
|
|
# inbox_id if not given, take from all conversations, else specific to inbox
|
|
|
|
# assignee_type if not given, take 'me'
|
|
|
|
# conversation_status if not given, take 'open'
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
# response of this class will be of type
|
|
|
|
# {conversations: [array of conversations], count: {open: count, resolved: count}}
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
# params
|
2019-08-14 09:48:44 +00:00
|
|
|
# assignee_type_id, inbox_id, :conversation_status_id,
|
|
|
|
|
|
|
|
def initialize(current_user, params)
|
|
|
|
@current_user = current_user
|
|
|
|
@current_account = current_user.account
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
set_inboxes
|
|
|
|
set_assignee_type
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
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
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
{ conversations: @conversations.latest,
|
|
|
|
count: { open: open_count, resolved: resolved_count } }
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_inboxes
|
|
|
|
if params[:inbox_id]
|
|
|
|
@inbox_ids = current_account.inboxes.where(id: params[:inbox_id])
|
|
|
|
else
|
|
|
|
if @current_user.administrator?
|
|
|
|
@inbox_ids = current_account.inboxes.pluck(:id)
|
|
|
|
elsif @current_user.agent?
|
|
|
|
@inbox_ids = @current_user.assigned_inboxes.pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_assignee_type
|
|
|
|
@assignee_type_id = ASSIGNEE_TYPES[ASSIGNEE_TYPES_BY_ID[params[:assignee_type_id].to_i]]
|
2019-10-20 08:47:26 +00:00
|
|
|
# ente budhiparamaya neekam kandit enthu tonunu? ;)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_all_conversations
|
|
|
|
@conversations = current_account.conversations.where(inbox_id: @inbox_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_assignee_type
|
|
|
|
if @assignee_type_id == ASSIGNEE_TYPES[:me]
|
|
|
|
@conversations = @conversations.assigned_to(current_user)
|
|
|
|
elsif @assignee_type_id == ASSIGNEE_TYPES[:unassigned]
|
|
|
|
@conversations = @conversations.unassigned
|
|
|
|
elsif @assignee_type_id == ASSIGNEE_TYPES[:all]
|
|
|
|
@conversations
|
|
|
|
end
|
|
|
|
@conversations
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_count_for_all_conversations
|
|
|
|
[@conversations.open.count, @conversations.resolved.count]
|
|
|
|
end
|
|
|
|
end
|