diff options
-rwxr-xr-x | bin/fixmystreet.com/buckinghamshire-flytipping | 80 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm | 44 |
2 files changed, 106 insertions, 18 deletions
diff --git a/bin/fixmystreet.com/buckinghamshire-flytipping b/bin/fixmystreet.com/buckinghamshire-flytipping new file mode 100755 index 000000000..72e8b7e7b --- /dev/null +++ b/bin/fixmystreet.com/buckinghamshire-flytipping @@ -0,0 +1,80 @@ +#!/usr/bin/env perl +# +# If a district flytipping report within Buckinghamshire has not been closed +# after three weeks, close it with a message. If it's older than six weeks, +# use a different message and suppress any alerts. + +use v5.14; +use warnings; + +BEGIN { + use File::Basename qw(dirname); + use File::Spec; + my $d = dirname(File::Spec->rel2abs($0)); + require "$d/../../setenv.pl"; +} + +use constant BUCKS_AREA_ID => 2217; +use constant DISTRICT_IDS => (2255, 2256, 2257, 2258); +use constant TIME_OPEN => '3 weeks'; +use constant TIME_OPEN_ALERT => '6 weeks'; + +use FixMyStreet::DB; +use FixMyStreet::Script::ArchiveOldEnquiries; +use Getopt::Long::Descriptive; + +my ($opts, $usage) = describe_options( + '%c %o', + ['commit|c', "actually close reports and send emails. Omitting this flag will do a dry-run"], + ['help|h', "print usage message and exit" ], +); +print($usage->text), exit if $opts->help; + +my $body = FixMyStreet::DB->resultset("Body")->for_areas(BUCKS_AREA_ID)->first; +die "Could not find Bucks body" unless $body; + +my @districts = FixMyStreet::DB->resultset("Body")->for_areas(DISTRICT_IDS)->all; +my @district_ids = map { $_->id } @districts; +die "Did not find all districts" unless @district_ids == 4; + +find_problems(TIME_OPEN_ALERT, TIME_OPEN, 'Auto-closure', 1); +find_problems(undef, TIME_OPEN_ALERT, 'Auto-closure (old)', 0); + +sub find_problems { + my ($from, $to, $title, $retain_alerts) = @_; + + my $template = FixMyStreet::DB->resultset("ResponseTemplate")->search({ + body_id => $body->id, title => $title, + })->first; + die "Could not find Bucks Flytipping template" unless $template; + + $to = "current_timestamp - '$to'::interval"; + my $time_param; + if ($from) { + $from = "current_timestamp - '$from'::interval"; + $time_param = [ -and => { '>=', \$from }, { '<', \$to } ], + } else { + $time_param = { '<', \$to }; + } + + # Fetch all Flytipping problems sent only to districts, between $from and $to + my $q = FixMyStreet::DB->resultset("Problem")->search( + \[ "? @> regexp_split_to_array(bodies_str, ',')", [ {} => \@district_ids ] ] + )->search({ + state => [ FixMyStreet::DB::Result::Problem->open_states() ], + category => 'Flytipping', + confirmed => $time_param, + }); + + # Provide some variables to the archiving script + FixMyStreet::Script::ArchiveOldEnquiries::update_options({ + user => $body->comment_user, + user_name => $body->comment_user->name, + closure_text => $template->text, + retain_alerts => $retain_alerts, + commit => $opts->commit, + }); + + # Close the reports + FixMyStreet::Script::ArchiveOldEnquiries::close_problems($q); +} diff --git a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm index 03bc511a0..dd44b9651 100644 --- a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm +++ b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm @@ -1,11 +1,8 @@ package FixMyStreet::Script::ArchiveOldEnquiries; -use strict; +use v5.14; use warnings; -require 5.8.0; -use FixMyStreet; -use FixMyStreet::App; use FixMyStreet::DB; use FixMyStreet::Cobrand; use FixMyStreet::Map; @@ -17,17 +14,17 @@ my $opts = { }; sub query { - return { - bodies_str => { 'LIKE', "%".$opts->{body}."%"}, - -and => [ + my $rs = shift; + return $rs->to_body($opts->{body})->search({ + -and => [ lastupdate => { '<', $opts->{email_cutoff} }, lastupdate => { '>', $opts->{closure_cutoff} }, ], - state => [ FixMyStreet::DB::Result::Problem->open_states() ], - }; + state => [ FixMyStreet::DB::Result::Problem->open_states() ], + }); } -sub archive { +sub update_options { my $params = shift; if ( $params ) { $opts = { @@ -35,13 +32,19 @@ sub archive { %$params, }; } +} + +sub archive { + my $params = shift; + update_options($params); unless ( $opts->{commit} ) { printf "Doing a dry run; emails won't be sent and reports won't be closed.\n"; printf "Re-run with --commit to actually archive reports.\n\n"; } - my @user_ids = FixMyStreet::DB->resultset('Problem')->search(query(), + my $rs = FixMyStreet::DB->resultset('Problem'); + my @user_ids = query($rs)->search(undef, { distinct => 1, columns => ['user_id'], @@ -55,7 +58,7 @@ sub archive { }); my $user_count = $users->count; - my $problem_count = FixMyStreet::DB->resultset('Problem')->search(query(), + my $problem_count = query($rs)->search(undef, { columns => ['id'], rows => $opts->{limit}, @@ -71,8 +74,7 @@ sub archive { } } - my $problems_to_close = FixMyStreet::DB->resultset('Problem')->search({ - bodies_str => { 'LIKE', "%".$opts->{body}."%"}, + my $problems_to_close = $rs->to_body($opts->{body})->search({ lastupdate => { '<', $opts->{closure_cutoff} }, state => [ FixMyStreet::DB::Result::Problem->open_states() ], }, { @@ -87,7 +89,8 @@ sub archive { sub send_email_and_close { my ($user) = @_; - my $problems = $user->problems->search(query(), { + my $problems = $user->problems; + $problems = query($problems)->search(undef, { order_by => { -desc => 'confirmed' }, }); @@ -135,22 +138,27 @@ sub close_problems { return unless $opts->{commit}; my $problems = shift; + my $extra = { auto_closed_by_script => 1 }; + $extra->{is_superuser} = 1 if !$opts->{user_name}; + while (my $problem = $problems->next) { my $timestamp = \'current_timestamp'; my $comment = $problem->add_to_comments( { - text => '', + text => $opts->{closure_text} || '', created => $timestamp, confirmed => $timestamp, user_id => $opts->{user}, - name => _('an administrator'), + name => $opts->{user_name} || _('an administrator'), mark_fixed => 0, anonymous => 0, state => 'confirmed', problem_state => 'closed', - extra => { is_superuser => 1 }, + extra => $extra, } ); $problem->update({ state => 'closed', send_questionnaire => 0 }); + next if $opts->{retain_alerts}; + # Stop any alerts being sent out about this closure. my @alerts = FixMyStreet::DB->resultset('Alert')->search( { alert_type => 'new_updates', |