Просмотр исходного кода

Fixes #5392 - New checklists will not be indexed according to regular conversion in ticket records

Mantas Masalskis 4 месяцев назад
Родитель
Сommit
762d7688b0

+ 1 - 0
app/models/checklist.rb

@@ -4,6 +4,7 @@ class Checklist < ApplicationModel
   include HasDefaultModelUserRelations
   include ChecksClientNotification
   include HasHistory
+  include Checklist::SearchIndex
   include Checklist::TriggersSubscriptions
   include Checklist::Assets
   include CanChecklistSortedItems

+ 17 - 0
app/models/checklist/search_index.rb

@@ -0,0 +1,17 @@
+# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
+
+class Checklist
+  module SearchIndex
+    extend ActiveSupport::Concern
+
+    def search_index_attribute_lookup(include_references: true)
+      attributes = super
+
+      attributes['items'] = items.map do |item|
+        item.search_index_attribute_lookup(include_references: include_references)
+      end
+
+      attributes
+    end
+  end
+end

+ 26 - 28
app/models/ticket/search_index.rb

@@ -15,7 +15,9 @@ module Ticket::SearchIndex
     attributes['mention_user_ids'] = mentions.pluck(:user_id)
 
     # checklists
-    add_checklist(attributes)
+    if checklist
+      attributes['checklist'] = checklist.search_index_attribute_lookup(include_references: false)
+    end
 
     # current payload size
     total_size_current = 0
@@ -25,18 +27,7 @@ module Ticket::SearchIndex
     Ticket::Article.where(ticket_id: id).limit(1000).find_each(batch_size: 50).each do |article|
 
       # lookup attributes of ref. objects (normally name and note)
-      article_attributes = article.search_index_attribute_lookup(include_references: false)
-
-      # remove note needed attributes
-      ignore = %w[message_id_md5 ticket]
-      ignore.each do |attribute|
-        article_attributes.delete(attribute)
-      end
-
-      # index raw text body
-      if article_attributes['content_type'] && article_attributes['content_type'] == 'text/html' && article_attributes['body']
-        article_attributes['body'] = article_attributes['body'].html2text
-      end
+      article_attributes = search_index_article_attributes(article)
 
       article_attributes_payload_size = article_attributes.to_json.bytes.size
 
@@ -59,13 +50,7 @@ module Ticket::SearchIndex
         # add attachment size to totel payload size
         total_size_current += attachment.content.bytes.size
 
-        data = {
-          'size'     => attachment.size,
-          '_name'    => attachment.filename,
-          '_content' => Base64.encode64(attachment.content).delete("\n"),
-        }
-
-        article_attributes['attachment'].push data
+        article_attributes['attachment'].push search_index_article_attachment_attributes(attachment)
       end
 
       attributes['article'].push article_attributes
@@ -112,18 +97,31 @@ module Ticket::SearchIndex
     false
   end
 
-  def add_checklist(attributes)
-    return if !checklist
+  def search_index_article_attributes(article)
 
-    attrs = {}
+    # lookup attributes of ref. objects (normally name and note)
+    article_attributes = article.search_index_attribute_lookup(include_references: false)
 
-    attrs['name'] = checklist.name if checklist.name.present?
+    # remove note needed attributes
+    ignore = %w[message_id_md5 ticket]
+    ignore.each do |attribute|
+      article_attributes.delete(attribute)
+    end
 
-    items = checklist.items.pluck(:text).compact_blank
-    attrs['items'] = items if items.present?
+    # index raw text body
+    if article_attributes['content_type'] && article_attributes['content_type'] == 'text/html' && article_attributes['body']
+      article_attributes['body'] = article_attributes['body'].html2text
+    end
 
-    return if attrs.blank?
+    article_attributes
+  end
 
-    attributes['checklist'] = attrs
+  def search_index_article_attachment_attributes(attachment)
+    {
+      'size'     => attachment.size,
+      '_name'    => attachment.filename,
+      '_content' => Base64.encode64(attachment.content).delete("\n"),
+    }
   end
+
 end

+ 21 - 0
spec/models/checklist_spec.rb

@@ -254,4 +254,25 @@ RSpec.describe Checklist, :aggregate_failures, current_user_id: 1, type: :model
       end
     end
   end
+
+  describe '.search_index_attribute_lookup' do
+    subject(:checklist) { create(:checklist, name: 'some name') }
+
+    it 'verify name attribute' do
+      expect(checklist.search_index_attribute_lookup['name']).to eq checklist.name
+    end
+
+    it 'verify items attribute' do
+      expect(checklist.search_index_attribute_lookup['items']).not_to eq []
+    end
+
+    it 'verify items[0].count' do
+      expect(checklist.search_index_attribute_lookup['items'].count).to eq checklist.items.count
+    end
+
+    it 'verify items[0].text attribute' do
+      expect(checklist.search_index_attribute_lookup['items'][0]['text']).to eq checklist.items[0].text
+    end
+  end
+
 end

+ 48 - 0
spec/models/ticket_spec.rb

@@ -1841,6 +1841,54 @@ RSpec.describe Ticket, type: :model do
     end
   end
 
+  describe '.search_index_article_attachment_attributes' do
+    context 'payload for article' do
+      subject!(:store_item) do
+        create(:store,
+               object:   'SomeObject',
+               o_id:     1,
+               data:     'some content',
+               filename: 'test.TXT')
+      end
+
+      it 'verify count of attributes' do
+        expect(ticket.send(:search_index_article_attachment_attributes, store_item).count).to be 3
+      end
+
+      it 'verify size' do
+        expect(ticket.send(:search_index_article_attachment_attributes, store_item)['size']).to eq '12'
+      end
+
+      it 'verify _name' do
+        expect(ticket.send(:search_index_article_attachment_attributes, store_item)['_name']).to eq 'test.TXT'
+      end
+
+      it 'verify _content' do
+        expect(ticket.send(:search_index_article_attachment_attributes, store_item)['_content']).to eq 'c29tZSBjb250ZW50'
+      end
+    end
+  end
+
+  describe '.search_index_article_attributes' do
+    context 'payload for attachment' do
+      subject!(:ticket_article) do
+        create(:ticket_article, ticket: create(:ticket))
+      end
+
+      it 'verify count of attributes' do
+        expect(ticket.send(:search_index_article_attributes, ticket_article).count).to eq 20
+      end
+
+      it 'verify from' do
+        expect(ticket.send(:search_index_article_attributes, ticket_article)['from']).to eq ticket_article.from
+      end
+
+      it 'verify body' do
+        expect(ticket.send(:search_index_article_attributes, ticket_article)['body']).to eq ticket_article.body
+      end
+    end
+  end
+
   describe '.search_index_attribute_lookup' do
     subject!(:ticket) { create(:ticket) }