diff options
author | Matthew Somerville <matthew@fury.ukcod.org.uk> | 2011-04-02 01:45:15 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@fury.ukcod.org.uk> | 2011-04-02 01:45:38 +0100 |
commit | 65fd3142a388207262fafeb831f29c12470c6a65 (patch) | |
tree | 16c0ab93d520436b141706b7ce6df21dd0b76c29 /bin/send-reports | |
parent | 960c1838721cf1f4366fb5c7600aa50b0c9e0c51 (diff) |
Post reports in London to the Report-It API.
Diffstat (limited to 'bin/send-reports')
-rwxr-xr-x | bin/send-reports | 96 |
1 files changed, 91 insertions, 5 deletions
diff --git a/bin/send-reports b/bin/send-reports index 8e6d2d178..3517f1ee0 100755 --- a/bin/send-reports +++ b/bin/send-reports @@ -15,10 +15,12 @@ require 5.8.0; use FindBin; use lib "$FindBin::Bin/../perllib"; use lib "$FindBin::Bin/../commonlib/perllib"; +use Digest::MD5; use Encode; use Error qw(:try); use File::Slurp; use JSON; +use LWP::UserAgent; use LWP::Simple; use CGI; # Trying awkward kludge use CronFns; @@ -53,7 +55,7 @@ my $site = CronFns::site($base_url); my $query = "SELECT id, council, category, title, detail, name, email, phone, used_map, latitude, longitude, (photo is not null) as has_photo, lang, - cobrand, cobrand_data + cobrand, cobrand_data, date_trunc('second', confirmed) as confirmed, postcode FROM problem WHERE state in ('confirmed','fixed') AND whensent IS NULL @@ -86,7 +88,8 @@ foreach my $row (@$unsent) { # Template variables for the email my $email_base_url = Cobrand::base_url_for_emails($cobrand, $cobrand_data); - my %h = map { $_ => $row->{$_} } qw/title detail name email phone category latitude longitude/; + my %h = map { $_ => $row->{$_} } qw/id title detail name email phone category latitude longitude confirmed used_map/; + $h{query} = $row->{postcode}; $h{url} = $email_base_url . '/report/' . $row->{id}; $h{phone_line} = $h{phone} ? _('Phone:') . " $h{phone}\n\n" : ''; if ($row->{has_photo}) { @@ -150,8 +153,10 @@ foreach my $row (@$unsent) { my $name = $areas_info->{$council}->{name}; push @dear, $name; if ($council == 2330) { # E. Hants have a web service - $send_web = 1; + $send_web = 'easthants'; $h{category} = 'Customer Services' if $h{category} eq 'Other'; + } elsif ($areas_info->{$council}->{type} eq 'LBO') { # London + $send_web = 'london'; } else { my ($council_email, $confirmed, $note) = dbh()->selectrow_array( "SELECT email,confirmed,note FROM contacts WHERE deleted='f' @@ -219,7 +224,6 @@ foreach my $row (@$unsent) { } else { push @recips, 'eha@' . mySociety::Config::get('EMAIL_DOMAIN'); } - push @recips, 'matthew@' . mySociety::Config::get('EMAIL_DOMAIN'); } # Special case for this parish council @@ -249,11 +253,15 @@ foreach my $row (@$unsent) { } } - if ($send_web) { + if ($send_web eq 'easthants') { $h{message} = construct_easthants_message(%h); if (!$nomail) { $result *= post_easthants_message(%h); } + } elsif ($send_web eq 'london') { + if (!$nomail) { + $result *= post_london_report(%h); + } } if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) { @@ -306,6 +314,8 @@ sub oxfordshire_contact { return "$email\@oxfordshire.gov.uk"; } +# East Hampshire + sub construct_easthants_message { my %h = @_; my $message = ''; @@ -346,6 +356,67 @@ sub post_easthants_message { return $return; } +# London + +sub post_london_report { + my %h = @_; + my $phone = $h{phone}; + my $mobile = ''; + if ($phone =~ /^\s*07/) { + $mobile = $phone; + $phone = ''; + } + my ($first, $last) = $h{name} =~ /^(\S*)(?: (.*))?$/; + my %params = ( + Key => mySociety::Config::get('LONDON_REPORTIT_KEY'), + Signature => Digest::MD5::md5_hex( $h{confirmed} . mySociety::Config::get('LONDON_REPORTIT_SECRET') ), + Type => Utils::london_categories()->{$h{category}}, + RequestDate => $h{confirmed}, + RequestMethod => 'Web', + ExternalId => $h{url}, + 'Customer.Title' => '', + 'Customer.FirstName' => $first, + 'Customer.Surname' => $last, + 'Customer.Email' => $h{email}, + 'Customer.Phone' => $phone, + 'Customer.Mobile' => $mobile, + ); + if ($h{used_map}) { + $params{'Location.Latitude'} = $h{latitude}; + $params{'Location.Longitude'} = $h{longitude}; + } elsif (mySociety::PostcodeUtil::is_valid_postcode($h{query})) { + # Didn't use map, and entered postcode, so use that. + $params{'Location.Postcode'} = $h{query}; + } else { + # Otherwise, lat/lon is all we have, even if it's wrong. + $params{'Location.Latitude'} = $h{latitude}; + $params{'Location.Longitude'} = $h{longitude}; + } + if ($h{has_photo}) { + $params{'Document1.Name'} = 'Photograph'; + $params{'Document1.MimeType'} = 'image/jpeg'; + $params{'Document1.URL'} = $h{image_url}; + $params{'Document1.URLPublic'} = 'true'; + } + my $browser = LWP::UserAgent->new; + my $response = $browser->post( mySociety::Config::get('LONDON_REPORTIT_URL'), \%params ); + my $out = $response->content; + if ($response->code ne 200) { + print "Failed to post $h{id} to London API, response was " . $response->code . " $out\n"; + return 1; + } + my ($id) = $out =~ /<caseid>(.*?)<\/caseid>/; + my ($org) = $out =~ /<organisation>(.*?)<\/organisation>/; + my ($team) = $out =~ /<team>(.*?)<\/team>/; + + $org = london_lookup($org); + dbh()->do("update problem set external_id=?, external_body=?, external_team=? where id=?", {}, + $id, $org, $team, $h{id}); + return 0; +} + +# Nearest things + sub find_closest { my ($row, $latitude, $longitude) = @_; my $str = ''; @@ -374,3 +445,18 @@ sub find_closest { return $str; } +sub london_lookup { + my $org = shift; + my $str = "Unknown ($org)"; + open(FP, "$FindBin::Bin/../data/dft.csv"); + while (<FP>) { + /^(.*?),(.*)/; + if ($org eq $1) { + $str = $2; + last; + } + } + close FP; + return $str; +} + |