diff options
Diffstat (limited to 'perllib/FixMyStreet/SendReport')
-rw-r--r-- | perllib/FixMyStreet/SendReport/Barnet.pm | 31 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/Email.pm | 53 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/EmptyHomes.pm | 30 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/London.pm | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/NI.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/Noop.pm | 12 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/Open311.pm | 100 | ||||
-rw-r--r-- | perllib/FixMyStreet/SendReport/Zurich.pm | 73 |
8 files changed, 205 insertions, 103 deletions
diff --git a/perllib/FixMyStreet/SendReport/Barnet.pm b/perllib/FixMyStreet/SendReport/Barnet.pm index 9a92686ec..07adb4c33 100644 --- a/perllib/FixMyStreet/SendReport/Barnet.pm +++ b/perllib/FixMyStreet/SendReport/Barnet.pm @@ -4,37 +4,15 @@ use Moose; BEGIN { extends 'FixMyStreet::SendReport'; } -use BarnetInterfaces::service::ZLBB_SERVICE_ORDER; use Encode; use Utils; use mySociety::Config; use mySociety::Web qw(ent); -# maximum number of webservice attempts to send before not trying any more (XXX may be better in config?) -use constant SEND_FAIL_RETRIES_CUTOFF => 3; - # specific council numbers use constant COUNCIL_ID_BARNET => 2489; use constant MAX_LINE_LENGTH => 132; -sub should_skip { - my $self = shift; - my $row = shift; - - my $council_name = 'Barnet'; - my $err_msg = ""; - - if ($row->send_fail_count >= SEND_FAIL_RETRIES_CUTOFF) { - $council_name &&= " to $council_name"; - $err_msg = "skipped: problem id=" . $row->id . " send$council_name has failed " - . $row->send_fail_count . " times, cutoff is " . SEND_FAIL_RETRIES_CUTOFF; - - $self->skipped( $err_msg ); - - return 1; - } -} - sub construct_message { my %h = @_; my $message = <<EOF; @@ -66,6 +44,7 @@ sub send { my $geo_code = "$h{easting} $h{northing}"; + require BarnetInterfaces::service::ZLBB_SERVICE_ORDER; my $interface = BarnetInterfaces::service::ZLBB_SERVICE_ORDER->new(); my ($nearest_postcode, $nearest_street) = ('', ''); @@ -80,9 +59,9 @@ sub send { ? $h{query} : $nearest_postcode; # use given postcode if available # note: endpoint can be of form 'https://username:password@url' - my $council_config = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => COUNCIL_ID_BARNET} )->first; - if ($council_config and $council_config->endpoint) { - $interface->set_proxy($council_config->endpoint); + my $body = FixMyStreet::App->model("DB::Body")->search( { 'body_areas.area_id' => COUNCIL_ID_BARNET }, { join => "body_areas" } )->first; + if ($body and $body->endpoint) { + $interface->set_proxy($body->endpoint); # Barnet web service doesn't like namespaces in the elements so use a prefix $interface->set_prefix('urn'); # uncomment these lines to print XML that will be sent rather @@ -90,7 +69,7 @@ sub send { #$interface->outputxml(1); #$interface->no_dispatch(1); } else { - die "Barnet webservice FAIL: looks like you're missing some config data: no endpoint (URL) found for area_id=" . COUNCIL_ID_BARNET; + die "Barnet webservice FAIL: looks like you're missing some config data: no endpoint (URL) found for area ID " . COUNCIL_ID_BARNET; } eval { diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm index b69436dd7..d71700020 100644 --- a/perllib/FixMyStreet/SendReport/Email.pm +++ b/perllib/FixMyStreet/SendReport/Email.pm @@ -11,30 +11,46 @@ sub build_recipient_list { my %recips; my $all_confirmed = 1; - foreach my $council ( keys %{ $self->councils } ) { + foreach my $body ( @{ $self->bodies } ) { my $contact = FixMyStreet::App->model("DB::Contact")->find( { deleted => 0, - area_id => $council, + body_id => $body->id, category => $row->category } ); - my ($council_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note ); + my ($body_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'; + $body_email = essex_contact($row->latitude, $row->longitude) if $body->areas->{2225}; + $body_email = oxfordshire_contact($row->latitude, $row->longitude) if $body->areas->{2237} && $body_email eq 'SPECIAL'; unless ($confirmed) { $all_confirmed = 0; - $note = 'Council ' . $row->council . ' deleted' + $note = 'Body ' . $row->bodies_str . ' deleted' unless $note; - $council_email = 'N/A' unless $council_email; - $self->unconfirmed_counts->{$council_email}{$row->category}++; - $self->unconfirmed_notes->{$council_email}{$row->category} = $note; + $body_email = 'N/A' unless $body_email; + $self->unconfirmed_counts->{$body_email}{$row->category}++; + $self->unconfirmed_notes->{$body_email}{$row->category} = $note; } - push @{ $self->to }, [ $council_email, $self->councils->{ $council }->{info}->{name} ]; - $recips{$council_email} = 1; + my $body_name = $body->name; + # see something uses council areas but doesn't send to councils so just use a + # generic name here to minimise confusion + if ( $row->cobrand eq 'seesomething' ) { + $body_name = 'See Something, Say Something'; + } + + my @emails; + # allow multiple emails per contact + if ( $body_email =~ /,/ ) { + @emails = split(/,/, $body_email); + } else { + @emails = ( $body_email ); + } + for my $email ( @emails ) { + push @{ $self->to }, [ $email, $body_name ]; + $recips{$email} = 1; + } } return () unless $all_confirmed; @@ -45,7 +61,7 @@ sub get_template { my ( $self, $row ) = @_; my $template = 'submit.txt'; - $template = 'submit-brent.txt' if $row->council eq 2488 || $row->council eq 2237; + $template = 'submit-brent.txt' if $row->bodies_str eq 2488 || $row->bodies_str eq 2237; my $template_path = FixMyStreet->path_to( "templates", "email", $row->cobrand, $row->lang, $template )->stringify; $template_path = FixMyStreet->path_to( "templates", "email", $row->cobrand, $template )->stringify unless -e $template_path; @@ -55,15 +71,20 @@ sub get_template { return $template; } +sub send_from { + my ( $self, $row ) = @_; + return [ $row->user->email, $row->name ]; +} + sub send { my $self = shift; my ( $row, $h ) = @_; my @recips = $self->build_recipient_list( $row, $h ); - # on a staging server send emails to ourselves rather than the councils - if (mySociety::Config::get('STAGING_SITE') && !FixMyStreet->test_mode) { - @recips = ( mySociety::Config::get('CONTACT_EMAIL') ); + # on a staging server send emails to ourselves rather than the bodies + if (mySociety::Config::get('STAGING_SITE') && !mySociety::Config::get('SEND_REPORTS_ON_STAGING') && !FixMyStreet->test_mode) { + @recips = ( $row->user->email ); } unless ( @recips ) { @@ -77,7 +98,7 @@ sub send { _template_ => $self->get_template( $row ), _parameters_ => $h, To => $self->to, - From => [ $row->user->email, $row->name ], + From => $self->send_from( $row ), }, mySociety::Config::get('CONTACT_EMAIL'), \@recips, diff --git a/perllib/FixMyStreet/SendReport/EmptyHomes.pm b/perllib/FixMyStreet/SendReport/EmptyHomes.pm index 4a6f058fe..4bae6af46 100644 --- a/perllib/FixMyStreet/SendReport/EmptyHomes.pm +++ b/perllib/FixMyStreet/SendReport/EmptyHomes.pm @@ -3,6 +3,8 @@ package FixMyStreet::SendReport::EmptyHomes; use Moose; use namespace::autoclean; +use mySociety::MaPit; + BEGIN { extends 'FixMyStreet::SendReport::Email'; } sub build_recipient_list { @@ -10,35 +12,41 @@ sub build_recipient_list { my %recips; my $all_confirmed = 1; - foreach my $council ( keys %{ $self->councils } ) { + foreach my $body ( @{ $self->bodies } ) { my $contact = FixMyStreet::App->model("DB::Contact")->find( { deleted => 0, - area_id => $council, + body_id => $body->id, category => 'Empty property', } ); - my ($council_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note ); + my ($body_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note ); unless ($confirmed) { $all_confirmed = 0; - #$note = 'Council ' . $row->council . ' deleted' + #$note = 'Council ' . $row->body . ' deleted' #unless $note; - $council_email = 'N/A' unless $council_email; - #$notgot{$council_email}{$row->category}++; - #$note{$council_email}{$row->category} = $note; + $body_email = 'N/A' unless $body_email; + #$notgot{$body_email}{$row->category}++; + #$note{$body_email}{$row->category} = $note; } - push @{ $self->to }, [ $council_email, $self->councils->{ $council }->{ info }->{name} ]; - $recips{$council_email} = 1; + push @{ $self->to }, [ $body_email, $body->name ]; + $recips{$body_email} = 1; - my $country = $self->councils->{$council}->{country}; + my $area_info = mySociety::MaPit::call('area', $body->body_areas->first->area_id); + my $country = $area_info->{country}; if ($country eq 'W') { - $recips{ 'shelter@' . mySociety::Config::get('EMAIL_DOMAIN') } = 1; + $recips{ 'wales@' . mySociety::Config::get('EMAIL_DOMAIN') } = 1; + } elsif ($country eq 'S') { + $recips{ 'scotland@' . mySociety::Config::get('EMAIL_DOMAIN') } = 1; } else { $recips{ 'eha@' . mySociety::Config::get('EMAIL_DOMAIN') } = 1; } } + # Set address email parameter from added data + $h->{address} = $row->extra->{address}; + return () unless $all_confirmed; return keys %recips; } diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm index 6e7951922..2c48a091c 100644 --- a/perllib/FixMyStreet/SendReport/London.pm +++ b/perllib/FixMyStreet/SendReport/London.pm @@ -79,7 +79,6 @@ sub send { 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"; $self->error( "Failed to post $h->{id} to London API, response was " . $response->code . " $out" ); return 1; } diff --git a/perllib/FixMyStreet/SendReport/NI.pm b/perllib/FixMyStreet/SendReport/NI.pm index 810ee60e2..e0ea24f9c 100644 --- a/perllib/FixMyStreet/SendReport/NI.pm +++ b/perllib/FixMyStreet/SendReport/NI.pm @@ -9,10 +9,10 @@ sub build_recipient_list { my %recips; my $all_confirmed = 1; - foreach my $council ( keys %{ $self->councils } ) { + foreach my $body ( @{ $self->bodies } ) { my $contact = FixMyStreet::App->model("DB::Contact")->find( { deleted => 0, - area_id => $council, + body_id => $body->id, category => $row->category } ); @@ -23,10 +23,10 @@ sub build_recipient_list { $email = 'N/A' unless $email; } - my $name = $self->councils->{$council}->{info}->{name}; + my $name = $body->name; if ( $email =~ /^roads.([^@]*)\@drdni/ ) { $name = "Roads Service (\u$1)"; - $h->{councils_name} = $name; + $h->{bodies_name} = $name; $row->external_body( 'Roads Service' ); } push @{ $self->to }, [ $email, $name ]; diff --git a/perllib/FixMyStreet/SendReport/Noop.pm b/perllib/FixMyStreet/SendReport/Noop.pm new file mode 100644 index 000000000..f2e0a3bdb --- /dev/null +++ b/perllib/FixMyStreet/SendReport/Noop.pm @@ -0,0 +1,12 @@ +package FixMyStreet::SendReport::Noop; + +use Moose; + +BEGIN { extends 'FixMyStreet::SendReport'; } + +# Always skip when using this method +sub should_skip { + return 1; +} + +1; diff --git a/perllib/FixMyStreet/SendReport/Open311.pm b/perllib/FixMyStreet/SendReport/Open311.pm index 8d7a418af..ce5ee80a0 100644 --- a/perllib/FixMyStreet/SendReport/Open311.pm +++ b/perllib/FixMyStreet/SendReport/Open311.pm @@ -9,17 +9,9 @@ use FixMyStreet::App; use mySociety::Config; use DateTime::Format::W3CDTF; use Open311; +use Readonly; -sub should_skip { - my $self = shift; - my $row = shift; - - if ( $row->send_fail_count > 0 ) { - if ( bromley_retry_timeout($row) ) { - return 1; - } - } -} +Readonly::Scalar my $COUNCIL_ID_OXFORDSHIRE => 2237; sub send { my $self = shift; @@ -27,17 +19,22 @@ sub send { my $result = -1; - foreach my $council ( keys %{ $self->councils } ) { - my $conf = $self->councils->{$council}->{config}; + foreach my $body ( @{ $self->bodies } ) { + my $conf = $self->body_config->{ $body->id }; my $always_send_latlong = 1; my $send_notpinpointed = 0; my $use_service_as_deviceid = 0; - my $basic_desc = 0; + my $extended_desc = 1; + + # To rollback temporary changes made by this function + my $revert = 0; # Extra bromley fields - if ( $row->council =~ /2482/ ) { + if ( $row->bodies_str eq '2482' ) { + + $revert = 1; my $extra = $row->extra; if ( $row->used_map || ( !$row->used_map && !$row->postcode ) ) { @@ -49,7 +46,7 @@ sub send { push @$extra, { name => 'report_title', value => $row->title }; push @$extra, { name => 'public_anonymity_required', value => $row->anonymous ? 'TRUE' : 'FALSE' }; push @$extra, { name => 'email_alerts_requested', value => 'FALSE' }; # always false as can never request them - push @$extra, { name => 'requested_datetime', value => DateTime::Format::W3CDTF->format_datetime($row->confirmed_local->set_nanosecond(0)) }; + push @$extra, { name => 'requested_datetime', value => DateTime::Format::W3CDTF->format_datetime($row->confirmed->set_nanosecond(0)) }; push @$extra, { name => 'email', value => $row->user->email }; $row->extra( $extra ); @@ -60,66 +57,92 @@ sub send { # make sure we have last_name attribute present in row's extra, so # it is passed correctly to Bromley as attribute[] if ( $row->cobrand ne 'bromley' ) { - my ( $firstname, $lastname ) = ( $row->user->name =~ /(\w+)\.?\s+(.+)/ ); + my ( $firstname, $lastname ) = ( $row->name =~ /(\w+)\.?\s+(.+)/ ); push @$extra, { name => 'last_name', value => $lastname }; } - $basic_desc = 1; + $extended_desc = 0; + } + + # extra Oxfordshire fields: send nearest street, postcode, northing and easting, and the FMS id + if ( $row->bodies_str =~ /$COUNCIL_ID_OXFORDSHIRE/ ) { + + my $extra = $row->extra; + push @$extra, { name => 'external_id', value => $row->id }; + push @$extra, { name => 'closest_address', value => $h->{closest_address} } if $h->{closest_address}; + if ( $row->used_map || ( !$row->used_map && !$row->postcode ) ) { + push @$extra, { name => 'northing', value => $h->{northing} }; + push @$extra, { name => 'easting', value => $h->{easting} }; + } + $row->extra( $extra ); + + $extended_desc = 'oxfordshire'; } # FIXME: we've already looked this up before my $contact = FixMyStreet::App->model("DB::Contact")->find( { deleted => 0, - area_id => $conf->area_id, + body_id => $body->id, category => $row->category } ); - my $open311 = Open311->new( + my %open311_params = ( jurisdiction => $conf->jurisdiction, endpoint => $conf->endpoint, api_key => $conf->api_key, always_send_latlong => $always_send_latlong, send_notpinpointed => $send_notpinpointed, use_service_as_deviceid => $use_service_as_deviceid, - basic_description => $basic_desc, + extended_description => $extended_desc, ); + if (FixMyStreet->test_mode) { + my $test_res = HTTP::Response->new(); + $test_res->code(200); + $test_res->message('OK'); + $test_res->content('<?xml version="1.0" encoding="utf-8"?><service_requests><request><service_request_id>248</service_request_id></request></service_requests>'); + $open311_params{test_mode} = 1; + $open311_params{test_get_returns} = { 'requests.xml' => $test_res }; + } + + my $open311 = Open311->new( %open311_params ); # non standard west berks end points - if ( $row->council =~ /2619/ ) { + if ( $row->bodies_str =~ /2619/ ) { $open311->endpoints( { services => 'Services', requests => 'Requests' } ); } + # non-standard Oxfordshire endpoint (because it's just a script, not a full Open311 service) + if ( $row->bodies_str =~ /$COUNCIL_ID_OXFORDSHIRE/ ) { + $open311->endpoints( { requests => 'open311_service_request.cgi' } ); + $revert = 1; + } + # required to get round issues with CRM constraints - if ( $row->council =~ /2218/ ) { + if ( $row->bodies_str =~ /2218/ ) { $row->user->name( $row->user->id . ' ' . $row->user->name ); + $revert = 1; } if ($row->cobrand eq 'fixmybarangay') { - # FixMyBarangay endpoints expect external_id as an attribute + # FixMyBarangay endpoints expect external_id as an attribute, as do Oxfordshire $row->extra( [ { 'name' => 'external_id', 'value' => $row->id } ] ); + $revert = 1; } 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->council =~ /2482/ || $row->cobrand eq 'fixmybarangay') { - $row->discard_changes(); - } + $row->discard_changes() if $revert; if ( $resp ) { $row->external_id( $resp ); $row->send_method_used('Open311'); - if ($row->cobrand eq 'fixmybarangay') { - # currently the only external body using Open311 is DPS - # (this will change when we have 'body' logic in place, meanwhile: hardcoded) - $row->external_body("DPS"); - } $result *= 0; $self->success( 1 ); } else { $result *= 1; # temporary fix to resolve some issues with west berks - if ( $row->council =~ /2619/ ) { + if ( $row->bodies_str =~ /2619/ ) { $result *= 0; } } @@ -130,17 +153,4 @@ sub send { return $result; } -sub bromley_retry_timeout { - my $row = shift; - - my $tz = DateTime::TimeZone->new( name => 'local' ); - my $now = DateTime->now( time_zone => $tz ); - my $diff = $now - $row->send_fail_timestamp; - if ( $diff->in_units( 'minutes' ) < 30 ) { - return 1; - } - - return 0; -} - 1; diff --git a/perllib/FixMyStreet/SendReport/Zurich.pm b/perllib/FixMyStreet/SendReport/Zurich.pm new file mode 100644 index 000000000..d46561e9e --- /dev/null +++ b/perllib/FixMyStreet/SendReport/Zurich.pm @@ -0,0 +1,73 @@ +package FixMyStreet::SendReport::Zurich; + +use Moose; + +BEGIN { extends 'FixMyStreet::SendReport::Email'; } + +sub build_recipient_list { + my ( $self, $row, $h ) = @_; + + # Only one body ever, most of the time with an email endpoint + my $body = @{ $self->bodies }[0]; + if ( $row->external_body ) { + $body = FixMyStreet::App->model("DB::Body")->find( { id => $row->external_body } ); + $h->{bodies_name} = $body->name; + } + my $body_email = $body->endpoint; + + my $parent = $body->parent; + if ($parent && !$parent->parent) { + # Division, might have an individual contact email address + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + body_id => $body->id, + category => $row->category + } ); + $body_email = $contact->email if $contact && $contact->email; + } + + push @{ $self->to }, [ $body_email, $body->name ]; + return $body_email; +} + +sub get_template { + my ( $self, $row ) = @_; + + my $template; + if ( $row->state eq 'unconfirmed' || $row->state eq 'confirmed' ) { + $template = 'submit.txt'; + } elsif ( $row->state eq 'in progress' ) { + $template = 'submit-in-progress.txt'; + } elsif ( $row->state eq 'planned' ) { + $template = 'submit-feedback-pending.txt'; + } elsif ( $row->state eq 'closed' ) { + $template = 'submit-external.txt'; + if ( $row->extra->{third_personal} ) { + $template = 'submit-external-personal.txt'; + } + } + + my $template_path = FixMyStreet->path_to( "templates", "email", "zurich", $template )->stringify; + $template = Utils::read_file( $template_path ); + return $template; +} + +# Zurich emails come from the site itself, unless it's to an external body, +# in which case it's from the category/body +sub send_from { + my ( $self, $row ) = @_; + + if ( $row->external_body ) { + my $body = @{ $self->bodies }[0]; + my $body_email = $body->endpoint; + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + body_id => $body->id, + category => $row->category + } ); + $body_email = $contact->email if $contact && $contact->email; + return [ $body_email, FixMyStreet->config('CONTACT_NAME') ]; + } + + return [ FixMyStreet->config('CONTACT_EMAIL'), FixMyStreet->config('CONTACT_NAME') ]; +} + +1; |