aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2020-01-08 15:55:48 +0000
committerMatthew Somerville <matthew@mysociety.org>2020-01-09 17:31:44 +0000
commita47cdf03631855fd83866b932161eaa8ccf0c8b5 (patch)
tree8589ba85501132a7eb14fb75e78eabe37503882d
parentc38ca476d282c8e369ae6788b3400c4cc41f3e20 (diff)
[Inactive] Add option to fully delete reports.
-rwxr-xr-xbin/process-inactive-reports7
-rw-r--r--perllib/FixMyStreet/DB/Result/Problem.pm11
-rw-r--r--perllib/FixMyStreet/Script/Inactive.pm31
-rw-r--r--t/script/inactive.t11
4 files changed, 54 insertions, 6 deletions
diff --git a/bin/process-inactive-reports b/bin/process-inactive-reports
index d2c030c2c..c79b401a3 100755
--- a/bin/process-inactive-reports
+++ b/bin/process-inactive-reports
@@ -15,9 +15,9 @@ use FixMyStreet::Script::Inactive;
use Pod::Usage;
my %h;
-GetOptions(\%h, 'anonymize=i', 'close=i', 'verbose|v', 'help|h', 'dry-run|n');
+GetOptions(\%h, 'anonymize=i', 'close=i', 'delete=i', 'verbose|v', 'help|h', 'dry-run|n');
pod2usage(0) if $h{help};
-pod2usage(1) unless $h{anonymize} || $h{close};
+pod2usage(1) unless $h{anonymize} || $h{close} || $h{delete};
FixMyStreet::Script::Inactive->new(%h)->reports;
@@ -29,11 +29,12 @@ process-inactive-reports - deal with anonymizing inactive non-open reports
=head1 SYNOPSIS
-process-inactive-reports [--anonymize N] [--close N]
+process-inactive-reports [--anonymize N] [--close N] [--delete N]
Options:
--anonymize Anonymize non-open reports (and related) inactive longer than this time (months)
--close Close comments on non-open reports inactive longer than this time (months)
+ --delete Delete non-open reports inactive longer than this time (months)
--dry-run Don't actually anonymize anything or send any emails
--verbose Output as to which reports are being affected
--help This help message
diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm
index a188d9c2b..d9bb8e125 100644
--- a/perllib/FixMyStreet/DB/Result/Problem.pm
+++ b/perllib/FixMyStreet/DB/Result/Problem.pm
@@ -1187,4 +1187,15 @@ has inspection_log_entry => (
},
);
+has alerts => (
+ is => 'ro',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ return $self->result_source->schema->resultset('Alert')->search({
+ alert_type => 'new_updates', parameter => $self->id
+ });
+ },
+);
+
1;
diff --git a/perllib/FixMyStreet/Script/Inactive.pm b/perllib/FixMyStreet/Script/Inactive.pm
index 02b01b9b1..bfe746a68 100644
--- a/perllib/FixMyStreet/Script/Inactive.pm
+++ b/perllib/FixMyStreet/Script/Inactive.pm
@@ -12,6 +12,7 @@ use FixMyStreet::Email;
has anonymize => ( is => 'ro' );
has close => ( is => 'ro' );
+has delete => ( is => 'ro' );
has email => ( is => 'ro' );
has verbose => ( is => 'ro' );
has dry_run => ( is => 'ro' );
@@ -55,6 +56,7 @@ sub reports {
say "DRY RUN" if $self->dry_run;
$self->anonymize_reports if $self->anonymize;
+ $self->delete_reports if $self->delete;
$self->close_updates if $self->close;
}
@@ -110,10 +112,8 @@ sub anonymize_reports {
});
# Remove alerts - could just delete, but of interest how many there were, perhaps?
- FixMyStreet::DB->resultset('Alert')->search({
+ $problem->alerts->search({
user_id => { '!=' => $self->anonymous_user->id },
- alert_type => 'new_updates',
- parameter => $problem->id,
})->update({
user_id => $self->anonymous_user->id,
whendisabled => \'current_timestamp',
@@ -121,6 +121,31 @@ sub anonymize_reports {
}
}
+sub delete_reports {
+ my $self = shift;
+
+ my $problems = FixMyStreet::DB->resultset("Problem")->search({
+ lastupdate => { '<', interval($self->delete) },
+ state => [
+ FixMyStreet::DB::Result::Problem->closed_states(),
+ FixMyStreet::DB::Result::Problem->fixed_states(),
+ FixMyStreet::DB::Result::Problem->hidden_states(),
+ ],
+ });
+
+ while (my $problem = $problems->next) {
+ say "Deleting associated data of problem #" . $problem->id if $self->verbose;
+ next if $self->dry_run;
+
+ $problem->comments->delete;
+ $problem->questionnaires->delete;
+ $problem->alerts->delete;
+ }
+ say "Deleting all matching problems" if $self->verbose;
+ return if $self->dry_run;
+ $problems->delete;
+}
+
sub anonymize_users {
my $self = shift;
diff --git a/t/script/inactive.t b/t/script/inactive.t
index 60cf3ecb5..582059cbf 100644
--- a/t/script/inactive.t
+++ b/t/script/inactive.t
@@ -60,6 +60,17 @@ subtest 'Closing updates on inactive fixed/closed reports' => sub {
$mech->content_contains('now closed to updates');
};
+subtest 'Deleting reports' => sub {
+ my $in = FixMyStreet::Script::Inactive->new( delete => 6 );
+ $in->reports;
+
+ my $count = FixMyStreet::DB->resultset("Problem")->count;
+ is $count, 6, 'Six left';
+
+ $mech->get("/report/" . $problems[2]->id);
+ is $mech->res->code, 404;
+};
+
subtest 'Anonymization of inactive users' => sub {
my $in = FixMyStreet::Script::Inactive->new( anonymize => 6, email => 3, verbose => 1 );
stdout_is { $in->users } "Anonymizing user #" . $user->id . "\nEmailing user #" . $user_inactive->id . "\n", 'users dealt with first time';