triggers_subscriptions.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Trigger GraphQL subscriptions on ticket changes.
  3. module Ticket::TriggersSubscriptions
  4. extend ActiveSupport::Concern
  5. included do
  6. after_update_commit :trigger_subscriptions
  7. after_update_commit :trigger_checklist_subscriptions
  8. after_update_commit :trigger_link_subscriptions
  9. end
  10. private
  11. def trigger_subscriptions
  12. Gql::Subscriptions::TicketUpdates.trigger(self, arguments: { ticket_id: Gql::ZammadSchema.id_from_object(self) })
  13. end
  14. TRIGGER_CHECKLIST_UPDATE_ON = %w[title group_id].freeze
  15. def trigger_checklist_subscriptions
  16. return if !saved_changes.keys.intersect? TRIGGER_CHECKLIST_UPDATE_ON
  17. Checklist
  18. .where(id: referencing_checklists)
  19. .includes(:ticket)
  20. .each do |elem|
  21. Gql::Subscriptions::Ticket::ChecklistUpdates.trigger(
  22. elem,
  23. arguments: {
  24. ticket_id: Gql::ZammadSchema.id_from_object(elem.ticket),
  25. }
  26. )
  27. end
  28. end
  29. TRIGGER_LINK_UPDATE_ON = %w[title state_id].freeze
  30. def trigger_link_subscriptions
  31. return if !saved_changes.keys.intersect? TRIGGER_LINK_UPDATE_ON
  32. Gql::Subscriptions::LinkUpdates.trigger(
  33. nil,
  34. arguments: {
  35. object_id: Gql::ZammadSchema.id_from_object(self),
  36. target_type: self.class.name
  37. }
  38. )
  39. links = Link.list(
  40. link_object: self.class.name,
  41. link_object_value: id
  42. ).uniq
  43. links.each do |link|
  44. target = link['link_object'].constantize.find(link['link_object_value'])
  45. Gql::Subscriptions::LinkUpdates.trigger(
  46. nil,
  47. arguments: {
  48. object_id: Gql::ZammadSchema.id_from_object(target),
  49. target_type: link['link_object']
  50. }
  51. )
  52. end
  53. end
  54. end