updates_ticket_organization.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (C) 2012-2024 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. return true if !Setting.get('ticket_organization_reassignment')
  12. # check if organization has changed
  13. return true if !saved_change_to_attribute?('organization_id')
  14. # update last 100 tickets of user
  15. tickets = Ticket.where(customer_id: id, organization_id: old_organization_id).limit(100)
  16. tickets.each do |ticket|
  17. next if ticket.organization_id == organization_id
  18. Transaction.execute(disable_notification: true, reset_user_id: true) do
  19. ticket.organization_id = organization_id
  20. ticket.save!
  21. end
  22. end
  23. end
  24. def old_organization_id
  25. previous_changes['organization_id'].first
  26. end
  27. end