diff --git a/app/models/chat/session.rb b/app/models/chat/session.rb index bbd78215b..56a1f1dae 100644 --- a/app/models/chat/session.rb +++ b/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 diff --git a/app/models/chat/session/search_index.rb b/app/models/chat/session/search_index.rb index 08f09d624..a76f977ae 100644 --- a/app/models/chat/session/search_index.rb +++ b/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 + def search_index_attribute_lookup + attributes = super + return if !attributes - attributes[:tags] = tag_list + attributes['tags'] = tag_list - messages = Chat::Message.where(chat_session_id: id) - attributes['messages'] = [] - messages.each do |message| + messages = Chat::Message.where(chat_session_id: id) + attributes['messages'] = [] + messages.each do |message| - # lookup attributes of ref. objects (normally name and note) - message_attributes = message.search_index_attribute_lookup + # lookup attributes of ref. objects (normally name and note) + message_attributes = message.search_index_attribute_lookup - attributes['messages'].push message_attributes - end - - attributes + attributes['messages'].push message_attributes end + + attributes end end diff --git a/spec/factories/chat/session.rb b/spec/factories/chat/session.rb new file mode 100644 index 000000000..bf7afc21c --- /dev/null +++ b/spec/factories/chat/session.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :'chat/session' do + chat_id { Chat.pluck(:id).sample } + end +end diff --git a/spec/models/chat/session_spec.rb b/spec/models/chat/session_spec.rb new file mode 100644 index 000000000..0a6c7ec58 --- /dev/null +++ b/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 diff --git a/spec/requests/integration/monitoring_spec.rb b/spec/requests/integration/monitoring_spec.rb index 2c3cb497b..7be530add 100644 --- a/spec/requests/integration/monitoring_spec.rb +++ b/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