aboutsummaryrefslogtreecommitdiffstats
path: root/bin/fixmystreet.com/buckinghamshire-flytipping
blob: a312f9fbe448dda858ffc6ad9574930027e1d9dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/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_NAME => 'Buckinghamshire Council';
use constant EX_DISTRICTS => ['Aylesbury Vale District Council', 'Chiltern District Council', 'South Bucks District Council', 'Wycombe District Council'];
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")->search({ name => BUCKS_NAME })->first;
die "Could not find Bucks body" unless $body;

my @districts = FixMyStreet::DB->resultset("Body")->search({ name => EX_DISTRICTS })->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 made off-road (i.e. those that previously
    # would been sent only to districts) any any older ones which actually were
    # sent to districts, between $from and $to
    my $q = FixMyStreet::DB->resultset("Problem")->search([
        # Reports sent to district, made before the unitary switchover
        -and => [
            \[ "? @> regexp_split_to_array(bodies_str, ',')", [ {} => \@district_ids ] ],
            category => 'Flytipping'
        ],
        # Reports sent to Bucks Council after unitary switchover
        {
            bodies_str => $body->id,
            category => 'Flytipping (off-road)'
        }
    ])->search({
        state => [ FixMyStreet::DB::Result::Problem->open_states() ],
        confirmed => $time_param,
    });

    # Provide some variables to the archiving script
    FixMyStreet::Script::ArchiveOldEnquiries::update_options({
        user => $body->comment_user->id,
        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);
}