diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 7c6b5c725..702045306 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -34,7 +34,9 @@ class ConversationFinder filter_by_assignee_type { - conversations: conversations, + conversations: @conversations, + contacts: @contacts, + messages: @messages count: { mine_count: mine_count, assigned_count: assigned_count, @@ -104,8 +106,27 @@ class ConversationFinder end def filter_by_query - conversation_ids = PgSearch.multisearch("#{params[:q]}%").where(account_id: current_account).pluck(:conversation_id) - @conversations = Conversation.where(id: conversation_ids).includes(:messages) + { + 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) end def filter_by_status diff --git a/app/models/concerns/multi_searchable_helpers.rb b/app/models/concerns/multi_searchable_helpers.rb index ca410c7b0..78f201cdf 100644 --- a/app/models/concerns/multi_searchable_helpers.rb +++ b/app/models/concerns/multi_searchable_helpers.rb @@ -12,27 +12,37 @@ module MultiSearchableHelpers } } - def self.rebuild_pg_search_documents - return unless name == 'Contact' - - connection.execute <<~SQL.squish - INSERT INTO pg_search_documents (searchable_type, searchable_id, content, account_id, conversation_id, created_at, updated_at) - SELECT 'Contact' AS searchable_type, - contacts.id AS searchable_id, - CONCAT_WS(' ', contacts.email, contacts.name, contacts.phone_number, contacts.id, contacts.account_id) AS content, - contacts.account_id::int AS account_id, - conversations.id AS conversation_id, - now() AS created_at, - now() AS updated_at - FROM contacts - INNER JOIN conversations - ON conversations.contact_id = contacts.id - SQL - end - def update_contact_search_document - PgSearch::Document.create({ searchable_type: 'Contact', searchable_id: contact_id, conversation_id: id, account_id: account_id, - content: "#{contact.email} #{contact.name} #{contact.phone_number} #{contact.id}" }) + return if contact_pg_search_record.present? + + initialize_contact_pg_search_record.update!( + content: "#{contact.id} #{contact.email} #{contact.name} #{contact.phone_number}", + conversation_id: id + ) end end + + def contact_pg_search_record + contacts_pg_search_records.find_by(conversation_id: id) + end + + def initialize_contact_pg_search_record + record = contacts_pg_search_records.find_by(conversation_id: nil) + + return record if record.present? + + PgSearch::Document.new( + searchable_type: 'Contact', + searchable_id: contact_id, + account_id: account_id + ) + end + + def contacts_pg_search_records + contacts_pg_search_records ||= PgSearch::Document.where( + searchable_type: 'Contact', + searchable_id: contact_id, + account_id: account_id + ) + end end diff --git a/app/models/contact.rb b/app/models/contact.rb index 807892b81..181b54e20 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -29,10 +29,10 @@ class Contact < ApplicationRecord include PgSearch::Model include MultiSearchableHelpers - # multisearchable( - # against: [:email, :name, :phone_number, :id], - # additional_attributes: -> (contact) { { conversation_id: nil, account_id: contact.account_id } } - # ) + multisearchable( + against: [:id, :email, :name, :phone_number], + additional_attributes: ->(contact) { { conversation_id: nil, account_id: contact.account_id } } + ) validates :account_id, presence: true validates :email, allow_blank: true, uniqueness: { scope: [:account_id], case_sensitive: false }, @@ -147,6 +147,24 @@ class Contact < ApplicationRecord email_format end + def self.rebuild_pg_search_documents + return super unless name == 'Contact' + + connection.execute <<~SQL.squish + INSERT INTO pg_search_documents (searchable_type, searchable_id, content, account_id, conversation_id, created_at, updated_at) + SELECT 'Contact' AS searchable_type, + contacts.id AS searchable_id, + CONCAT_WS(' ', contacts.email, contacts.name, contacts.phone_number, contacts.id, contacts.account_id) AS content, + contacts.account_id::int AS account_id, + conversations.id AS conversation_id, + now() AS created_at, + now() AS updated_at + FROM contacts + LEFT OUTER JOIN conversations + ON conversations.contact_id = contacts.id + SQL + end + private def ip_lookup diff --git a/app/models/conversation.rb b/app/models/conversation.rb index bad4e7851..2a603db5b 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -99,7 +99,7 @@ class Conversation < ApplicationRecord after_update_commit :execute_after_update_commit_callbacks after_create_commit :notify_conversation_creation - after_create :update_contact_search_document + after_create_commit :update_contact_search_document, if: :contact_id? after_commit :set_display_id, unless: :display_id? delegate :auto_resolve_duration, to: :account