diff options
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/FixMyStreet.pm | 13 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 290 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/EastHants.pm | 62 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/Email.pm | 126 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/London.pm | 115 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/Open311.pm | 70 | ||||
-rw-r--r-- | t/app/model/problem.t | 16 |
9 files changed, 430 insertions, 271 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index aacfb5e2b..6af538657 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -914,5 +914,7 @@ Get stats to display on the council reports page sub get_report_stats { return 0; } +sub get_council_sender { return 'Email' }; + 1; diff --git a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm index 37fa7a16d..9958e6215 100644 --- a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm +++ b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm @@ -6,5 +6,18 @@ sub restriction { return {}; } +sub get_council_sender { + my ( $self, $area_id, $area_info ) = shift; + + my $sender_conf = mySociety::Config::get( 'SENDERS' ); + return $sender_conf->{ $council } if exists $sender_conf->{ $council }; + + return 'London' if $area_info->{type} eq 'LBO'; + + return 'Open311' if FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $council, endpoint => { '!=', '' } } )->first; + + return 'Email'; +} + 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index ec8f5357d..e87211716 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -4,6 +4,8 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +use FixMyStreet::SendReport::Email; + my $site_restriction; my $site_key; @@ -293,6 +295,7 @@ while (my $row = $unsent->next) { $h{closest_address} = $cobrand->find_closest( $h{latitude}, $h{longitude}, $row ); } + my %reporters = (); my (@to, @recips, $template, $areas_info, @open311_councils); if ($site eq 'emptyhomes') { @@ -327,39 +330,16 @@ while (my $row = $unsent->next) { $areas_info = mySociety::MaPit::call('areas', \@all_councils); my (@dear, %recips); my $all_confirmed = 1; + foreach my $council (@councils) { my $name = $areas_info->{$council}->{name}; push @dear, $name; - if ($council == 2330) { # E. Hants have a web service - $send_web = 'easthants'; - $h{category} = 'Customer Services' if $h{category} eq 'Other'; - } elsif ($areas_info->{$council}->{type} eq 'LBO') { # London - $send_web = 'london'; - } elsif ( my $endpoint = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $council, endpoint => { '!=', '' } } )->first ) { - push @open311_councils, $endpoint; - $send_web = 'open311'; - } else { - my $contact = FixMyStreet::App->model("DB::Contact")->find( { - deleted => 0, - area_id => $council, - category => $row->category - } ); - my ($council_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note ); - $council_email = essex_contact($row->latitude, $row->longitude) if $council == 2225; - $council_email = oxfordshire_contact($row->latitude, $row->longitude) if $council == 2237 && $council_email eq 'SPECIAL'; - unless ($confirmed) { - $all_confirmed = 0; - $note = 'Council ' . $row->council . ' deleted' - unless $note; - $council_email = 'N/A' unless $council_email; - $notgot{$council_email}{$row->category}++; - $note{$council_email}{$row->category} = $note; - } - push @to, [ $council_email, $name ]; - $recips{$council_email} = 1; - $send_email = 1; - } + my $sender = $cobrand->get_council_sender( $council, $areas_info->{$council} ); + $sender = "FixMyStreet::SendReport::$sender"; + $reporters{ $sender } = $sender->new() unless $reporters{$sender}; + $reporters{ $sender }->add_council( $council, $name ); } + @recips = keys %recips; next unless $all_confirmed; @@ -396,7 +376,7 @@ while (my $row = $unsent->next) { } - unless ($send_email || $send_web) { + unless ($send_email || $send_web || keys %reporters ) { die 'Report not going anywhere for ID ' . $row->id . '!'; } @@ -405,6 +385,9 @@ while (my $row = $unsent->next) { @recips = ( mySociety::Config::get('CONTACT_EMAIL') ); $send_web = 0; $send_email = 1; + %reporters = ( + 'FixMyStreet::SendReport::Email' => $reporters{ 'FixMyStreet::SendReport::Email' } + ); } elsif ($site eq 'emptyhomes') { my $council = $row->council; my $country = $areas_info->{$council}->{country}; @@ -426,75 +409,15 @@ while (my $row = $unsent->next) { # Multiply results together, so one success counts as a success. my $result = -1; - if ($send_email) { - $result *= FixMyStreet::App->send_email_cron( - { - _template_ => $template, - _parameters_ => \%h, - To => \@to, - From => [ $row->user->email, $row->name ], - }, - mySociety::Config::get('CONTACT_EMAIL'), - \@recips, - $nomail + for my $sender ( keys %reporters ) { + $result *= $reporters{ $sender }->send( + $row, \%h, \@to, $template, \@recips, $nomail ); } - - if ($send_web eq 'easthants') { - $h{message} = construct_easthants_message(%h); - if (!$nomail) { - $result *= post_easthants_message(%h); - } - } elsif ($send_web eq 'london') { - $h{message} = construct_london_message(%h); - if (!$nomail) { - $result *= post_london_report( $row, %h ); - } - } elsif ($send_web eq 'open311') { - foreach my $conf ( @open311_councils ) { - print 'posting to end point for ' . $conf->area_id . "\n" if $verbose; - - my $contact = FixMyStreet::App->model("DB::Contact")->find( { - deleted => 0, - area_id => $conf->area_id, - category => $row->category - } ); - - my $open311 = Open311->new( - jurisdiction => $conf->jurisdiction, - endpoint => $conf->endpoint, - api_key => $conf->api_key, - ); - - # non standard west berks end points - if ( $row->council =~ /2619/ ) { - $open311->endpoints( { services => 'Services', requests => 'Requests' } ); - } - - # required to get round issues with CRM constraints - if ( $row->council =~ /2218/ ) { - $row->user->name( $row->user->id . ' ' . $row->user->name ); - } - - my $resp = $open311->send_service_request( $row, \%h, $contact->email ); - - # make sure we don't save user changes from above - if ( $row->council =~ /2218/ ) { - $row->discard_changes(); - } - - if ( $resp ) { - $row->external_id( $resp ); - $result *= 0; - } else { - $result *= 1; - # temporary fix to resolve some issues with west berks - if ( $row->council =~ /2619/ ) { - $result *= 0; - } - } - } - } + #if ($send_email) { + #$result *= FixMyStreet::SendReport::Email::send( + #); + #} if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) { $row->update( { @@ -514,177 +437,4 @@ if ($verbose) { } } -sub _get_district_for_contact { - my ( $lat, $lon ) = @_; - my $district = - mySociety::MaPit::call( 'point', "4326/$lon,$lat", type => 'DIS' ); - ($district) = keys %$district; - return $district; -} - -# Essex has different contact addresses depending upon the district -# Might be easier if we start storing in the db all areas covered by a point -# Will do for now :) -sub essex_contact { - my $district = _get_district_for_contact(@_); - my $email; - $email = 'eastarea' if $district == 2315 || $district == 2312; - $email = 'midarea' if $district == 2317 || $district == 2314 || $district == 2316; - $email = 'southarea' if $district == 2319 || $district == 2320 || $district == 2310; - $email = 'westarea' if $district == 2309 || $district == 2311 || $district == 2318 || $district == 2313; - die "Returned district $district which is not in Essex!" unless $email; - return "highways.$email\@essexcc.gov.uk"; -} - -# Oxfordshire has different contact addresses depending upon the district -sub oxfordshire_contact { - my $district = _get_district_for_contact(@_); - my $email; - $email = 'northernarea' if $district == 2419 || $district == 2420 || $district == 2421; - $email = 'southernarea' if $district == 2417 || $district == 2418; - die "Returned district $district which is not in Oxfordshire!" unless $email; - return "$email\@oxfordshire.gov.uk"; -} - -# East Hampshire - -sub construct_easthants_message { - my %h = @_; - my $message = ''; - $message .= "[ This report was also sent to the district council covering the location of the problem, as the user did not categorise it; please ignore if you're not the correct council to deal with the issue. ]\n\n" - if $h{multiple}; - $message .= <<EOF; -Subject: $h{title} - -Details: $h{detail} - -$h{fuzzy}, or to provide an update on the problem, please visit the following link: - -$h{url} - -$h{closest_address} -EOF - return $message; -} - -my $eh_service; -sub post_easthants_message { - my %h = @_; - my $return = 1; - $eh_service ||= EastHantsWSDL->on_fault(sub { my($soap, $res) = @_; die ref $res ? $res->faultstring : $soap->transport->status, "\n"; }); - try { - # ServiceName, RemoteCreatedBy, Salutation, FirstName, Name, Email, Telephone, HouseNoName, Street, Town, County, Country, Postcode, Comments, FurtherInfo, ImageURL - my $message = ent(encode_utf8($h{message})); - my $name = ent(encode_utf8($h{name})); - my $result = $eh_service->INPUTFEEDBACK( - $h{category}, 'FixMyStreet', '', '', $name, $h{email}, $h{phone}, - '', '', '', '', '', '', $message, 'Yes', $h{image_url} - ); - $return = 0 if $result eq 'Report received'; - } otherwise { - my $e = shift; - print "Caught an error: $e\n"; - }; - return $return; -} - -# London - -sub construct_london_message { - my %h = @_; - return <<EOF, -A user of FixMyStreet has submitted the following report of a local -problem that they believe might require your attention. - -Subject: $h{title} - -Details: $h{detail} - -$h{fuzzy}, or to provide an update on the problem, please visit the -following link: - -$h{url} - -$h{closest_address} -Yours, -The FixMyStreet team -EOF -} - -sub post_london_report { - my ( $problem, %h ) = @_; - my $phone = $h{phone}; - my $mobile = ''; - if ($phone && $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, - 'ProblemDescription' => $h{message}, - ); - 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); - $problem->external_id( $id ); - $problem->external_body( $org ); - $problem->external_team( $team ); - return 0; -} - -# Nearest things - -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; -} - 1; diff --git a/perllib/FixMyStreet/SendReport.pm b/perllib/FixMyStreet/SendReport.pm new file mode 100644 index 000000000..3a79bd82a --- /dev/null +++ b/perllib/FixMyStreet/SendReport.pm @@ -0,0 +1,7 @@ +package FixMyStreet::SendReport; + +use Moose; + + + +1; diff --git a/perllib/FixMyStreet/SendReport/EastHants.pm b/perllib/FixMyStreet/SendReport/EastHants.pm new file mode 100644 index 000000000..768587b03 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/EastHants.pm @@ -0,0 +1,62 @@ +package FixMyStreet::SendReport::Email; + +my %councils = (); +my @to; + +sub reset { + %councils = (); + @to = (); +} + +sub add_council { + my $council = shift; + my $name = shift; + + $councils{ $council } = $name; +} + +sub construct_message { + my %h = @_; + my $message = ''; + $message .= "[ This report was also sent to the district council covering the location of the problem, as the user did not categorise it; please ignore if you're not the correct council to deal with the issue. ]\n\n" + if $h{multiple}; + $message .= <<EOF; +Subject: $h{title} + +Details: $h{detail} + +$h{fuzzy}, or to provide an update on the problem, please visit the following link: + +$h{url} + +$h{closest_address} +EOF + return $message; +} + +sub send { + return if mySociety::Config::get('STAGING_SITE'); + + my ( $row, $h, $to, $template, $recips, $nomail ) = @_; + + $h->{category} = 'Customer Services' if $h->{category} eq 'Other'; + $h->{message} = construct_message( %$h ); + my $return = 1; + $eh_service ||= EastHantsWSDL->on_fault(sub { my($soap, $res) = @_; die ref $res ? $res->faultstring : $soap->transport->status, "\n"; }); + try { + # ServiceName, RemoteCreatedBy, Salutation, FirstName, Name, Email, Telephone, HouseNoName, Street, Town, County, Country, Postcode, Comments, FurtherInfo, ImageURL + my $message = ent(encode_utf8($h->{message})); + my $name = ent(encode_utf8($h->{name})); + my $result = $eh_service->INPUTFEEDBACK( + $h->{category}, 'FixMyStreet', '', '', $name, $h->{email}, $h->{phone}, + '', '', '', '', '', '', $message, 'Yes', $h->{image_url} + ); + $return = 0 if $result eq 'Report received'; + } otherwise { + my $e = shift; + print "Caught an error: $e\n"; + }; + return $return; +} + +1; diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm new file mode 100644 index 000000000..94cd94767 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/Email.pm @@ -0,0 +1,126 @@ +package FixMyStreet::SendReport::Email; + +use Moose; +use namespace::autoclean; +use Data::Printer; + +BEGIN { extends 'FixMyStreet::SendReport'; } + +has 'councils' => (is => 'rw', isa => 'HashRef', default => sub { {} } ); +has 'to' => (is => 'rw', isa => 'ArrayRef', default => sub { [] } ); + +my %councils = (); +my @to; + +sub reset { + my $self = shift; + + $self->councils( {} ); + $self->to( [] ); +} + +sub add_council { + my $self = shift; + my $council = shift; + my $name = shift; + + $self->councils->{ $council } = $name; +} + +sub build_recipient_list { + my $self = shift; + my $row = shift; + my %recips; + + my $all_confirmed = 1; + foreach my $council ( keys %{ $self->councils } ) { + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + deleted => 0, + area_id => $council, + category => $row->category + } ); + + my ($council_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note ); + + $council_email = essex_contact($row->latitude, $row->longitude) if $council == 2225; + $council_email = oxfordshire_contact($row->latitude, $row->longitude) if $council == 2237 && $council_email eq 'SPECIAL'; + + unless ($confirmed) { + $all_confirmed = 0; + #$note = 'Council ' . $row->council . ' deleted' + #unless $note; + $council_email = 'N/A' unless $council_email; + #$notgot{$council_email}{$row->category}++; + #$note{$council_email}{$row->category} = $note; + } + + push @{ $self->to }, [ $council_email, $self->councils->{ $council } ]; + $recips{$council_email} = 1; + } + + return () unless $all_confirmed; + return keys %recips; +} + +sub send { + my $self = shift; + my ( $row, $h, $to, $template, $recips, $nomail ) = @_; + + my @recips; + + # on a staging server send emails to ourselves rather than the councils + if (mySociety::Config::get('STAGING_SITE')) { + @recips = ( mySociety::Config::get('CONTACT_EMAIL') ); + } else { + @recips = $self->build_recipient_list( $row ); + } + + return unless @recips; + + my $result = FixMyStreet::App->send_email_cron( + { + _template_ => $template, + _parameters_ => $h, + To => $self->to, + From => [ $row->user->email, $row->name ], + }, + mySociety::Config::get('CONTACT_EMAIL'), + \@recips, + $nomail + ); + + return $result; +} + +# Essex has different contact addresses depending upon the district +# Might be easier if we start storing in the db all areas covered by a point +# Will do for now :) +sub essex_contact { + my $district = _get_district_for_contact(@_); + my $email; + $email = 'eastarea' if $district == 2315 || $district == 2312; + $email = 'midarea' if $district == 2317 || $district == 2314 || $district == 2316; + $email = 'southarea' if $district == 2319 || $district == 2320 || $district == 2310; + $email = 'westarea' if $district == 2309 || $district == 2311 || $district == 2318 || $district == 2313; + die "Returned district $district which is not in Essex!" unless $email; + return "highways.$email\@essexcc.gov.uk"; +} + +# Oxfordshire has different contact addresses depending upon the district +sub oxfordshire_contact { + my $district = _get_district_for_contact(@_); + my $email; + $email = 'northernarea' if $district == 2419 || $district == 2420 || $district == 2421; + $email = 'southernarea' if $district == 2417 || $district == 2418; + die "Returned district $district which is not in Oxfordshire!" unless $email; + return "$email\@oxfordshire.gov.uk"; +} + +sub _get_district_for_contact { + my ( $lat, $lon ) = @_; + my $district = + mySociety::MaPit::call( 'point', "4326/$lon,$lat", type => 'DIS' ); + ($district) = keys %$district; + return $district; +} +1; diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm new file mode 100644 index 000000000..3d4ce89c2 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/London.pm @@ -0,0 +1,115 @@ +package FixMyStreet::SendReport::Email; + +my %councils = (); +my @to; + +sub reset { + %councils = (); + @to = (); +} + +sub add_council { + my $council = shift; + my $name = shift; + + $councils{ $council } = $name; +} +sub construct_message { + my %h = @_; + return <<EOF, +A user of FixMyStreet has submitted the following report of a local +problem that they believe might require your attention. + +Subject: $h{title} + +Details: $h{detail} + +$h{fuzzy}, or to provide an update on the problem, please visit the +following link: + +$h{url} + +$h{closest_address} +Yours, +The FixMyStreet team +EOF +} + +sub send { + return if mySociety::Config::get('STAGING_SITE'); + my ( $row, $h, $to, $template, $recips, $nomail ) = @_; + + $h->{message} = construct_message( %$h ); + my $phone = $h->{phone}; + my $mobile = ''; + if ($phone && $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, + 'ProblemDescription' => $h->{message}, + ); + 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); + $problem->external_id( $id ); + $problem->external_body( $org ); + $problem->external_team( $team ); + return 0; +} + +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; +} + +1; diff --git a/perllib/FixMyStreet/SendReport/Open311.pm b/perllib/FixMyStreet/SendReport/Open311.pm new file mode 100644 index 000000000..a7f6e67d1 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/Open311.pm @@ -0,0 +1,70 @@ +package FixMyStreet::SendReport::Open311; + +my %councils = (); +my @to; + +sub reset { + %councils = (); + @to = (); +} + +sub add_council { + my $council = shift; + my $name = shift; + + $councils{ $council } = $name; +} + +sub send { + return if mySociety::Config::get('STAGING_SITE'); + my $self = shift; + my ( $row, $h, $to, $template, $recips, $nomail ) = @_; + foreach my $council ( keys %{ $self->councils } ) { + my $conf = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $self->councils->{ $council }, endpoint => { '!=', '' } } )->first; + #print 'posting to end point for ' . $conf->area_id . "\n" if $verbose; + + + # FIXME: we've already looked this up before + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + deleted => 0, + area_id => $conf->area_id, + category => $row->category + } ); + + my $open311 = Open311->new( + jurisdiction => $conf->jurisdiction, + endpoint => $conf->endpoint, + api_key => $conf->api_key, + ); + + # non standard west berks end points + if ( $row->council =~ /2619/ ) { + $open311->endpoints( { services => 'Services', requests => 'Requests' } ); + } + + # required to get round issues with CRM constraints + if ( $row->council =~ /2218/ ) { + $row->user->name( $row->user->id . ' ' . $row->user->name ); + } + + my $resp = $open311->send_service_request( $row, $h, $contact->email ); + + # make sure we don't save user changes from above + if ( $row->council =~ /2218/ ) { + $row->discard_changes(); + } + + if ( $resp ) { + $row->external_id( $resp ); + $result *= 0; + } else { + $result *= 1; + # temporary fix to resolve some issues with west berks + if ( $row->council =~ /2619/ ) { + $result *= 0; + } + } + } +} + +1; diff --git a/t/app/model/problem.t b/t/app/model/problem.t index 58c66ac74..1b80f00ea 100644 --- a/t/app/model/problem.t +++ b/t/app/model/problem.t @@ -348,6 +348,19 @@ for my $test ( my $mech = FixMyStreet::TestMech->new(); +FixMyStreet::App->model('DB::Contact')->find_or_create( + { + area_id => 2663, + category => 'potholes', + email => 'test@example.org', + confirmed => 1, + deleted => 0, + editor => 'test', + whenedited => \'ms_current_timestamp()', + note => '', + } +); + foreach my $test ( { } ) { @@ -362,10 +375,11 @@ foreach my $test ( { $problem->discard_changes; $problem->update( { - council => 2651, + council => 2663, state => 'confirmed', confirmed => \'ms_current_timestamp()', whensent => undef, + category => 'potholes', } ); FixMyStreet::App->model('DB::Problem')->send_reports(); |