base.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Ticket::Selector::Base
  3. attr_accessor :selector, :options, :changed_attributes
  4. def initialize(selector:, options:)
  5. if selector.respond_to?(:permit!)
  6. selector = selector.permit!.to_h
  7. end
  8. @selector = Marshal.load(Marshal.dump(selector)).deep_symbolize_keys
  9. @options = options
  10. @changed_attributes = {}
  11. @options[:changes] ||= {}
  12. migrate
  13. set_static_conditions
  14. check_changes
  15. end
  16. def migrate
  17. return if !selector[:conditions].nil?
  18. result = {
  19. operator: 'AND',
  20. conditions: [],
  21. }
  22. selector.each_key do |key|
  23. result[:conditions] << {
  24. name: key.to_s,
  25. }.merge(selector[key])
  26. end
  27. @selector = result
  28. end
  29. def set_static_conditions
  30. conditions = static_conditions_ticket + static_conditions_article + static_conditions_merged + static_conditions_ticket_update
  31. return if conditions.blank?
  32. @selector = {
  33. operator: 'AND',
  34. conditions: conditions + [@selector]
  35. }
  36. end
  37. def static_conditions_ticket
  38. return [] if options[:ticket_id].blank?
  39. [
  40. {
  41. name: 'ticket.id',
  42. operator: 'is',
  43. value: options[:ticket_id]
  44. }
  45. ]
  46. end
  47. def static_conditions_article
  48. return [] if options[:article_id].blank?
  49. [
  50. {
  51. name: 'article.id',
  52. operator: 'is',
  53. value: options[:article_id]
  54. }
  55. ]
  56. end
  57. def static_conditions_merged
  58. return [] if options[:exclude_merged].blank?
  59. [
  60. {
  61. name: 'ticket_state.name',
  62. operator: 'is not',
  63. value: Ticket::StateType.find_by(name: 'merged').states.pluck(:name),
  64. }
  65. ]
  66. end
  67. # https://github.com/zammad/zammad/issues/4550
  68. def static_conditions_ticket_update
  69. return [] if options[:ticket_action] != 'update' || options[:changes_required].blank?
  70. [
  71. {
  72. name: 'ticket.action',
  73. operator: 'is',
  74. value: 'update'
  75. }
  76. ]
  77. end
  78. def check_changes
  79. if options[:article_id].present? && attribute_exists?('article.id')
  80. @changed_attributes['article'] = true
  81. end
  82. options[:changes].each_key do |key|
  83. next if !attribute_exists?("ticket.#{key}")
  84. @changed_attributes["ticket.#{key}"] = true
  85. end
  86. end
  87. def attribute_exists?(attribute, check_condition = @selector[:conditions])
  88. result = false
  89. check_condition.each do |condition|
  90. if condition.key?(:conditions)
  91. if attribute_exists?(attribute, condition[:conditions])
  92. result = true
  93. end
  94. elsif condition[:name] == attribute
  95. result = true
  96. end
  97. end
  98. result
  99. end
  100. def get
  101. raise 'NOT_IMPLEMENTED'
  102. end
  103. class InvalidCondition < StandardError
  104. end
  105. end