aboutsummaryrefslogtreecommitdiffstats
path: root/bin/northamptonshire/update-emergency-message
blob: 7248e9159efda40ce19d5cf70d52c7e2977b0a44 (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
#!/usr/bin/env perl

# update the emergency message on NCC categories

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 FixMyStreet::DB;

use Getopt::Long;

my ($commit, $message);
GetOptions(
    'commit' => \$commit,
    'message=s' => \$message,
);

if (!$commit) {
    say "*** DRY RUN ***";
}

my $northants = FixMyStreet::DB->resultset("Body")->find({ name => 'Northamptonshire County Council' });
if ($northants) {
    my @northants_contacts = $northants->contacts->all;
    my $found_total = 0;
    foreach my $category (@northants_contacts) {
        my $extra_fields = $category->get_extra_fields;
        my $found = 0;
        foreach (@$extra_fields) {
            next unless $_->{code} eq 'emergency';
            $found_total++;
            $_->{code} = '_fms_disable_';
            $_->{description} = $message;
            $_->{protected} = 'true';
            $_->{disable_form} = 'true';
            $found = 1;
        }
        if ($found) {
            $category->set_extra_fields(@$extra_fields);
            say "Updating emergency message on " . $category->category . ", Northamptonshire";
            if ($commit) {
                $category->update;
            }
        }
    }
    if (!$found_total) {
        say STDERR "No emergency messages found for Northamptonshire";
    }
} else {
    say STDERR "Could not find Northamptonshire";
}