aboutsummaryrefslogtreecommitdiffstats
path: root/bin/open311-populate-service-list
blob: dcdacb0473170c6ad9a2135d862d73d311038065 (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
#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;
use XML::Simple;
use FixMyStreet::App;

use Data::Dumper;

my $council = shift;
my $end_point = shift;

die "require council id and end point\n" unless $council and $end_point;

my $service_list = get( $end_point );

my $xml = XML::Simple->new();
my $list = $xml->XMLin( $service_list );
my @found_contacts;

# print Dumper $list;

foreach my $service ( @{ $list->{service} } ) {
    print $service->{service_code} . ': ' . $service->{service_name} .  "\n";
    my $contacts = FixMyStreet::App->model( 'DB::Contact')->search(
        {
            email => $service->{service_code},
            area_id => $council
        }
    );

    my $contact = $contacts->first;

    if ( $contact ) {
        print "$council already has a contact for service code " . $service->{service_code} . "\n";
        push @found_contacts, $service->{service_code};
    } else {
        my $contact = FixMyStreet::App->model( 'DB::Contact')->create(
            {
                email => $service->{service_code},
                area_id => $council,
                category => $service->{service_name},
                confirmed => 1,
                deleted => 0,
                editor => $0,
                whenedited => \'ms_current_timestamp()',
                note => 'created automatically by script',
            }
        );
        print "created contact for service code " . $service->{service_code} . " for council $council\n";
    }
}

my $found_contacts = FixMyStreet::App->model( 'DB::Contact')->search(
    {
        email => { -not_in => \@found_contacts },
        area_id => $council,
        deleted => 0,
    }
);

$found_contacts->update(
    {
        deleted => 1,
        editor  => $0,
        whenedited => \'ms_current_timestamp()',
        note => 'automatically marked as deleted by script'
    }
);