aboutsummaryrefslogtreecommitdiffstats
path: root/bin/zurich/overdue-alert
blob: 700a642f4dda756b52dbd6c215d3a6159328fb90 (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

# zurich/overdue-alert:
# Send email alerts to administrators for overdue admin activities.
#
# Copyright (c) 2012 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org

use strict;
use warnings;
require 5.8.0;

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../../setenv.pl";
}

use DateTime;
use CronFns;
use FixMyStreet::App;

my ($verbose, $nomail) = CronFns::options();

# Only run on working days
my $now = DateTime->now();
exit if FixMyStreet::Cobrand::Zurich::is_public_holiday($now) or FixMyStreet::Cobrand::Zurich::is_weekend($now);

my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('zurich')->new();
my %bodies = map { $_->id => $_ } FixMyStreet::App->model("DB::Body")->all;

loop_through( 'alert-moderation-overdue.txt', 0, 1, [ 'unconfirmed' ] );
loop_through( 'alert-overdue.txt', 1, 6, 'in progress' );
loop_through( 'alert-overdue.txt', 0, 6, ['confirmed', 'planned'] );

sub loop_through {
    my ( $template, $include_parent, $days, $states ) = @_;
    my $dtf = FixMyStreet::App->model("DB")->storage->datetime_parser;
    my $date_threshold = $dtf->format_datetime(FixMyStreet::Cobrand::Zurich::sub_days( $now, $days ));

    my $reports = FixMyStreet::App->model("DB::Problem")->search( {
        state => $states,
        created => { '<',  $date_threshold },
        bodies_str => { '!=', undef },
    } );

    my %to_send = ();
    while (my $row = $reports->next) {
        $to_send{$row->bodies_str} .= '* ' . $row->id . ": '" . $row->title . "'\n\n";
    }

    my $template_path = FixMyStreet->path_to( "templates", "email", "zurich", $template )->stringify;
    $template = Utils::read_file( $template_path );

    foreach my $body_id (keys %to_send) {
        send_alert( $template, $body_id, $to_send{$body_id}, $include_parent );
    }
}

sub send_alert {
    my ( $template, $body_id, $data, $include_parent ) = @_;

    my $body = $bodies{$body_id};
    my $body_email = $body->endpoint;

    my $h = {
        data => $data,
        admin_url => $cobrand->admin_base_url,
    };

    my $to = [ [ $body_email, $body->name ] ];
    if ( $include_parent ) {
        my $parent = $body->parent;
        my $parent_email = $parent->endpoint;
        push @$to, [ $parent_email, $parent->name ];
    }

    FixMyStreet::App->send_email_cron(
        {
            _template_ => $template,
            _parameters_ => $h,
            To => $to,
            From => [ FixMyStreet->config('CONTACT_EMAIL'), FixMyStreet->config('CONTACT_NAME') ],
        },
        FixMyStreet->config('CONTACT_EMAIL'),
        $nomail
    );
}