diff --git a/app/controllers/api/v1/accounts/conversations_controller.rb b/app/controllers/api/v1/accounts/conversations_controller.rb index ec107dfff..066710b73 100644 --- a/app/controllers/api/v1/accounts/conversations_controller.rb +++ b/app/controllers/api/v1/accounts/conversations_controller.rb @@ -11,6 +11,10 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro @conversations_count = result[:count] end + def text_search + @result = conversation_finder.search + end + def meta result = conversation_finder.perform @conversations_count = result[:count] diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 702045306..bb0c6daab 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -34,9 +34,7 @@ class ConversationFinder filter_by_assignee_type { - conversations: @conversations, - contacts: @contacts, - messages: @messages + conversations: conversations, count: { mine_count: mine_count, assigned_count: assigned_count, @@ -105,7 +103,7 @@ class ConversationFinder @conversations end - def filter_by_query + def search { messages: filter_messages, conversations: filter_conversations, @@ -113,9 +111,17 @@ class ConversationFinder } end + def filter_by_query + allowed_message_types = [Message.message_types[:incoming], Message.message_types[:outgoing]] + @conversations = conversations.joins(:messages).where('messages.content ILIKE :search', search: "%#{params[:q]}%") + .where(messages: { message_type: allowed_message_types }).includes(:messages) + .where('messages.content ILIKE :search', search: "%#{params[:q]}%") + .where(messages: { message_type: allowed_message_types }) + end + def filter_conversations conversation_ids = PgSearch.multisearch("#{@params[:q]}%").where(account_id: @current_account, - searchable_type: 'Conversation').pluck(:searchable_id) + searchable_type: 'Conversation').pluck(:searchable_id) @conversations = Conversation.where(id: conversation_ids) end diff --git a/app/models/concerns/multi_searchable_helpers.rb b/app/models/concerns/multi_searchable_helpers.rb index 78f201cdf..658718b44 100644 --- a/app/models/concerns/multi_searchable_helpers.rb +++ b/app/models/concerns/multi_searchable_helpers.rb @@ -39,7 +39,7 @@ module MultiSearchableHelpers end def contacts_pg_search_records - contacts_pg_search_records ||= PgSearch::Document.where( + PgSearch::Document.where( searchable_type: 'Contact', searchable_id: contact_id, account_id: account_id diff --git a/app/views/api/v1/accounts/conversations/text_search.json.jbuilder b/app/views/api/v1/accounts/conversations/text_search.json.jbuilder new file mode 100644 index 000000000..75397f6c0 --- /dev/null +++ b/app/views/api/v1/accounts/conversations/text_search.json.jbuilder @@ -0,0 +1,13 @@ +json.payload do + json.array! @result[:conversations] do |conversation| + json.partial! 'api/v1/models/conversation', formats: [:json], conversation: conversation + end + + json.array! @result[:contacts] do |contact| + json.partial! 'api/v1/models/contact', formats: [:json], resource: contact + end + + json.array! @result[:messages] do |message| + json.partial! 'api/v1/models/message', formats: [:json], message: message + end +end diff --git a/app/views/api/v1/models/_conversation.json.jbuilder b/app/views/api/v1/models/_conversation.json.jbuilder index 4159cd374..0951892a6 100644 --- a/app/views/api/v1/models/_conversation.json.jbuilder +++ b/app/views/api/v1/models/_conversation.json.jbuilder @@ -10,7 +10,7 @@ json.inbox do json.channel_type conversation.inbox.channel_type end json.messages do - json.array! conversation.messages.last(1) do |message| + json.array! conversation.messages do |message| json.content message.content json.id message.id json.sender_name message.sender.name if message.sender diff --git a/config/routes.rb b/config/routes.rb index 333070e55..1be648306 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,6 +71,7 @@ Rails.application.routes.draw do get :meta get :search post :filter + get :text_search end scope module: :conversations do resources :messages, only: [:index, :create, :destroy]