[Rt-commit] rt branch, 4.2/shred-notxn-users, created. rt-4.2.12-50-gb945b0d

Shawn Moore shawn at bestpractical.com
Tue Sep 8 13:41:29 EDT 2015


The branch, 4.2/shred-notxn-users has been created
        at  b945b0da211ed86c323fbab522f63e9662934ba9 (commit)

- Log -----------------------------------------------------------------
commit b945b0da211ed86c323fbab522f63e9662934ba9
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Tue Sep 8 17:40:59 2015 +0000

    Add user shredder option no_ticket_transactions

diff --git a/lib/RT/Shredder/Plugin/Users.pm b/lib/RT/Shredder/Plugin/Users.pm
index 7e1c31f..682b234 100644
--- a/lib/RT/Shredder/Plugin/Users.pm
+++ b/lib/RT/Shredder/Plugin/Users.pm
@@ -108,12 +108,21 @@ Note that found users still B<may have relations> with other objects,
 for example via Creator or LastUpdatedBy fields, and you most probably
 want to use C<replace_relations> option.
 
+=head2 no_ticket_transactions - boolean
+
+If true then plugin looks for users who have created no ticket transactions.
+This is especially useful after wiping out tickets.
+
+Note that found users still B<may have relations> with other objects,
+for example via Creator or LastUpdatedBy fields, and you most probably
+want to use C<replace_relations> option.
+
 =cut
 
 sub SupportArgs
 {
     return $_[0]->SUPER::SupportArgs,
-           qw(status name email member_of not_member_of replace_relations no_tickets);
+           qw(status name email member_of not_member_of replace_relations no_tickets no_ticket_transactions);
 }
 
 sub TestArgs
@@ -209,6 +218,11 @@ sub Run
             Shredder => $args{'Shredder'},
         );
     }
+    if( $self->{'opt'}{'no_ticket_transactions'} ) {
+        push @filter, $self->FilterWithoutTicketTransactions(
+            Shredder => $args{'Shredder'},
+        );
+    }
 
     if (@filter) {
         $self->FetchNext( $objs, 'init' );
@@ -284,11 +298,40 @@ sub _WithoutTickets {
     my $tickets = RT::Tickets->new( RT->SystemUser );
     $tickets->{'allow_deleted_search'} = 1;
     $tickets->FromSQL( 'Watcher.id = '. $user->id );
-    # HACK: we may use Count method which counts all records
-    # that match condtion, but we really want to know only that
-    # at least one record exist, so we fetch first row only
+
+    # we could use the Count method which counts all records
+    # that match, but we really want to know only that
+    # at least one record exists, so this is faster
     $tickets->RowsPerPage(1);
     return !$tickets->First;
 }
 
+sub FilterWithoutTicketTransactions {
+    my $self = shift;
+    my %args = (
+        Shredder => undef,
+        Objects  => undef,
+        @_,
+    );
+
+    return sub {
+        my $user = shift;
+        $self->_WithoutTicketTransactions( $user )
+    };
+}
+
+sub _WithoutTicketTransactions {
+    my ($self, $user) = @_;
+    return unless $user and $user->Id;
+    my $txns = RT::Transactions->new( RT->SystemUser );
+    $txns->Limit(FIELD => 'ObjectType', VALUE => 'RT::Ticket');
+    $txns->Limit(FIELD => 'Creator', VALUE => $user->Id);
+
+    # we could use the Count method which counts all records
+    # that match, but we really want to know only that
+    # at least one record exists, so this is faster
+    $txns->RowsPerPage(1);
+    return !$txns->First;
+}
+
 1;
diff --git a/t/shredder/03plugin_users.t b/t/shredder/03plugin_users.t
index 477f147..8f0fc2c 100644
--- a/t/shredder/03plugin_users.t
+++ b/t/shredder/03plugin_users.t
@@ -6,7 +6,7 @@ use Test::Deep;
 use RT::Test::Shredder tests => 21;
 my $test = "RT::Test::Shredder";
 
-my @ARGS = sort qw(limit status name member_of not_member_of email replace_relations no_tickets);
+my @ARGS = sort qw(limit status name member_of not_member_of email replace_relations no_tickets no_ticket_transactions);
 
 use_ok('RT::Shredder::Plugin::Users');
 {

-----------------------------------------------------------------------


More information about the rt-commit mailing list