From 6c6ae8b927df4bd610b7444667674dacaf8b17b4 Mon Sep 17 00:00:00 2001 From: phil Date: Sat, 7 Aug 2021 16:47:05 +0200 Subject: [PATCH] =?UTF-8?q?Erg=C3=A4nze=20fehlendes=20Script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/zammad-delete-customers.rb.j2 | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 templates/zammad-delete-customers.rb.j2 diff --git a/templates/zammad-delete-customers.rb.j2 b/templates/zammad-delete-customers.rb.j2 new file mode 100644 index 0000000..bb2515e --- /dev/null +++ b/templates/zammad-delete-customers.rb.j2 @@ -0,0 +1,27 @@ +# https://github.com/JBails/PurgeZammad +# This script will delete all users that do not have an assigned ticket (open or closed), with no regards as to how long they haven't had a ticket +# i.e. doesn't care if the last ticket was deleted 30 seconds or 30 days ago + +ticketless_customers = User.with_permissions('ticket.customer'){% if zammad_preserve_customer_domains is defined %}{% for domain in zammad_preserve_customer_domains %}.where('email NOT LIKE ?', '%{{ domain }}%'){% endfor %}{% endif %}.where('id NOT IN (SELECT customer_id FROM tickets)') +count = ticketless_customers.count + +puts "#{count} customers without tickets found." +puts + +ticketless_customers.find_each.with_index do |user, i| + next if user.permissions?(%w[admin ticket.agent]) + next if user.id == 1 + + display_name = user.fullname + (user.fullname == user.email ? '' : " (#{user.email})") + + User.transaction do + begin + user.destroy! + puts " #{display_name} deleted." + rescue => e + puts " #{display_name} could not be deleted: #{e.message}" + raise ActiveRecord::Rollback + next + end + end +end