From d0006e81a02af0eeffe922cf968bdd9c2c0523f7 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Fri, 19 Jun 2020 14:56:07 +0100 Subject: [PATCH] [Bromley] Look for open events. --- perllib/FixMyStreet/App/Controller/Waste.pm | 4 +-- perllib/FixMyStreet/Cobrand/Bromley.pm | 37 +++++++++++++++++++ perllib/Integrations/Echo.pm | 40 +++++++++++++++++++++ t/app/controller/waste.t | 7 ++++ templates/web/bromley/waste/services.html | 12 +++++-- 5 files changed, 96 insertions(+), 4 deletions(-) diff --git a/perllib/FixMyStreet/App/Controller/Waste.pm b/perllib/FixMyStreet/App/Controller/Waste.pm index e9a1d462ee8..5d02c158d31 100644 --- a/perllib/FixMyStreet/App/Controller/Waste.pm +++ b/perllib/FixMyStreet/App/Controller/Waste.pm @@ -136,7 +136,7 @@ sub construct_bin_request_form { my $field_list = []; foreach (@{$c->stash->{service_data}}) { - next unless $_->{next}; + next unless $_->{next} && !$_->{request_open}; my $name = $_->{service_name}; my $containers = $_->{request_containers}; my $max = $_->{request_max}; @@ -237,7 +237,7 @@ sub construct_bin_report_form { my $field_list = []; foreach (@{$c->stash->{service_data}}) { - next unless $_->{last} && $_->{report_allowed}; + next unless $_->{last} && $_->{report_allowed} && !$_->{report_open}; my $id = $_->{service_id}; my $name = $_->{service_name}; push @$field_list, "service-$id" => { diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 9adfe05db80..bda2ef32296 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -513,6 +513,9 @@ sub bin_services_for_address { my $result = $echo->GetServiceUnitsForObject($property->{uprn}); return [] unless @$result; + my $events = $echo->GetEventsForObject($property->{id}); + my $open = $self->_parse_open_events($events); + my @out; foreach (@$result) { next unless $_->{ServiceTasks}; @@ -523,12 +526,15 @@ sub bin_services_for_address { next unless $schedules->{next} or $schedules->{last}; my $containers = $service_to_containers{$_->{ServiceId}}; + my ($open_request) = grep { $_ } map { $open->{request}->{$_} } @$containers; my $row = { id => $_->{Id}, service_id => $_->{ServiceId}, service_name => $service_name_override{$_->{ServiceId}} || $_->{ServiceName}, report_allowed => within_working_days($schedules->{last}, 2), + report_open => $open->{missed}->{$_->{ServiceId}}, request_allowed => $request_allowed{$_->{ServiceId}}, + request_open => $open_request, request_containers => $containers, request_max => $quantity_max{$_->{ServiceId}}, service_task_id => $servicetask->{Id}, @@ -547,6 +553,37 @@ sub bin_services_for_address { return \@out; } +sub _parse_open_events { + my $self = shift; + my $events = shift; + my $open; + foreach (@$events) { + next if $_->{ResolvedDate} || $_->{ResolutionCodeId}; # Is this the right field? + my $event_type = $_->{EventTypeId}; + my $service_id = $_->{ServiceId}; + if ($event_type == 2104) { # Request + my $data = $_->{Data}{ExtensibleDatum}; + my $container; + DATA: foreach (@$data) { + if ($_->{ChildData}) { + foreach (@{$_->{ChildData}{ExtensibleDatum}}) { + if ($_->{DatatypeName} eq 'Container Type') { + $container = $_->{Value}; + last DATA; + } + } + } + } + my $report = $self->problems->search({ external_id => $_->{Guid} })->first; + $open->{request}->{$container} = $report ? { report => $report } : 1; + } elsif (2095 <= $event_type && $event_type <= 2103) { # Missed collection + my $report = $self->problems->search({ external_id => $_->{Guid} })->first; + $open->{missed}->{$service_id} = $report ? { report => $report } : 1; + } + } + return $open; +} + sub _parse_schedules { my $servicetask = shift; my $schedules = $servicetask->{ServiceTaskSchedules}{ServiceTaskSchedule}; diff --git a/perllib/Integrations/Echo.pm b/perllib/Integrations/Echo.pm index be6e5ff4a91..4a92e7e81ad 100644 --- a/perllib/Integrations/Echo.pm +++ b/perllib/Integrations/Echo.pm @@ -271,6 +271,46 @@ sub GetServiceTaskInstances { return force_arrayref($res, 'ServiceTaskInstances'); } +sub GetEventsForObject { + my ($self, $id, $type) = @_; + my $from = DateTime->now->set_time_zone(FixMyStreet->local_time_zone)->subtract(months => 3); + return [ { + # Missed collection for service 542 + EventTypeId => 2100, + ServiceId => 542, + }, { + # Request for a new paper container + EventTypeId => 2104, + Data => { ExtensibleDatum => [ + { Value => 2, DatatypeName => 'Source' }, + { + ChildData => { ExtensibleDatum => [ + { Value => 1, DatatypeName => 'Action' }, + { Value => 12, DatatypeName => 'Container Type' }, + ] }, + }, + ] }, + ServiceId => 535, + } ] if $self->sample_data; + # uncoverable statement + my $res = $self->call('GetEventsForObject', + objectRef => ixhash( + Key => 'Id', + Type => 'PointAddress', + Value => { 'msArray:anyType' => $id }, + ), + query => ixhash( + $type ? (EventTypeRef => ixhash( + Key => 'Id', + Type => 'EventType', + Value => { 'msArray:anyType' => $type }, + )) : (), + From => dt_to_hash($from), + ), + ); + return force_arrayref($res, 'Event'); +} + sub ixhash { tie (my %data, 'Tie::IxHash', @_); return \%data; diff --git a/t/app/controller/waste.t b/t/app/controller/waste.t index 77f825f3b54..ab0e21a2c0c 100644 --- a/t/app/controller/waste.t +++ b/t/app/controller/waste.t @@ -59,6 +59,9 @@ FixMyStreet::override_config { $mech->content_contains('2 Example Street'); $mech->content_contains('Food Waste'); }; + subtest 'Thing already requested' => sub { + $mech->content_contains('A food waste collection has been reported as missed'); + }; subtest 'Report a missed bin' => sub { $mech->content_contains('service-101', 'Can report, last collection was 27th'); $mech->content_contains('service-537', 'Can report, last collection was 27th'); @@ -132,6 +135,10 @@ FixMyStreet::override_config { is $report->get_extra_field_value('Quantity'), 2; is $report->get_extra_field_value('Container_Type'), 1; }; + subtest 'Thing already requested' => sub { + $mech->get_ok('/waste/uprn/1000000002'); + $mech->content_contains('A new paper & cardboard container request has been made'); + }; subtest 'General enquiry, bad data' => sub { $mech->get_ok('/waste/uprn/1000000002/enquiry'); is $mech->uri->path, '/waste/uprn/1000000002'; diff --git a/templates/web/bromley/waste/services.html b/templates/web/bromley/waste/services.html index bd82635a717..5bd0dd0350c 100644 --- a/templates/web/bromley/waste/services.html +++ b/templates/web/bromley/waste/services.html @@ -1,4 +1,8 @@ -[% IF unit.report_allowed %] +[% IF unit.report_open %] + [% IF unit.report_open.report %][% ELSE %][% END %] + A [% unit.service_name FILTER lower %] collection has been reported as missed + [% IF unit.report_open.report %][% ELSE %][% END %] +[% ELSIF unit.report_allowed %] [% any_report_allowed = 1 %]
@@ -10,7 +14,11 @@ [% END %] Report a problem with a [% unit.service_name FILTER lower %] collection Report damage during a [% unit.service_name FILTER lower %] collection -[% IF unit.request_allowed %] +[% IF unit.request_open %] + [% IF unit.request_open.report %][% ELSE %][% END %] + A new [% unit.service_name FILTER lower %] container request has been made + [% IF unit.request_open.report %][% ELSE %][% END %] +[% ELSIF unit.request_allowed %] [% any_request_allowed = 1 %]