From 1d18f778d93eca841c79cc0b77aa800b15955ad6 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Fri, 9 Feb 2024 12:58:31 +0000 Subject: [PATCH] [Echo] Look up nearest segment when posting event. --- perllib/Integrations/Echo.pm | 59 +++++++++++++------ perllib/Open311/Endpoint/Integration/Echo.pm | 13 +--- .../Endpoint/Integration/UK/Brent/Echo.pm | 2 +- t/open311/endpoint/brent.t | 16 +++-- 4 files changed, 56 insertions(+), 34 deletions(-) diff --git a/perllib/Integrations/Echo.pm b/perllib/Integrations/Echo.pm index 629a3c4ed..8e02b8cee 100644 --- a/perllib/Integrations/Echo.pm +++ b/perllib/Integrations/Echo.pm @@ -142,26 +142,28 @@ sub PostEvent { EventObjectType => 'Source', ObjectRef => $uprn, ); - } elsif ($args->{usrn}) { - my $usrn = ixhash( - Key => 'Usrn', - Type => 'Street', - Value => [ - # Must be a string, not a long - { 'msArray:anyType' => SOAP::Data->value($args->{usrn})->type('string') }, - ], - ); - $source = ixhash( - EventObjectType => 'Source', - ObjectRef => $usrn, - Location => ixhash( - Latitude => $args->{lat}, - Longitude => $args->{long}, - ), - ); } else { + my $points = $self->FindPoints($args->{lat}, $args->{long}); + my $object; + if ($points->[0]) { + $object = ixhash( + Key => 'Id', + Type => 'PointSegment', + Value => [ { 'msArray:anyType' => $points->[0]->{Id} } ], + ); + } elsif ($args->{usrn}) { + $object = ixhash( + Key => 'Usrn', + Type => 'Street', + Value => [ + # Must be a string, not a long + { 'msArray:anyType' => SOAP::Data->value($args->{usrn})->type('string') }, + ], + ); + } $source = ixhash( EventObjectType => 'Source', + $object ? (ObjectRef => $object) : (), Location => ixhash( Latitude => $args->{lat}, Longitude => $args->{long}, @@ -210,6 +212,20 @@ sub PerformEventAction { $self->call('PerformEventAction', action => $action); } +sub FindPoints { + my ($self, $lat, $lon) = @_; + + my $obj = ixhash( + PointType => 'PointSegment', + Near => ixhash( + Latitude => $lat, + Longitude => $lon, + ), + ); + my $res = $self->call('FindPoints', query => $obj); + return force_arrayref($res, 'PointInfo'); +} + sub ixhash { tie (my %data, 'Tie::IxHash', @_); return \%data; @@ -233,4 +249,13 @@ sub make_soap_structure { return @out; } +sub force_arrayref { + my ($res, $key) = @_; + return [] unless $res; + my $data = $res->{$key}; + return [] unless $data; + $data = [ $data ] unless ref $data eq 'ARRAY'; + return $data; +} + 1; diff --git a/perllib/Open311/Endpoint/Integration/Echo.pm b/perllib/Open311/Endpoint/Integration/Echo.pm index accc57b96..5bc32147f 100644 --- a/perllib/Open311/Endpoint/Integration/Echo.pm +++ b/perllib/Open311/Endpoint/Integration/Echo.pm @@ -332,7 +332,7 @@ sub post_service_request { # Look up extra data fields my $event_type = $integ->GetEventType($request->{event_type}); - my $data = force_arrayref($event_type->{Datatypes}, 'ExtensibleDatatype'); + my $data = Integrations::Echo::force_arrayref($event_type->{Datatypes}, 'ExtensibleDatatype'); foreach my $type (@$data) { my $row = { id => $type->{Id} }; my $value = $self->check_for_data_value($type->{Name}, $args, $request); @@ -340,7 +340,7 @@ sub post_service_request { my %extra; my $extra_count = 0; if ($type->{ChildDatatypes}) { - my $moredata = force_arrayref($type->{ChildDatatypes}, 'ExtensibleDatatype'); + my $moredata = Integrations::Echo::force_arrayref($type->{ChildDatatypes}, 'ExtensibleDatatype'); foreach (@$moredata) { my $subrow = { id => $_->{Id} }; my $value = $self->check_for_data_value($_->{Name}, $args, $request, $type->{Name}); @@ -497,13 +497,4 @@ used as part of a Multi integration. sub get_service_request_updates { } -sub force_arrayref { - my ($res, $key) = @_; - return [] unless $res; - my $data = $res->{$key}; - return [] unless $data; - $data = [ $data ] unless ref $data eq 'ARRAY'; - return $data; -} - 1; diff --git a/perllib/Open311/Endpoint/Integration/UK/Brent/Echo.pm b/perllib/Open311/Endpoint/Integration/UK/Brent/Echo.pm index cb4d48150..f91390461 100644 --- a/perllib/Open311/Endpoint/Integration/UK/Brent/Echo.pm +++ b/perllib/Open311/Endpoint/Integration/UK/Brent/Echo.pm @@ -60,7 +60,7 @@ around post_service_request_update => sub { my $event_type = $integ->GetEventType($event->{EventTypeId}); my $state_id = $event->{EventStateId}; - my $states = Open311::Endpoint::Integration::Echo::force_arrayref($event_type->{Workflow}->{States}, 'State'); + my $states = Integrations::Echo::force_arrayref($event_type->{Workflow}->{States}, 'State'); my $data; foreach (@$states) { my $core = $_->{CoreState}; diff --git a/t/open311/endpoint/brent.t b/t/open311/endpoint/brent.t index 999545c38..beda41e05 100644 --- a/t/open311/endpoint/brent.t +++ b/t/open311/endpoint/brent.t @@ -275,17 +275,16 @@ $soap_lite->mock(call => sub { my @event_object = ${${$params[2]->value}->value->value}->value; is $event_object[0]->value, 'Source'; my @object_ref = ${$event_object[1]->value}->value; - is $object_ref[0]->value, 'Usrn'; - is $object_ref[1]->value, 'Street'; - my $usrn = ${$object_ref[2]->value}->value->value->value->value; + is $object_ref[0]->value, 'Id'; + is $object_ref[1]->value, 'PointSegment'; + my $segment = ${$object_ref[2]->value}->value->value->value; my @data = ${$params[0]->value}->value->value; + is $segment, '11345'; if ($event_type == 935) { - is $usrn, '123/4567'; is @data, 5, 'Name and source is only extra data'; } elsif ($event_type == 943) { - is $usrn, '123/4567'; is @data, 4, 'Name (no surname) and source is only extra data'; } my $c = 0; @@ -378,6 +377,13 @@ $soap_lite->mock(call => sub { is $datatype_id, 112; } return SOAP::Result->new(result => { EventActionGuid => 'ABC' }); + } elsif ($args[0]->name eq 'FindPoints') { + return SOAP::Result->new(result => { PointInfo => [ + { Description => 'Example Street 1-10 Near Example Avenue', Id => '11345' }, + { Description => 'Example Street, 11-40 Between Example Avenue and Example Road', Id => '12345' }, + { Description => '', Id => '13345' }, + { Description => 'Example Street, 41-60 Near Example Close', Id => '15345' }, + ] }); } else { is $args[0], ''; }