diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/open311-populate-service-list | 108 | ||||
-rwxr-xr-x | bin/send-reports | 29 |
2 files changed, 137 insertions, 0 deletions
diff --git a/bin/open311-populate-service-list b/bin/open311-populate-service-list new file mode 100755 index 000000000..f9f183f26 --- /dev/null +++ b/bin/open311-populate-service-list @@ -0,0 +1,108 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use LWP::Simple; +use XML::Simple; +use FixMyStreet::App; +use Open311; + +use Data::Dumper; + +my $council_list = FixMyStreet::App->model('DB::Open311conf'); + +while ( my $council = $council_list->next ) { + + my $open311 = Open311->new( + endpoint => $council->endpoint, + jurisdiction => $council->jurisdiction, + api_key => $council->api_key + ); + + my $list = $open311->get_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( + { + area_id => $council->area_id, + -OR => [ + email => $service->{service_code}, + category => $service->{service_name} + ] + } + ); + + 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 => 'created automatically by script', + } + ); + + if ( lc( $service->{metadata} ) eq 'true' ) { + print "Fetching meta data for $service->{service_code}\n"; + my $meta_data = $open311->get_service_meta_info( $service->{service_code} ); + + # turn the data into something a bit more friendly to use + my %meta = (); + foreach my $attribute ( @{ $meta_data->{attribute} } ) { + $meta{ $attribute->{code} } = $attribute; + } + + $contact->extra( \%meta ); + $contact->update; + } + print "created contact for service code " . $service->{service_code} . " for council @{[$council->area_id]}\n"; + } + } + + 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' + } + ); +} diff --git a/bin/send-reports b/bin/send-reports index 1af3ba1ea..298eb458d 100755 --- a/bin/send-reports +++ b/bin/send-reports @@ -28,6 +28,8 @@ use mySociety::EmailUtil; use mySociety::MaPit; use mySociety::Web qw(ent); +use Open311; + # Set up site, language etc. my ($verbose, $nomail) = CronFns::options(); my $base_url = mySociety::Config::get('BASE_URL'); @@ -136,6 +138,8 @@ while (my $row = $unsent->next) { $h{category} = 'Customer Services' if $h{category} eq 'Other'; } elsif ($areas_info->{$council}->{type} eq 'LBO') { # London $send_web = 'london'; + } elsif ( FixMyStreet::App->model("DB::Open311conf")->find( { area_id => $council } ) ) { + $send_web = 'open311'; } else { my $contact = FixMyStreet::App->model("DB::Contact")->find( { deleted => 0, @@ -248,6 +252,31 @@ while (my $row = $unsent->next) { if (!$nomail) { $result *= post_london_report( $row, %h ); } + } elsif ($send_web eq 'open311') { + # FIXME - looking this up twice :( + my $conf = FixMyStreet::App->model('DB::Open311conf')->find( { area_id => $row->council } ); + + # FIXME - doesn't deal with multiple recipients + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + deleted => 0, + area_id => $row->council, + category => $row->category + } ); + + my $open311 = Open311->new( + jurisdiction => $conf->jurisdiction, + endpoint => $conf->endpoint, + api_key => $conf->api_key, + ); + + my $resp = $open311->send_service_request( $row, \%h, $contact->email ); + + if ( $resp ) { + $row->external_id( $resp ); + $result = 0; + } else { + $result = 1; + } } if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) { |