diff options
Diffstat (limited to 'bin/open311-populate-service-list')
-rwxr-xr-x | bin/open311-populate-service-list | 136 |
1 files changed, 67 insertions, 69 deletions
diff --git a/bin/open311-populate-service-list b/bin/open311-populate-service-list index 7a91c2c05..ddab063f4 100755 --- a/bin/open311-populate-service-list +++ b/bin/open311-populate-service-list @@ -5,94 +5,92 @@ use warnings; use LWP::Simple; use XML::Simple; use FixMyStreet::App; +use Open311; use Data::Dumper; -my $council = shift; -my $end_point = shift; +my $council_list = FixMyStreet::App->model('DB::Open311conf'); -die <<"EOT" -You need to provide a council id and an end point url +while ( my $council = $council_list->next ) { -Usage: - - $0 council_id end_point_url - - council_id: the mapit id of the council as used in the contacts table - end_point_url: the location of the open311 service list xml - -EOT - unless $council and $end_point; - -my $service_list = get( $end_point ); - -die "Invalid council id: $council\n" unless $council =~ /^\d+$/; -die "No service list found at $end_point\n" unless $service_list; - -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 $open311 = Open311->new( + endpoint => $council->endpoint, + jurisdiction => $council->jurisdiction, + api_key => $council->api_key ); - my $contact = $contacts->first; + my $service_list = $open311->get_service_list; + + my $xml = XML::Simple->new(); + my $list = $xml->XMLin( $service_list ); + my @found_contacts; - if ( $contact ) { + # print Dumper $list; - print "$council already has a contact for service code " . $service->{service_code} . "\n"; - push @found_contacts, $service->{service_code}; + foreach my $service ( @{ $list->{service} } ) { + print $service->{service_code} . ': ' . $service->{service_name} . "\n"; + my $contacts = FixMyStreet::App->model( 'DB::Contact')->search( + { + area_id => $council->area_id, + -OR => [ + email => $service->{service_code}, + category => $service->{service_name} + ] + } + ); - if ( $contact->deleted ) { - $contact->update( + my $contact = $contacts->first; + + # FIXME - handle change of service name or service code + if ( $contact ) { + + print $council->area_id . " already has a contact for service code " . $service->{service_code} . "\n"; + push @found_contacts, $service->{service_code}; + + if ( $contact->deleted ) { + $contact->update( + { + category => $service->{service_name}, + email => $service->{service_code}, + confirmed => 1, + deleted => 0, + editor => $0, + whenedited => \'ms_current_timestamp()', + note => 'automatically undeleted by script', + } + ); + } + } else { + my $contact = FixMyStreet::App->model( 'DB::Contact')->create( { + email => $service->{service_code}, + area_id => $council->area_id, category => $service->{service_name}, confirmed => 1, deleted => 0, editor => $0, whenedited => \'ms_current_timestamp()', - note => 'automatically undeleted by script', + note => 'created automatically by script', } ); + print "created contact for service code " . $service->{service_code} . " for council @{[$council->area_id]}\n"; } - } 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, - } -); + my $found_contacts = FixMyStreet::App->model( 'DB::Contact')->search( + { + email => { -not_in => \@found_contacts }, + area_id => $council->area_id, + deleted => 0, + } + ); -$found_contacts->update( - { - deleted => 1, - editor => $0, - whenedited => \'ms_current_timestamp()', - note => 'automatically marked as deleted by script' - } -); + $found_contacts->update( + { + deleted => 1, + editor => $0, + whenedited => \'ms_current_timestamp()', + note => 'automatically marked as deleted by script' + } + ); +} |