feat: Ability to search conversation with message content (#1265)

- API end point which takes message content as search parameter
- end point supports additional filtering with labels and inbox ids
- swagger doc
This commit is contained in:
Sojan Jose
2020-09-23 15:27:41 +05:30
committed by GitHub
parent bc8becf49c
commit 532331edb6
7 changed files with 122 additions and 9 deletions

View File

@@ -15,6 +15,12 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro
@conversations_count = result[:count]
end
def search
result = conversation_finder.perform
@conversations = result[:conversations]
@conversations_count = result[:count]
end
def create
@conversation = ::Conversation.create!(conversation_params)
end

View File

@@ -27,6 +27,7 @@ class ConversationFinder
find_all_conversations
filter_by_status
filter_by_labels if params[:labels]
filter_by_query if params[:q]
mine_count, unassigned_count, all_count = set_count_for_all_conversations
@@ -76,6 +77,12 @@ class ConversationFinder
@conversations
end
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
def filter_by_status
@conversations = @conversations.where(status: params[:status] || DEFAULT_STATUS)
end

View File

@@ -0,0 +1,22 @@
json.data do
json.meta do
json.mine_count @conversations_count[:mine_count]
json.unassigned_count @conversations_count[:unassigned_count]
json.all_count @conversations_count[:all_count]
end
json.payload do
json.array! @conversations do |conversation|
json.inbox_id conversation.inbox_id
json.messages conversation.messages
json.status conversation.status
json.muted conversation.muted?
json.can_reply conversation.can_reply?
json.timestamp conversation.messages.last.try(:created_at).try(:to_i)
json.contact_last_seen_at conversation.contact_last_seen_at.to_i
json.agent_last_seen_at conversation.agent_last_seen_at.to_i
json.additional_attributes conversation.additional_attributes
json.account_id conversation.account_id
json.labels conversation.label_list
end
end
end