aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/SendReport/Open311.pm
diff options
context:
space:
mode:
authorDave Whiteland <dave@mysociety.org>2012-05-29 15:57:41 +0100
committerDave Whiteland <dave@mysociety.org>2012-05-29 15:57:41 +0100
commit67da8efc720d2d0bd22bd9fe8655b7e983b35bb4 (patch)
tree38b8570647124df06c637d4b923f6010211ef328 /perllib/FixMyStreet/SendReport/Open311.pm
parent40b3a51d33caefa8f5fb97ce9be18ef936c7e260 (diff)
parent131ff6e9bf3626d6a8fff6ae54669d250148a63a (diff)
Merge remote branch 'origin/master' into fmb-read-only
Conflicts: bin/send-reports perllib/FixMyStreet/Cobrand/Default.pm perllib/FixMyStreet/Cobrand/FixMyStreet.pm templates/web/fixmystreet/alert/index.html templates/web/fixmystreet/around/display_location.html web/cobrands/fixmystreet/_layout.scss web/js/map-OpenLayers.js
Diffstat (limited to 'perllib/FixMyStreet/SendReport/Open311.pm')
-rw-r--r--perllib/FixMyStreet/SendReport/Open311.pm125
1 files changed, 125 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/SendReport/Open311.pm b/perllib/FixMyStreet/SendReport/Open311.pm
new file mode 100644
index 000000000..845a6295c
--- /dev/null
+++ b/perllib/FixMyStreet/SendReport/Open311.pm
@@ -0,0 +1,125 @@
+package FixMyStreet::SendReport::Open311;
+
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'FixMyStreet::SendReport'; }
+
+use FixMyStreet::App;
+use mySociety::Config;
+use DateTime::Format::W3CDTF;
+use Open311;
+
+sub should_skip {
+ my $self = shift;
+ my $row = shift;
+
+ if ( $row->send_fail_count > 0 ) {
+ if ( bromley_retry_timeout($row) ) {
+ return 1;
+ }
+ }
+}
+
+sub send {
+ my $self = shift;
+ my ( $row, $h, $to, $template, $recips, $nomail ) = @_;
+
+ my $result = -1;
+
+ foreach my $council ( keys %{ $self->councils } ) {
+ my $conf = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $council, endpoint => { '!=', '' } } )->first;
+
+ my $always_send_latlong = 1;
+ my $send_notpinpointed = 0;
+
+ my $basic_desc = 0;
+
+ # Extra bromley fields
+ if ( $row->council =~ /2482/ ) {
+
+ my $extra = $row->extra;
+ if ( $row->used_map || ( !$row->used_map && !$row->postcode ) ) {
+ push @$extra, { name => 'northing', value => $h->{northing} };
+ push @$extra, { name => 'easting', value => $h->{easting} };
+ }
+ push @$extra, { name => 'report_url', value => $h->{url} };
+ push @$extra, { name => 'service_request_id_ext', value => $row->id };
+ 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 => 'email', value => $row->user->email };
+ $row->extra( $extra );
+
+ $always_send_latlong = 0;
+ $send_notpinpointed = 1;
+
+ $basic_desc = 1;
+ }
+
+ # 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,
+ always_send_latlong => $always_send_latlong,
+ send_notpinpointed => $send_notpinpointed,
+ basic_description => $basic_desc,
+ );
+
+ # 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->council =~ /2482/ ) {
+ $row->discard_changes();
+ }
+
+ if ( $resp ) {
+ $row->external_id( $resp );
+ $result *= 0;
+ $self->success( 1 );
+ } else {
+ $result *= 1;
+ # temporary fix to resolve some issues with west berks
+ if ( $row->council =~ /2619/ ) {
+ $result *= 0;
+ }
+ }
+ }
+
+ $self->error( 'Failed to send over Open311' ) unless $self->success;
+
+ 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;