Add new file

This commit is contained in:
Kumi 2023-12-18 17:09:46 +00:00
parent cc870f1185
commit 0fe150afbf

17
purge_inactive_users.rake Normal file
View file

@ -0,0 +1,17 @@
namespace :zammad do
desc "Purge inactive users"
task purge_inactive_users: :environment do
User.joins('LEFT JOIN tickets ON tickets.customer_id = users.id')
.joins(:roles)
.where('roles.name = \'Customer\' AND tickets.state_id NOT IN (?) AND tickets.updated_at < ?', Ticket::State.where(name: ['new', 'open']).pluck(:id), 30.days.ago)
.distinct
.find_each do |user|
if Ticket.where(customer_id: user.id)
.where('state_id IN (?) AND updated_at >= ?', Ticket::State.where(name: ['new', 'open']).pluck(:id), 30.days.ago)
.empty?
user.destroy
end
end
puts "Purge complete."
end
end