2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-03-01 08:18:40 +00:00
|
|
|
|
|
|
|
# If a user is assigned to another organization, also assign their latest tickets to it.
|
|
|
|
module User::UpdatesTicketOrganization
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_create :user_update_ticket_organization
|
|
|
|
after_update :user_update_ticket_organization
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_update_ticket_organization
|
|
|
|
|
|
|
|
# check if organization has changed
|
|
|
|
return true if !saved_change_to_attribute?('organization_id')
|
|
|
|
|
|
|
|
# update last 100 tickets of user
|
|
|
|
tickets = Ticket.where(customer_id: id).limit(100)
|
|
|
|
tickets.each do |ticket|
|
2022-06-13 12:56:50 +00:00
|
|
|
next if ticket.organization_id == organization_id
|
|
|
|
|
|
|
|
Transaction.execute(disable_notification: true, reset_user_id: true) do
|
2021-03-01 08:18:40 +00:00
|
|
|
ticket.organization_id = organization_id
|
2022-06-13 12:56:50 +00:00
|
|
|
ticket.save!
|
2021-03-01 08:18:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|