triggers_subscriptions.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. return true if !saved_change_to_attribute?('group_id')
  14. TaskbarUpdateTriggerSubscriptionsJob.perform_later("#{self.class}-#{id}")
  15. end
  16. TRIGGER_CHECKLIST_UPDATE_ON = %w[title group_id].freeze
  17. def trigger_checklist_subscriptions
  18. return if !saved_changes.keys.intersect? TRIGGER_CHECKLIST_UPDATE_ON
  19. Checklist
  20. .where(id: referencing_checklists)
  21. .includes(:ticket)
  22. .each do |elem|
  23. Gql::Subscriptions::Ticket::ChecklistUpdates.trigger(
  24. elem,
  25. arguments: {
  26. ticket_id: Gql::ZammadSchema.id_from_object(elem.ticket),
  27. }
  28. )
  29. end
  30. end
  31. TRIGGER_LINK_UPDATE_ON = %w[title state_id].freeze
  32. def trigger_link_subscriptions
  33. return if !saved_changes.keys.intersect? TRIGGER_LINK_UPDATE_ON
  34. Gql::Subscriptions::LinkUpdates.trigger(
  35. nil,
  36. arguments: {
  37. object_id: Gql::ZammadSchema.id_from_object(self),
  38. target_type: self.class.name
  39. }
  40. )
  41. links = Link.list(
  42. link_object: self.class.name,
  43. link_object_value: id
  44. ).uniq
  45. links.each do |link|
  46. target = link['link_object'].constantize.find(link['link_object_value'])
  47. Gql::Subscriptions::LinkUpdates.trigger(
  48. nil,
  49. arguments: {
  50. object_id: Gql::ZammadSchema.id_from_object(target),
  51. target_type: link['link_object']
  52. }
  53. )
  54. end
  55. end
  56. end