diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/fixmystreet.com/buckinghamshire-flytipping | 80 |
1 files changed, 80 insertions, 0 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); +} |