diff options
Diffstat (limited to 'perllib/FixMyStreet/App')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Open311.pm | 26 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Open311/Updates.pm | 88 |
2 files changed, 105 insertions, 9 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Open311.pm b/perllib/FixMyStreet/App/Controller/Open311.pm index 841330e92..b4b5d5e3a 100644 --- a/perllib/FixMyStreet/App/Controller/Open311.pm +++ b/perllib/FixMyStreet/App/Controller/Open311.pm @@ -111,8 +111,6 @@ sub get_discovery : Private { { 'contact' => ["Send email to $contact_email."], 'changeset' => [$prod_changeset], - # XXX rewrite to match - 'key_service' => ["Read access is open to all according to our \u003Ca href='/open_data' target='_blank'\u003Eopen data license\u003C/a\u003E. For write access either: 1. return the 'guid' cookie on each call (unique to each client) or 2. use an api key from a user account which can be generated here: http://seeclickfix.com/register The unversioned url will always point to the latest supported version."], 'max_requests' => [ $c->config->{OPEN311_LIMIT} || 1000 ], 'endpoints' => [ { @@ -195,9 +193,7 @@ sub get_services : Private { ); } $c->forward( 'format_output', [ { - 'services' => [ { - 'service' => \@services - } ] + 'services' => \@services } ] ); } @@ -291,9 +287,7 @@ sub output_requests : Private { } $c->forward( 'format_output', [ { - 'requests' => [ { - 'request' => \@problemlist - } ] + service_requests => \@problemlist } ] ); } @@ -429,7 +423,21 @@ sub format_output : Private { $c->res->body( encode_json($hashref) ); } elsif ('xml' eq $format) { $c->res->content_type('application/xml; charset=utf-8'); - $c->res->body( XMLout($hashref, RootName => undef, NoAttr => 1 ) ); + my $group_tags = { + services => 'service', + attributes => 'attribute', + values => 'value', + service_requests => 'request', + errors => 'error', + service_request_updates => 'request_update', + }; + $c->res->body( XMLout($hashref, + KeyAttr => {}, + GroupTags => $group_tags, + SuppressEmpty => undef, + RootName => undef, + NoAttr => 1, + ) ); } else { $c->detach( 'error', [ sprintf(_('Invalid format %s specified.'), $format) diff --git a/perllib/FixMyStreet/App/Controller/Open311/Updates.pm b/perllib/FixMyStreet/App/Controller/Open311/Updates.pm new file mode 100644 index 000000000..105400a8a --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Open311/Updates.pm @@ -0,0 +1,88 @@ +package FixMyStreet::App::Controller::Open311::Updates; + +use utf8; +use Moose; +use namespace::autoclean; +use Open311; +use Open311::GetServiceRequestUpdates; + +BEGIN { extends 'Catalyst::Controller'; } + +=head1 NAME + +FixMyStreet::App::Controller::Open311::Updates - Catalyst Controller + +=head1 DESCRIPTION + +=head1 METHODS + +=cut + +sub receive : Regex('^open311/v2/servicerequestupdates.(xml|json)$') : Args(0) { + my ( $self, $c ) = @_; + $c->stash->{format} = $c->req->captures->[0]; + + $c->detach('bad_request', [ 'POST' ]) unless $c->req->method eq 'POST'; + + my $body; + if ($c->cobrand->can('council_area_id')) { + $body = $c->cobrand->body; + } else { + $body = $c->model('DB::Body')->find({ id => $c->get_param('jurisdiction_id') }); + } + $c->detach('bad_request', ['jurisdiction_id']) unless $body; + my $user = $body->comment_user; + + my $key = $c->get_param('api_key') || ''; + my $token = $c->cobrand->feature('open311_token') || ''; + $c->detach('bad_request', [ 'api_key' ]) unless $key && $key eq $token; + + my $request = { + media_url => $c->get_param('media_url'), + external_status_code => $c->get_param('external_status_code'), + }; + foreach (qw(service_request_id update_id updated_datetime status description)) { + $request->{$_} = $c->get_param($_) || $c->detach('bad_request', [ $_ ]); + } + + my %open311_conf = ( + endpoint => $body->endpoint, + api_key => $body->api_key, + jurisdiction => $body->jurisdiction, + extended_statuses => $body->send_extended_statuses, + ); + + my $cobrand = $body->get_cobrand_handler; + $cobrand->call_hook(open311_config_updates => \%open311_conf) + if $cobrand; + + my $open311 = Open311->new(%open311_conf); + my $updates = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $open311, + current_body => $body, + ); + + my $p = $updates->find_problem($request); + $c->detach('bad_request', [ 'not found' ]) unless $p; + + my $comment = $p->comments->search( { external_id => $request->{update_id} } )->first; + $c->detach('bad_request', [ 'already exists' ]) if $comment; + + $comment = $updates->process_update($request, $p); + + my $data = { service_request_updates => { update_id => $comment->id } }; + + $c->forward('/open311/format_output', [ $data ]); +} + +sub bad_request : Private { + my ($self, $c, $comment) = @_; + $c->response->status(400); + $c->forward('/open311/format_output', [ { errors => { code => 400, description => "Bad request: $comment" } } ]); +} + +__PACKAGE__->meta->make_immutable; + +1; + |