20230807051414_input_fields_operator_renaming.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class InputFieldsOperatorRenaming < ActiveRecord::Migration[6.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. update_postmaster_filter
  7. update_core_workflows
  8. end
  9. private
  10. OPERATOR_MAPPING = {
  11. 'is' => 'is any of',
  12. 'is not' => 'is none of',
  13. 'starts with' => 'starts with one of',
  14. 'ends with' => 'ends with one of',
  15. }.freeze
  16. def update_postmaster_filter
  17. PostmasterFilter.in_batches.each_record do |filter|
  18. next if filter.match.blank?
  19. filter.match.each_value do |condition|
  20. next if condition[:operator].blank?
  21. next if OPERATOR_MAPPING.keys.exclude?(condition[:operator])
  22. condition[:operator] = OPERATOR_MAPPING[condition[:operator]]
  23. end
  24. filter.save!
  25. end
  26. end
  27. def update_core_workflows
  28. CoreWorkflow.in_batches.each_record do |workflow|
  29. next if workflow.condition_saved.blank? && workflow.condition_selected.blank?
  30. update_core_workflow_condition(workflow, :condition_saved)
  31. update_core_workflow_condition(workflow, :condition_selected)
  32. workflow.save!
  33. end
  34. end
  35. def update_core_workflow_condition(workflow, condition_type)
  36. return if !input_operators_used?(workflow, condition_type)
  37. workflow[condition_type.to_sym].each do |attribute, condition|
  38. next if !operator_relevant?(condition[:operator])
  39. next if !input_attribute?(attribute)
  40. condition[:operator] = OPERATOR_MAPPING[condition[:operator]]
  41. end
  42. end
  43. def operator_relevant?(operator)
  44. return false if operator.blank?
  45. return false if OPERATOR_MAPPING.keys.exclude?(operator)
  46. true
  47. end
  48. def input_attribute?(attribute)
  49. meta_information = object_manager_attributes_type_lookup[attribute]
  50. return false if meta_information.nil?
  51. return false if !meta_information.eql?('input')
  52. true
  53. end
  54. def input_operators_used?(workflow, condition_type)
  55. workflow[condition_type.to_sym].values.any? do |condition|
  56. condition.key?(:operator) && condition[:operator].present? && OPERATOR_MAPPING.key?(condition[:operator])
  57. end
  58. end
  59. def object_manager_attributes_type_lookup
  60. @object_manager_attributes_type_lookup ||= begin
  61. ObjectManager::Attribute.all.each_with_object({}) do |attribute, hash|
  62. object_name = ObjectLookup.by_id(attribute.object_lookup_id)
  63. if object_name.casecmp('ticketarticle').zero?
  64. object_name = 'article'
  65. end
  66. hash["#{object_name.downcase}.#{attribute.name}"] = attribute.data_type
  67. end
  68. end
  69. end
  70. end