action.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Rubocop does not like :: prefix, but Yard throws an error parsing without it
  3. class Ticket::PerformChanges::Action < ::PerformChanges::Action # rubocop:disable Style/RedundantConstantBase
  4. attr_accessor :article
  5. def initialize(record, execution_data, perform_changes_data)
  6. super
  7. @article = begin
  8. perform_changes_data[:context_data][:article]
  9. rescue
  10. nil
  11. end
  12. end
  13. private
  14. # articles.last breaks (returns the wrong article)
  15. # if another email notification trigger preceded this one
  16. # (see https://github.com/zammad/zammad/issues/1543)
  17. def notification_factory_template_objects
  18. @notification_factory_template_objects ||= begin
  19. {
  20. ticket: record,
  21. article: last_articles[:last_article],
  22. created_article: article,
  23. created_internal_article: article&.internal? ? article : nil,
  24. created_external_article: article&.internal? ? nil : article,
  25. }.merge(last_articles)
  26. end
  27. end
  28. def all_articles
  29. @all_articles ||= record.articles
  30. end
  31. def last_articles
  32. @last_articles ||= article.present? ? from_current_article : from_all_articles
  33. end
  34. def from_all_articles
  35. {
  36. last_article: all_articles.last,
  37. last_internal_article: all_articles.reverse.find(&:internal?),
  38. last_external_article: all_articles.reverse.find { |a| !a.internal? },
  39. }
  40. end
  41. def from_current_article
  42. {
  43. last_article: article,
  44. last_internal_article: article.internal? ? article : all_articles.reverse.find(&:internal?),
  45. last_external_article: article.internal? ? all_articles.reverse.find { |a| !a.internal? } : article,
  46. }
  47. end
  48. end