class Agents::DestroyJob < ApplicationJob queue_as :default def perform(account, user) ActiveRecord::Base.transaction do destroy_notification_setting(account, user) remove_user_from_teams(account, user) remove_user_from_inboxes(account, user) unassign_conversations(account, user) end end private def remove_user_from_inboxes(account, user) inboxes = account.inboxes.all inbox_members = user.inbox_members.where(inbox_id: inboxes.pluck(:id)) inbox_members.destroy_all end def remove_user_from_teams(account, user) teams = account.teams.all team_members = user.team_members.where(team_id: teams.pluck(:id)) team_members.destroy_all end def destroy_notification_setting(account, user) setting = user.notification_settings.find_by(account_id: account.id) setting&.destroy! end def unassign_conversations(account, user) # rubocop:disable Rails/SkipsModelValidations user.assigned_conversations.where(account: account).in_batches.update_all(assignee_id: nil) # rubocop:enable Rails/SkipsModelValidations end end