diff options
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 24 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 27 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bromley.pm | 28 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/FixMyStreet.pm | 26 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 51 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Open311conf.pm | 23 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 10 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 18 | ||||
-rw-r--r-- | perllib/Open311.pm | 208 | ||||
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 110 | ||||
-rw-r--r-- | perllib/Open311/PopulateServiceList.pm | 26 |
12 files changed, 506 insertions, 47 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index af4cdd5aa..7b630d36f 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -124,12 +124,16 @@ sub report_form_ajax : Path('ajax') : Args(0) { my $category = $c->render_fragment( 'report/new/category.html'); my $councils_text = $c->render_fragment( 'report/new/councils_text.html'); my $has_open311 = keys %{ $c->stash->{category_extras} }; + my $extra_name_info = $c->stash->{extra_name_info} + ? $c->render_fragment('report/new/extra_name.html') + : ''; my $body = JSON->new->utf8(1)->encode( { councils_text => $councils_text, category => $category, has_open311 => $has_open311, + extra_name_info => $extra_name_info, } ); @@ -394,6 +398,13 @@ sub initialize_report : Private { } + if ( $c->req->param('first_name') && $c->req->param('last_name') ) { + $c->stash->{first_name} = $c->req->param('first_name'); + $c->stash->{last_name} = $c->req->param('last_name'); + + $c->req->param( 'name', sprintf( '%s %s', $c->req->param('first_name'), $c->req->param('last_name') ) ); + } + # Capture whether the map was used $report->used_map( $c->req->param('skipped') ? 0 : 1 ); @@ -542,14 +553,14 @@ sub setup_categories_and_councils : Private { ); $category_label = _('Property type:'); - } elsif ($first_council->{type} eq 'LBO') { + } elsif ($first_council->{id} != 2482 && $first_council->{type} eq 'LBO') { $area_ids_to_list{ $first_council->{id} } = 1; @category_options = ( _('-- Pick a category --'), sort keys %{ Utils::london_categories() } ); - $category_label = _('Category:'); + $category_label = _('Category'); } else { @@ -579,7 +590,7 @@ sub setup_categories_and_councils : Private { if (@category_options) { @category_options = ( _('-- Pick a category --'), @category_options, _('Other') ); - $category_label = _('Category:'); + $category_label = _('Category'); } } @@ -589,6 +600,7 @@ sub setup_categories_and_councils : Private { $c->stash->{category_options} = \@category_options; $c->stash->{category_extras} = \%category_extras; $c->stash->{category_extras_json} = encode_json \%category_extras; + $c->stash->{extra_name_info} = $first_council->{id} == 2482 ? 1 : 0; my @missing_details_councils = grep { !$area_ids_to_list{$_} } # @@ -733,7 +745,7 @@ sub process_report : Private { $councils = join( ',', @{ $c->stash->{area_ids_to_list} } ) || -1; $report->council( $councils ); - } elsif ( $first_council->{type} eq 'LBO') { + } elsif ( $first_council->{id} != 2482 && $first_council->{type} eq 'LBO') { unless ( Utils::london_categories()->{ $report->category } ) { $c->stash->{field_errors}->{category} = _('Please choose a category'); @@ -785,6 +797,8 @@ sub process_report : Private { }; } + $c->cobrand->process_extras( $c, \@contacts, \@extra ); + if ( @extra ) { $c->stash->{report_meta} = \@extra; $report->extra( \@extra ); @@ -916,6 +930,8 @@ sub check_for_errors : Private { %{ $c->stash->{report}->check_for_errors }, ); + # FIXME: need to check for required bromley fields here + # if they're got the login details wrong when signing in then # we don't care about the name field even though it's validated # by the user object diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 15444f556..114c86c28 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -153,6 +153,15 @@ want to move adding these elsewhere sub process_update : Private { my ( $self, $c ) = @_; + if ( $c->req->param('first_name' ) && $c->req->param('last_name' ) ) { + my $first_name = $c->req->param('first_name'); + my $last_name = $c->req->param('last_name'); + $c->req->param('name', sprintf( '%s %s', $first_name, $last_name ) ); + + $c->stash->{first_name} = $first_name; + $c->stash->{last_name} = $last_name; + } + my %params = map { $_ => scalar $c->req->param($_) } ( 'update', 'name', 'fixed', 'state', 'reopen' ); @@ -185,6 +194,24 @@ sub process_update : Private { $update->problem_state( $params{state} ); } + if ( $c->req->param('fms_extra_title') ) { + my %extras = (); + $extras{title} = $c->req->param('fms_extra_title'); + $extras{email_alerts_required} = $c->req->param('add_alert'); + $update->extra( \%extras ); + + $c->stash->{fms_extra_title} = $c->req->param('fms_extra_title'); + } + + if ( $c->stash->{ first_name } && $c->stash->{ last_name } ) { + my $extra = $update->extra || {}; + $extra->{first_name} = $c->stash->{ first_name }; + $extra->{last_name} = $c->stash->{ last_name }; + $update->extra( $extra ); + } + + $c->log->debug( 'name is ' . $c->req->param('name') ); + $c->stash->{update} = $update; $c->stash->{add_alert} = $c->req->param('add_alert'); diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 75174b638..90661c0c3 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -31,5 +31,33 @@ sub example_places { return ( 'BR1 3UH', 'Glebe Rd, Bromley' ); } +sub process_extras { + my $self = shift; + my $ctx = shift; + my $contacts = shift; + my $extra = shift; + + for my $field (qw/ fms_extra_title first_name last_name /) { + my $value = $ctx->request->param($field); + + if ( !$value ) { + $ctx->stash->{field_errors}->{$field} = + _('This information is required'); + } + push @$extra, + { + name => $field, + description => uc($field), + value => $value || '', + }; + } + + if ( $ctx->request->param('fms_extra_title') ) { + $ctx->stash->{fms_extra_title} = + $ctx->request->param('fms_extra_title'); + $ctx->stash->{extra_name_info} = 1; + } +} + 1; diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 47038f25c..cee320cca 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -922,5 +922,7 @@ sub example_places { return [ 'B2 4QA', 'Tib St, Manchester' ]; } +sub process_extras {} + 1; diff --git a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm index 80d57400d..6c0089d26 100644 --- a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm +++ b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm @@ -45,5 +45,31 @@ sub generate_problem_banner { return $banner; } +sub process_extras { + my $self = shift; + my $ctx = shift; + my $contacts = shift; + my $extra = shift; + + if ( $contacts->[0]->area_id == 2482 ) { + for my $field ( qw/ fms_extra_title / ) { + my $value = $ctx->request->param( $field ); + + if ( !$value ) { + $ctx->stash->{field_errors}->{ $field } = _('This information is required'); + } + push @$extra, { + name => $field, + description => uc( $field), + value => $value || '', + }; + } + + if ( $ctx->request->param('fms_extra_title') ) { + $ctx->stash->{fms_extra_title} = $ctx->request->param('fms_extra_title'); + $ctx->stash->{extra_name_info} = 1; + } + } +} 1; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 195fe4019..d31bf5510 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -1,3 +1,4 @@ +use utf8; package FixMyStreet::DB::Result::Comment; # Created by DBIx::Class::Schema::Loader @@ -7,7 +8,6 @@ use strict; use warnings; use base 'DBIx::Class::Core'; - __PACKAGE__->load_components("FilterColumn", "InflateColumn::DateTime", "EncodedColumn"); __PACKAGE__->table("comment"); __PACKAGE__->add_columns( @@ -54,29 +54,62 @@ __PACKAGE__->add_columns( { data_type => "boolean", is_nullable => 0 }, "problem_state", { data_type => "text", is_nullable => 1 }, + "external_id", + { data_type => "text", is_nullable => 1 }, + "extra", + { data_type => "text", is_nullable => 1 }, + "send_fail_count", + { data_type => "integer", default_value => 0, is_nullable => 0 }, + "send_fail_reason", + { data_type => "text", is_nullable => 1 }, + "send_fail_timestamp", + { data_type => "timestamp", is_nullable => 1 }, + "whensent", + { data_type => "timestamp", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->belongs_to( - "user", - "FixMyStreet::DB::Result::User", - { id => "user_id" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, -); -__PACKAGE__->belongs_to( "problem", "FixMyStreet::DB::Result::Problem", { id => "problem_id" }, { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, ); +__PACKAGE__->belongs_to( + "user", + "FixMyStreet::DB::Result::User", + { id => "user_id" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-06-27 10:07:32 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ilLn3dlagg5COdpZDmzrVQ +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-26 15:44:18 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:nvkElEgSU6XcLd9znSqhmQ + +__PACKAGE__->filter_column( + extra => { + filter_from_storage => sub { + my $self = shift; + my $ser = shift; + return undef unless defined $ser; + my $h = new IO::String($ser); + return RABX::wire_rd($h); + }, + filter_to_storage => sub { + my $self = shift; + my $data = shift; + my $ser = ''; + my $h = new IO::String($ser); + RABX::wire_wr( $data, $h ); + return $ser; + }, + } +); use DateTime::TimeZone; use Image::Size; use Moose; use namespace::clean -except => [ 'meta' ]; +use RABX; with 'FixMyStreet::Roles::Abuser'; diff --git a/perllib/FixMyStreet/DB/Result/Open311conf.pm b/perllib/FixMyStreet/DB/Result/Open311conf.pm index 0a5784560..b9cd432ee 100644 --- a/perllib/FixMyStreet/DB/Result/Open311conf.pm +++ b/perllib/FixMyStreet/DB/Result/Open311conf.pm @@ -1,3 +1,4 @@ +use utf8; package FixMyStreet::DB::Result::Open311conf; # Created by DBIx::Class::Schema::Loader @@ -7,7 +8,6 @@ use strict; use warnings; use base 'DBIx::Class::Core'; - __PACKAGE__->load_components("FilterColumn", "InflateColumn::DateTime", "EncodedColumn"); __PACKAGE__->table("open311conf"); __PACKAGE__->add_columns( @@ -26,13 +26,30 @@ __PACKAGE__->add_columns( { data_type => "text", is_nullable => 1 }, "api_key", { data_type => "text", is_nullable => 1 }, + "send_method", + { data_type => "text", is_nullable => 1 }, + "send_comments", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "comment_user_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->add_unique_constraint("open311conf_area_id_key", ["area_id"]); +__PACKAGE__->belongs_to( + "comment_user", + "FixMyStreet::DB::Result::User", + { id => "comment_user_id" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, +); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-07-29 18:09:25 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ryqCpvwjNtQrZm4I3s0hxg +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-26 17:03:34 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:fC6Ws8p/pXyjgqfm2LRKsw # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index ce7488703..8c479953b 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -84,6 +84,12 @@ __PACKAGE__->add_columns( { data_type => "boolean", default_value => \"false", is_nullable => 0 }, "geocode", { data_type => "bytea", is_nullable => 1 }, + "send_fail_count", + { data_type => "integer", is_nullable => 1 }, + "send_fail_reason", + { data_type => "text", is_nullable => 1 }, + "send_fail_timestamp", + { data_type => "timestamp", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( @@ -106,8 +112,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-09-19 14:38:43 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:nq8Ufn/SEoDGSrrGlHIxag +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-16 10:08:56 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:VODeZlWk8l/+IzBBlRNV0A # Add fake relationship to stored procedure table __PACKAGE__->has_one( diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index 56d726a8d..7b0ffca73 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -172,4 +172,22 @@ sub belongs_to_council { return 0; } +=head2 split_name + + $name = $user->split_name; + printf( 'Welcome %s %s', $name->{first}, $name->{last} ); + +Returns a hashref with first and last keys with first name(s) and last name. +NB: the spliting algorithm is extremely basic. + +=cut + +sub split_name { + my $self = shift; + + my ($first, $last) = $self->name =~ /^(\S*)(?: (.*))?$/; + + return { first => $first, last => $last }; +} + 1; diff --git a/perllib/Open311.pm b/perllib/Open311.pm index e26e3e4c6..23208df1b 100644 --- a/perllib/Open311.pm +++ b/perllib/Open311.pm @@ -12,10 +12,13 @@ has api_key => ( is => 'ro', isa => 'Str' ); has endpoint => ( is => 'ro', isa => 'Str' ); has test_mode => ( is => 'ro', isa => 'Bool' ); has test_uri_used => ( is => 'rw', 'isa' => 'Str' ); +has test_req_used => ( is => 'rw' ); has test_get_returns => ( is => 'rw' ); -has endpoints => ( is => 'rw', default => sub { { services => 'services.xml', requests => 'requests.xml' } } ); +has endpoints => ( is => 'rw', default => sub { { services => 'services.xml', requests => 'requests.xml', service_request_updates => 'update.xml', update => 'update.xml' } } ); has debug => ( is => 'ro', isa => 'Bool', default => 0 ); has debug_details => ( is => 'rw', 'isa' => 'Str', default => '' ); +has success => ( is => 'rw', 'isa' => 'Bool', default => 0 ); +has error => ( is => 'rw', 'isa' => 'Str', default => '' ); sub get_service_list { my $self = shift; @@ -39,16 +42,40 @@ sub send_service_request { my $extra = shift; my $service_code = shift; - my $description = <<EOT; -title: @{[$problem->title()]} + my $params = $self->_populate_service_request_params( + $problem, $extra, $service_code + ); -detail: @{[$problem->detail()]} + my $response = $self->_post( $self->endpoints->{requests}, $params ); -url: $extra->{url} + if ( $response ) { + my $obj = $self->_get_xml_object( $response ); -Submitted via FixMyStreet -EOT -; + if ( $obj ) { + if ( $obj->{ request }->{ service_request_id } ) { + return $obj->{ request }->{ service_request_id }; + } else { + my $token = $obj->{ request }->{ token }; + if ( $token ) { + return $self->get_service_request_id_from_token( $token ); + } + } + } + + warn sprintf( "Failed to submit problem %s over Open311, response\n: %s\n%s", $problem->id, $response, $self->debug_details ); + return 0; + } +} + +sub _populate_service_request_params { + my $self = shift; + my $problem = shift; + my $extra = shift; + my $service_code = shift; + + my $description = $self->_generate_service_request_description( + $problem, $extra + ); my ( $firstname, $lastname ) = ( $problem->user->name =~ /(\w+)\s+(.+)/ ); @@ -74,30 +101,37 @@ EOT my $extras = $problem->extra; for my $attr ( @$extras ) { - my $name = sprintf( 'attribute[%s]', $attr->{name} ); + my $attr_name = $attr->{name}; + if ( $attr_name eq 'first_name' || $attr_name eq 'last_name' ) { + $params->{$attr_name} = $attr->{value} if $attr->{value}; + next; + } + $attr_name =~ s/fms_extra_//; + my $name = sprintf( 'attribute[%s]', $attr_name ); $params->{ $name } = $attr->{value}; } } - my $response = $self->_post( $self->endpoints->{requests}, $params ); + return $params; +} - if ( $response ) { - my $obj = $self->_get_xml_object( $response ); +sub _generate_service_request_description { + my $self = shift; + my $problem = shift; + my $extra = shift; - if ( $obj ) { - if ( $obj->{ request }->{ service_request_id } ) { - return $obj->{ request }->{ service_request_id }; - } else { - my $token = $obj->{ request }->{ token }; - if ( $token ) { - return $self->get_service_request_id_from_token( $token ); - } - } - } + my $description = <<EOT; +title: @{[$problem->title()]} - warn sprintf( "Failed to submit problem %s over Open311, response\n: %s\n%s", $problem->id, $response, $self->debug_details ); - return 0; - } +detail: @{[$problem->detail()]} + +url: $extra->{url} + +Submitted via FixMyStreet +EOT +; + + return $description; } sub get_service_requests { @@ -129,6 +163,97 @@ sub get_service_request_id_from_token { } } +sub get_service_request_updates { + my $self = shift; + my $start_date = shift; + my $end_date = shift; + + my $params = {}; + + if ( $start_date || $end_date ) { + return 0 unless $start_date && $end_date; + + $params->{start_date} = $start_date; + $params->{end_date} = $end_date; + } + + my $xml = $self->_get( $self->endpoints->{service_request_updates}, $params || undef ); + my $service_requests = $self->_get_xml_object( $xml ); + my $requests; + if ( ref $service_requests->{request_update } eq 'ARRAY' ) { + $requests = $service_requests->{request_update}; + } + else { + $requests = [ $service_requests->{request_update} ]; + } + + return $requests; +} + +sub post_service_request_update { + my $self = shift; + my $comment = shift; + + my $params = $self->_populate_service_request_update_params( $comment ); + + my $response = $self->_post( $self->endpoints->{update}, $params ); + + if ( $response ) { + my $obj = $self->_get_xml_object( $response ); + + if ( $obj ) { + if ( $obj->{ request_update }->{ update_id } ) { + my $update_id = $obj->{request_update}->{update_id}; + + # if there's nothing in the update_id element we get a HASHREF back + unless ( ref $update_id ) { + return $obj->{ request_update }->{ update_id }; + } + } else { + my $token = $obj->{ request_update }->{ token }; + if ( $token ) { + return $self->get_service_request_id_from_token( $token ); + } + } + } + + warn sprintf( "Failed to submit comment %s over Open311, response - %s\n%s", $comment->id, $response, $self->debug_details ); + return 0; + } +} + +sub _populate_service_request_update_params { + my $self = shift; + my $comment = shift; + + my $name = $comment->name || $comment->user->name; + my ( $firstname, $lastname ) = ( $name =~ /(\w+)\s+(.+)/ ); + + my $params = { + update_id_ext => $comment->id, + updated_datetime => $comment->confirmed, + service_request_id => $comment->problem->external_id, + service_request_id_ext => $comment->problem->id, + status => $comment->problem->is_open ? 'OPEN' : 'CLOSED', + email => $comment->user->email, + description => $comment->text, + public_anonymity_required => $comment->anonymous ? 'TRUE' : 'FALSE', + last_name => $lastname, + first_name => $firstname, + }; + + if ( $comment->extra ) { + $params->{'email_alerts_requested'} + = $comment->extra->{email_alerts_requested} ? 'TRUE' : 'FALSE'; + $params->{'title'} = $comment->extra->{title}; + + $params->{first_name} = $comment->extra->{first_name} if $comment->extra->{first_name}; + $params->{last_name} = $comment->extra->{last_name} if $comment->extra->{last_name}; + } + + return $params; +} + sub _get { my $self = shift; my $path = shift; @@ -144,10 +269,25 @@ sub _get { my $content; if ( $self->test_mode ) { + $self->success(1); $content = $self->test_get_returns->{ $path }; $self->test_uri_used( $uri->as_string ); } else { - $content = get( $uri->as_string ); + my $ua = LWP::UserAgent->new; + + my $req = HTTP::Request->new( + GET => $uri->as_string + ); + + my $res = $ua->request( $req ); + + if ( $res->is_success ) { + $content = $res->decoded_content; + $self->success(1); + } else { + $self->success(0); + $self->error( $res->status_line ); + } } return $content; @@ -171,17 +311,27 @@ sub _post { $self->debug_details( $self->debug_details . "\nrequest:" . $req->as_string ); my $ua = LWP::UserAgent->new(); - my $res = $ua->request( $req ); + my $res; + + if ( $self->test_mode ) { + $res = $self->test_get_returns->{ $path }; + $self->test_req_used( $req ); + } else { + $res = $ua->request( $req ); + } if ( $res->is_success ) { + $self->success(1); return $res->decoded_content; } else { - warn sprintf( + $self->success(0); + $self->error( sprintf( "request failed: %s\nerror: %s\n%s", $res->status_line, $self->_process_error( $res->decoded_content ), $self->debug_details - ); + ) ); + warn $self->error; return 0; } } diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm new file mode 100644 index 000000000..ac06dc03e --- /dev/null +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -0,0 +1,110 @@ +package Open311::GetServiceRequestUpdates; + +use Moose; +use Open311; +use FixMyStreet::App; +use DateTime::Format::W3CDTF; + +has council_list => ( is => 'ro' ); +has system_user => ( is => 'rw' ); +has start_date => ( is => 'ro', default => undef ); +has end_date => ( is => 'ro', default => undef ); + +sub fetch { + my $self = shift; + + my $councils = FixMyStreet::App->model('DB::Open311Conf')->search( + { + send_method => 'open311', + send_comments => 1, + comment_user_id => { '!=', undef }, + endpoint => { '!=', '' }, + } + ); + + while ( my $council = $councils->next ) { + + my $o = Open311->new( + endpoint => $council->endpoint, + api_key => $council->api_key, + jurisdiction => $council->jurisdiction, + ); + + $self->system_user( $council->comment_user ); + $self->update_comments( $o, { areaid => $council->area_id }, ); + } +} + +sub update_comments { + my ( $self, $open311, $council_details ) = @_; + + my @args = (); + + if ( $self->start_date || $self->end_date ) { + return 0 unless $self->start_date && $self->end_date; + + push @args, $self->start_date; + push @args, $self->end_date; + } + + my $requests = $open311->get_service_request_updates( @args ); + + return 0 unless $open311->success; + + for my $request (@$requests) { + my $request_id = $request->{service_request_id}; + + # If there's no request id then we can't work out + # what problem it belongs to so just skip + next unless $request_id; + + my $problem = + FixMyStreet::App->model('DB::Problem') + ->search( { + external_id => $request_id, + council => { like => '%' . $council_details->{areaid} . '%' }, + } ); + + if (my $p = $problem->first) { + my $c = $p->comments->search( { external_id => $request->{update_id} } ); + + if ( !$c->first ) { + my $comment_time = DateTime::Format::W3CDTF->parse_datetime( $request->{updated_datetime} ); + + my $comment = FixMyStreet::App->model('DB::Comment')->new( + { + problem => $p, + user => $self->system_user, + external_id => $request->{update_id}, + text => $request->{description}, + mark_fixed => 0, + mark_open => 0, + anonymous => 0, + name => $self->system_user->name, + confirmed => $comment_time, + created => $comment_time, + state => 'confirmed', + } + ); + + if ( $p->is_open and $request->{status} eq 'closed' ) { + $p->state( 'fixed - council' ); + $p->update; + + $comment->mark_fixed( 1 ); + } elsif ( ( $p->is_closed || $p->is_fixed ) and $request->{status} eq 'open' ) { + $p->state( 'confirmed' ); + $p->update; + + $comment->mark_open( 1 ); + } + + $comment->insert(); + } + } + } + + return 1; +} + +1; diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm index cfec9005d..ac355a33c 100644 --- a/perllib/Open311/PopulateServiceList.pm +++ b/perllib/Open311/PopulateServiceList.pm @@ -189,6 +189,12 @@ sub _add_contact_to_meta { print "Fetching meta data for $self->_current_service->{service_code}\n"; my $meta_data = $self->_current_open311->get_service_meta_info( $self->_current_service->{service_code} ); + if ( ref $meta_data->{ attributes }->{ attribute } eq 'HASH' ) { + $meta_data->{ attributes }->{ attribute } = [ + $meta_data->{ attributes }->{ attribute } + ]; + } + # turn the data into something a bit more friendly to use my @meta = # remove trailing colon as we add this when we display so we don't want 2 @@ -197,6 +203,26 @@ sub _add_contact_to_meta { sort { $a->{order} <=> $b->{order} } @{ $meta_data->{attributes}->{attribute} }; + # we add these later on from bromley so don't list them here + # as we don't want to display them + if ( $self->_current_council->area_id == 2482 ) { + my %ignore = map { $_ => 1 } qw/ + service_request_id_ext + requested_datetime + report_url + title + last_name + email + easting + northing + report_title + public_anonymity_required + email_alerts_requested + /; + + @meta = grep { ! $ignore{ $_->{ code } } } @meta; + } + $contact->extra( \@meta ); $contact->update; } |