Skip to content

Commit

Permalink
[Echo] Look up nearest segment when posting event.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Feb 23, 2024
1 parent 64650ea commit 1d18f77
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
59 changes: 42 additions & 17 deletions perllib/Integrations/Echo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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;
Expand All @@ -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;
13 changes: 2 additions & 11 deletions perllib/Open311/Endpoint/Integration/Echo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ 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);

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});
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion perllib/Open311/Endpoint/Integration/UK/Brent/Echo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
16 changes: 11 additions & 5 deletions t/open311/endpoint/brent.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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], '';
}
Expand Down

0 comments on commit 1d18f77

Please sign in to comment.