updates_ticket_organization.rb 873 B

123456789101112131415161718192021222324252627282930
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. # If a user is assigned to another organization, also assign their latest tickets to it.
  3. module User::UpdatesTicketOrganization
  4. extend ActiveSupport::Concern
  5. included do
  6. after_create :user_update_ticket_organization
  7. after_update :user_update_ticket_organization
  8. end
  9. private
  10. def user_update_ticket_organization
  11. # check if organization has changed
  12. return true if !saved_change_to_attribute?('organization_id')
  13. # update last 100 tickets of user
  14. tickets = Ticket.where(customer_id: id).limit(100)
  15. tickets.each do |ticket|
  16. next if ticket.organization_id == organization_id
  17. Transaction.execute(disable_notification: true, reset_user_id: true) do
  18. ticket.organization_id = organization_id
  19. ticket.save!
  20. end
  21. end
  22. end
  23. end