2019-08-14 09:48:44 +00:00
|
|
|
class ConversationFinder
|
|
|
|
attr_reader :current_user, :current_account, :params
|
|
|
|
|
2019-12-01 04:46:51 +00:00
|
|
|
DEFAULT_STATUS = 'open'.freeze
|
|
|
|
|
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
|
2020-02-26 15:45:01 +00:00
|
|
|
# assignee_type if not given, take 'all'
|
2019-08-14 09:48:44 +00:00
|
|
|
# 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
|
2020-02-26 15:45:01 +00:00
|
|
|
# assignee_type, inbox_id, :status
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
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-12-01 04:46:51 +00:00
|
|
|
find_all_conversations
|
|
|
|
filter_by_status
|
2020-02-02 10:59:18 +00:00
|
|
|
filter_by_labels if params[:labels]
|
2020-09-23 09:57:41 +00:00
|
|
|
filter_by_query if params[:q]
|
2019-12-01 04:46:51 +00:00
|
|
|
|
|
|
|
mine_count, unassigned_count, all_count = set_count_for_all_conversations
|
|
|
|
|
|
|
|
filter_by_assignee_type
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-12-01 04:46:51 +00:00
|
|
|
{
|
|
|
|
conversations: conversations,
|
|
|
|
count: {
|
|
|
|
mine_count: mine_count,
|
|
|
|
unassigned_count: unassigned_count,
|
|
|
|
all_count: all_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
|
2020-02-26 15:45:01 +00:00
|
|
|
@assignee_type = params[:assignee_type]
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_all_conversations
|
2019-12-15 11:29:12 +00:00
|
|
|
@conversations = current_account.conversations.includes(
|
2020-09-19 07:16:34 +00:00
|
|
|
:assignee, :inbox, :taggings, contact: [:avatar_attachment]
|
2019-12-15 11:29:12 +00:00
|
|
|
).where(inbox_id: @inbox_ids)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_assignee_type
|
2020-09-08 05:54:08 +00:00
|
|
|
case @assignee_type
|
|
|
|
when 'me'
|
2019-08-14 09:48:44 +00:00
|
|
|
@conversations = @conversations.assigned_to(current_user)
|
2020-09-08 05:54:08 +00:00
|
|
|
when 'unassigned'
|
2019-08-14 09:48:44 +00:00
|
|
|
@conversations = @conversations.unassigned
|
|
|
|
end
|
|
|
|
@conversations
|
|
|
|
end
|
|
|
|
|
2020-09-23 09:57:41 +00:00
|
|
|
def filter_by_query
|
|
|
|
@conversations = @conversations.joins(:messages).where('messages.content LIKE :search',
|
|
|
|
search: "%#{params[:q]}%").includes(:messages).where('messages.content LIKE :search',
|
|
|
|
search: "%#{params[:q]}%")
|
|
|
|
end
|
|
|
|
|
2019-12-01 04:46:51 +00:00
|
|
|
def filter_by_status
|
|
|
|
@conversations = @conversations.where(status: params[:status] || DEFAULT_STATUS)
|
|
|
|
end
|
|
|
|
|
2020-02-02 10:59:18 +00:00
|
|
|
def filter_by_labels
|
|
|
|
@conversations = @conversations.tagged_with(params[:labels], any: true)
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def set_count_for_all_conversations
|
2019-12-01 04:46:51 +00:00
|
|
|
[
|
|
|
|
@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
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|