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
|
2022-06-22 05:34:42 +00:00
|
|
|
SORT_OPTIONS = {
|
|
|
|
latest: 'latest',
|
|
|
|
sort_on_created_at: 'sort_on_created_at',
|
|
|
|
last_user_message_at: 'last_user_message_at'
|
|
|
|
}.with_indifferent_access
|
2019-12-01 04:46:51 +00:00
|
|
|
|
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
|
2021-07-19 13:40:58 +00:00
|
|
|
set_up
|
2019-12-01 04:46:51 +00:00
|
|
|
|
2021-07-19 13:40:58 +00:00
|
|
|
mine_count, unassigned_count, all_count, = set_count_for_all_conversations
|
|
|
|
assigned_count = all_count - unassigned_count
|
2019-12-01 04:46:51 +00:00
|
|
|
|
|
|
|
filter_by_assignee_type
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-12-01 04:46:51 +00:00
|
|
|
{
|
2022-12-09 04:06:30 +00:00
|
|
|
conversations: @conversations,
|
|
|
|
contacts: @contacts,
|
|
|
|
messages: @messages
|
2019-12-01 04:46:51 +00:00
|
|
|
count: {
|
|
|
|
mine_count: mine_count,
|
2021-07-19 13:40:58 +00:00
|
|
|
assigned_count: assigned_count,
|
2019-12-01 04:46:51 +00:00
|
|
|
unassigned_count: unassigned_count,
|
|
|
|
all_count: all_count
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-07-19 13:40:58 +00:00
|
|
|
def set_up
|
|
|
|
set_inboxes
|
|
|
|
set_team
|
|
|
|
set_assignee_type
|
|
|
|
|
|
|
|
find_all_conversations
|
|
|
|
filter_by_status unless params[:q]
|
|
|
|
filter_by_team if @team
|
|
|
|
filter_by_labels if params[:labels]
|
|
|
|
filter_by_query if params[:q]
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def set_inboxes
|
2021-06-11 06:14:31 +00:00
|
|
|
@inbox_ids = if params[:inbox_id]
|
2022-02-04 02:25:28 +00:00
|
|
|
@current_user.assigned_inboxes.where(id: params[:inbox_id])
|
2021-06-11 06:14:31 +00:00
|
|
|
else
|
|
|
|
@current_user.assigned_inboxes.pluck(:id)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
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
|
|
|
|
|
2021-01-31 07:10:02 +00:00
|
|
|
def set_team
|
|
|
|
@team = current_account.teams.find(params[:team_id]) if params[:team_id]
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def find_all_conversations
|
2022-11-29 16:18:00 +00:00
|
|
|
@conversations = current_account.conversations.where(inbox_id: @inbox_ids)
|
|
|
|
filter_by_conversation_type if params[:conversation_type]
|
|
|
|
@conversations
|
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
|
2021-07-14 17:21:27 +00:00
|
|
|
when 'assigned'
|
2021-07-19 13:40:58 +00:00
|
|
|
@conversations = @conversations.assigned
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
@conversations
|
|
|
|
end
|
|
|
|
|
2022-11-29 16:18:00 +00:00
|
|
|
def filter_by_conversation_type
|
|
|
|
case @params[:conversation_type]
|
|
|
|
when 'mention'
|
|
|
|
conversation_ids = current_account.mentions.where(user: current_user).pluck(:conversation_id)
|
|
|
|
@conversations = @conversations.where(id: conversation_ids)
|
|
|
|
when 'unattended'
|
|
|
|
@conversations = @conversations.where(first_reply_created_at: nil)
|
|
|
|
end
|
|
|
|
@conversations
|
2022-05-22 07:38:41 +00:00
|
|
|
end
|
|
|
|
|
2020-09-23 09:57:41 +00:00
|
|
|
def filter_by_query
|
2022-12-09 04:06:30 +00:00
|
|
|
{
|
|
|
|
messages: filter_messages,
|
|
|
|
conversations: filter_conversations,
|
|
|
|
contacts: filter_contacts
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_conversations
|
|
|
|
conversation_ids = PgSearch.multisearch("#{@params[:q]}%").where(account_id: @current_account,
|
|
|
|
searchable_type: 'Conversation').pluck(:searchable_id)
|
|
|
|
@conversations = Conversation.where(id: conversation_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_messages
|
|
|
|
message_ids = PgSearch.multisearch("#{@params[:q]}%").where(account_id: @current_account, searchable_type: 'Message').pluck(:searchable_id)
|
|
|
|
@messages = Message.where(id: message_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_contacts
|
|
|
|
contact_ids = PgSearch.multisearch("#{@params[:q]}%").where(account_id: @current_account, searchable_type: 'Contact').pluck(:searchable_id)
|
|
|
|
@contacts = Contact.where(id: contact_ids)
|
2020-09-23 09:57:41 +00:00
|
|
|
end
|
|
|
|
|
2019-12-01 04:46:51 +00:00
|
|
|
def filter_by_status
|
2021-09-29 10:53:39 +00:00
|
|
|
return if params[:status] == 'all'
|
|
|
|
|
2019-12-01 04:46:51 +00:00
|
|
|
@conversations = @conversations.where(status: params[:status] || DEFAULT_STATUS)
|
|
|
|
end
|
|
|
|
|
2021-01-31 07:10:02 +00:00
|
|
|
def filter_by_team
|
|
|
|
@conversations = @conversations.where(team: @team)
|
|
|
|
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
|
2021-07-31 15:49:42 +00:00
|
|
|
params[:page] || 1
|
2019-12-01 04:46:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def conversations
|
2020-11-10 09:55:26 +00:00
|
|
|
@conversations = @conversations.includes(
|
2021-11-22 18:02:17 +00:00
|
|
|
:taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox
|
2020-11-10 09:55:26 +00:00
|
|
|
)
|
2022-06-22 05:34:42 +00:00
|
|
|
sort_by = SORT_OPTIONS[params[:sort_by]] || SORT_OPTIONS['latest']
|
|
|
|
@conversations.send(sort_by).page(current_page)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|