diff --git a/app/controllers/api/v1/accounts/conversations_controller.rb b/app/controllers/api/v1/accounts/conversations_controller.rb index 066710b73..724bda430 100644 --- a/app/controllers/api/v1/accounts/conversations_controller.rb +++ b/app/controllers/api/v1/accounts/conversations_controller.rb @@ -2,8 +2,8 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro include Events::Types include DateRangeHelper - before_action :conversation, except: [:index, :meta, :search, :create, :filter] - before_action :inbox, :contact, :contact_inbox, only: [:create] + before_action :conversation, except: [:index, :meta, :search, :create, :filter, :text_search] + before_action :inbox, :contact, :contact_inbox, :text_search, only: [:create] def index result = conversation_finder.perform @@ -12,7 +12,7 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro end def text_search - @result = conversation_finder.search + @result = conversation_finder.text_search end def meta diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index bb0c6daab..4eec19c12 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -44,6 +44,14 @@ class ConversationFinder } end + def text_search + { + messages: filter_messages, + conversations: filter_conversations, + contacts: filter_contacts + } + end + private def set_up @@ -103,12 +111,12 @@ class ConversationFinder @conversations end - def search - { - messages: filter_messages, - conversations: filter_conversations, - contacts: filter_contacts - } + 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_by_query diff --git a/app/views/api/v1/accounts/conversations/text_search.json.jbuilder b/app/views/api/v1/accounts/conversations/text_search.json.jbuilder index 75397f6c0..b49bd445e 100644 --- a/app/views/api/v1/accounts/conversations/text_search.json.jbuilder +++ b/app/views/api/v1/accounts/conversations/text_search.json.jbuilder @@ -1,13 +1,19 @@ json.payload do - json.array! @result[:conversations] do |conversation| - json.partial! 'api/v1/models/conversation', formats: [:json], conversation: conversation + json.conversations do + json.array! @result[:conversations] do |conversation| + json.partial! 'api/v1/models/conversation', formats: [:json], conversation: conversation + end end - json.array! @result[:contacts] do |contact| - json.partial! 'api/v1/models/contact', formats: [:json], resource: contact + json.contacts do + json.array! @result[:contacts] do |contact| + json.partial! 'api/v1/models/contact', formats: [:json], resource: contact + end end - json.array! @result[:messages] do |message| - json.partial! 'api/v1/models/message', formats: [:json], message: message + json.messages do + json.array! @result[:messages] do |message| + json.partial! 'api/v1/models/message', formats: [:json], message: message + end end end