Browse Source

Fixes #3095 - Not all attributes if chats are indexed by elasticsearch (chat.name and tags are missing)

Martin Edenhofer 4 years ago
parent
commit
71dcde5876

+ 3 - 1
app/models/chat/session.rb

@@ -7,7 +7,9 @@ class Chat::Session < ApplicationModel
   include Chat::Session::Assets
 
   # rubocop:disable Rails/InverseOf
-  has_many :messages, class_name: 'Chat::Message', foreign_key: 'chat_session_id'
+  has_many   :messages, class_name: 'Chat::Message', foreign_key: 'chat_session_id'
+  belongs_to :user,     class_name: 'User', optional: true
+  belongs_to :chat,     class_name: 'Chat'
   # rubocop:enable Rails/InverseOf
 
   before_create :generate_session_id

+ 12 - 16
app/models/chat/session/search_index.rb

@@ -2,9 +2,6 @@
 module Chat::Session::SearchIndex
   extend ActiveSupport::Concern
 
-  # methods defined here are going to extend the class, not the instance of it
-  class_methods do
-
 =begin
 
 lookup name of ref. objects
@@ -18,24 +15,23 @@ returns
 
 =end
 
-    def search_index_attribute_lookup
-      attributes = super
-      return if !attributes
-
-      attributes[:tags] = tag_list
+  def search_index_attribute_lookup
+    attributes = super
+    return if !attributes
 
-      messages = Chat::Message.where(chat_session_id: id)
-      attributes['messages'] = []
-      messages.each do |message|
+    attributes['tags'] = tag_list
 
-        # lookup attributes of ref. objects (normally name and note)
-        message_attributes = message.search_index_attribute_lookup
+    messages = Chat::Message.where(chat_session_id: id)
+    attributes['messages'] = []
+    messages.each do |message|
 
-        attributes['messages'].push message_attributes
-      end
+      # lookup attributes of ref. objects (normally name and note)
+      message_attributes = message.search_index_attribute_lookup
 
-      attributes
+      attributes['messages'].push message_attributes
     end
+
+    attributes
   end
 
 end

+ 5 - 0
spec/factories/chat/session.rb

@@ -0,0 +1,5 @@
+FactoryBot.define do
+  factory :'chat/session' do
+    chat_id { Chat.pluck(:id).sample }
+  end
+end

+ 24 - 0
spec/models/chat/session_spec.rb

@@ -0,0 +1,24 @@
+require 'rails_helper'
+
+RSpec.describe Chat::Session, type: :model do
+
+  describe '.search_index_attribute_lookup' do
+    subject(:chat_session) { create(:'chat/session', user: chat_user, chat: chat) }
+
+    let(:chat) { create(:chat) }
+    let(:chat_user) { create(:agent) }
+
+    it 'verify message attribute' do
+      expect(chat_session.search_index_attribute_lookup['messages']).to eq []
+    end
+
+    it 'verify user attribute' do
+      expect(chat_session.search_index_attribute_lookup['user']['id']).to eq chat_user.id
+    end
+
+    it 'verify chat attribute' do
+      expect(chat_session.search_index_attribute_lookup['chat']).to eq chat.name
+    end
+
+  end
+end

+ 4 - 4
spec/requests/integration/monitoring_spec.rb

@@ -595,7 +595,7 @@ RSpec.describe 'Monitoring', type: :request do
       expect(json_response['message']).to be_truthy
       expect(json_response['issues']).to be_truthy
       expect(json_response['healthy']).to eq(false)
-      expect( json_response['message']).to eq("Failed to run background job #1 'SearchIndexJob' 1 time(s) with 1 attempt(s).")
+      expect( json_response['message']).to eq("Failed to run background job #1 'SearchIndexAssociationsJob' 1 time(s) with 1 attempt(s).;Failed to run background job #2 'SearchIndexJob' 1 time(s) with 1 attempt(s).")
 
       # add another job
       manual_added = SearchIndexJob.perform_later('Ticket', 1)
@@ -609,7 +609,7 @@ RSpec.describe 'Monitoring', type: :request do
       expect(json_response['message']).to be_truthy
       expect(json_response['issues']).to be_truthy
       expect(json_response['healthy']).to eq(false)
-      expect( json_response['message']).to eq("Failed to run background job #1 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
+      expect( json_response['message']).to eq("Failed to run background job #1 'SearchIndexAssociationsJob' 1 time(s) with 1 attempt(s).;Failed to run background job #2 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
 
       # add another job
       dummy_class = Class.new(ApplicationJob) do
@@ -630,7 +630,7 @@ RSpec.describe 'Monitoring', type: :request do
       expect(json_response['message']).to be_truthy
       expect(json_response['issues']).to be_truthy
       expect(json_response['healthy']).to eq(false)
-      expect( json_response['message']).to eq("Failed to run background job #1 'Object' 1 time(s) with 5 attempt(s).;Failed to run background job #2 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
+      expect(json_response['message']).to eq("Failed to run background job #1 'Object' 1 time(s) with 5 attempt(s).;Failed to run background job #2 'SearchIndexAssociationsJob' 1 time(s) with 1 attempt(s).;Failed to run background job #3 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
 
       # reset settings
       Setting.set('es_url', prev_es_config)
@@ -649,7 +649,7 @@ RSpec.describe 'Monitoring', type: :request do
       expect(json_response['message']).to be_truthy
       expect(json_response['issues']).to be_truthy
       expect(json_response['healthy']).to eq(false)
-      expect(json_response['message']).to eq("13 failing background jobs;Failed to run background job #1 'Object' 8 time(s) with 40 attempt(s).;Failed to run background job #2 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
+      expect(json_response['message']).to eq("14 failing background jobs;Failed to run background job #1 'Object' 7 time(s) with 35 attempt(s).;Failed to run background job #2 'SearchIndexAssociationsJob' 1 time(s) with 1 attempt(s).;Failed to run background job #3 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
 
       # cleanup
       Delayed::Job.delete_all