aboutsummaryrefslogtreecommitdiffstats
path: root/bin/oxfordshire/send-rdi-emails
blob: 11e7a6fe98b107b58f348e585b704222a0311dcf (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
#!/usr/bin/env perl

use strict;
use warnings;
use v5.14;

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

use DateTime;
use Try::Tiny;
use FixMyStreet;
use FixMyStreet::Cobrand;
use FixMyStreet::Email;
use FixMyStreet::Integrations::ExorRDI;

my $end_date = DateTime->now( time_zone => FixMyStreet->time_zone || FixMyStreet->local_time_zone )
    ->truncate(to => 'hour')->set_hour(16);
my $start_date = $end_date->clone->subtract(days => 1);
my $inspection_date = $end_date; # All inspections are considered to have happened on the same date.

my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('oxfordshire')->new;
$cobrand->set_lang_and_domain('en-gb', 1);
my @inspectors = $cobrand->users->search({
    'user_body_permissions.permission_type' => 'report_inspect'
}, {
    join => 'user_body_permissions',
    distinct => 1,
})->all;

foreach my $inspector (@inspectors) {
    my $params = {
        start_date => $start_date,
        end_date => $end_date,
        inspection_date => $inspection_date,
        user => $inspector,
    };
    my $rdi = FixMyStreet::Integrations::ExorRDI->new($params);
    try {
        my $hdrs = {
            To => join('', 'fms', '_', 'admin', '@', $cobrand->admin_user_domain),
            _attachments_ => [ {
                body => $rdi->construct,
                attributes => {
                    filename => $rdi->filename,
                    charset => 'utf-8',
                    content_type => 'text/csv',
                    name => $rdi->filename,
                }
            } ],
        };

        my $result = FixMyStreet::Email::send_cron(
            FixMyStreet::DB->storage->schema,
            "rdi.txt", $params, $hdrs,
            undef, 0, $cobrand,
        );
        if ($result) {
            say "Failed to send inspection for " . $inspector->id;
        }
    } catch {
        die $_ unless $_ =~ /FixMyStreet::Integrations::ExorRDI::Error/;
        # Nothing to report, continue
    }
}

1;