fix: specs and PR feedbacks

This commit is contained in:
Tejaswini Chile 2022-12-19 22:29:48 +05:30
parent 5bb4c12e6b
commit 56488c6bae
4 changed files with 36 additions and 3 deletions

View file

@ -0,0 +1,9 @@
class Conversations::MultiSearchJob < ApplicationJob
queue_as :default
def perform
Contact.rebuild_pg_search_documents
PgSearch::Multisearch.rebuild(Conversation)
PgSearch::Multisearch.rebuild(Message)
end
end

View file

@ -147,6 +147,8 @@ class Contact < ApplicationRecord
email_format email_format
end end
# NOTE: To add multi search records with conversation_id associated to contacts for previously added records.
# We can not find conversation_id from contacts directly so we added this joins here.
def self.rebuild_pg_search_documents def self.rebuild_pg_search_documents
return super unless name == 'Contact' return super unless name == 'Contact'

View file

@ -1,8 +1,6 @@
class EnableMultiSearchable < ActiveRecord::Migration[6.1] class EnableMultiSearchable < ActiveRecord::Migration[6.1]
def up def up
Contact.rebuild_pg_search_documents ::Conversations::MultiSearchJob.perform_now
PgSearch::Multisearch.rebuild(Conversation)
PgSearch::Multisearch.rebuild(Message)
execute 'CREATE EXTENSION IF NOT EXISTS pg_trgm;' execute 'CREATE EXTENSION IF NOT EXISTS pg_trgm;'
end end

View file

@ -59,5 +59,29 @@ describe ::TextSearch do
expect(result[:conversations].length).to be 1 expect(result[:conversations].length).to be 1
end end
end end
context 'when create records in tables including multi search' do
let(:contact) { create(:contact, name: 'Welma', account_id: account.id, email: 'welma@scoobydoo.com') }
let(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: user_1, status: 'open', contact_id: contact.id) }
it 'conversation creation pg search records' do
contact_search_record = PgSearch::Document.find_by(searchable_id: contact.id, searchable_type: contact.class.name)
conversation_search_record = PgSearch::Document.find_by(searchable_id: conversation.id, searchable_type: conversation.class.name)
expect(contact_search_record).to be_present
expect(conversation_search_record).to be_present
end
it 'conversation deletion deletes pg search records' do
contact.destroy!
conversation.destroy!
contact_search_record = PgSearch::Document.find_by(searchable_id: contact.id, searchable_type: contact.class.name)
conversation_search_record = PgSearch::Document.find_by(searchable_id: conversation.id, searchable_type: conversation.class.name)
expect(contact_search_record).to be_nil
expect(conversation_search_record).to be_nil
end
end
end end
end end