From 99e3a9738f5b0e418fac32f19ba5133ba14f7762 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Tue, 26 Nov 2024 14:57:50 +0000 Subject: [PATCH 1/2] [Bromley] Cope with second Echo referral on same report. If a report was originally sent to Echo, then referred to Bromley more than once, we do not want to send the report to Bromley more than once. --- perllib/FixMyStreet/Cobrand/Bromley.pm | 10 ++++++++-- t/cobrand/bromley.t | 26 ++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 9b8a26e06a..539add1022 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -446,14 +446,20 @@ sub open311_get_update_munging { $notes = $_->{Value} if $_->{DatatypeName} eq 'Veolia Notes'; } $problem->set_extra_metadata(handover_notes => $notes); + if (my $original_external_id = $problem->get_extra_metadata('original_bromley_external_id')) { + # Originally sent to Bromley, don't need to resend report $problem->external_id($original_external_id); $comment->problem_state(REFERRED_TO_BROMLEY); $comment->send_state('unprocessed'); - } else { - # Resending report, don't need comment to be public + } elsif ($self->_has_report_been_sent_to_echo($problem)) { + # Resending report from Echo to Bromley, don't need comment to be public $comment->state('hidden'); $problem->resend; + } else { + # Assume has already been sent to Bromley, no need to resend report + $comment->problem_state(REFERRED_TO_BROMLEY); + $comment->send_state('unprocessed'); } } } diff --git a/t/cobrand/bromley.t b/t/cobrand/bromley.t index 7db428e58c..7a0f2d3313 100644 --- a/t/cobrand/bromley.t +++ b/t/cobrand/bromley.t @@ -836,16 +836,19 @@ subtest 'redirecting of reports between backends' => sub { is_deeply [ map { $_->state } $report->comments->order_by('id')->all ], ['hidden', 'hidden']; }; + + my $event_guid = '05a10cb2-44c9-48d9-92a2-cc6788994bae'; + subtest 'A report sent to Echo, redirected to Confirm' => sub { $report->comments->delete; $report->unset_extra_metadata('original_bromley_external_id'); - $report->update({ external_id => 'original-guid' }); + $report->update({ external_id => $event_guid }); $mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in); is $report->comments->count, 1, 'A new update'; is_deeply [ map { $_->state } $report->comments->all ], ['hidden']; $report->discard_changes; is $report->whensent, undef; - is $report->external_id, 'original-guid', 'ID not changed'; + is $report->external_id, $event_guid, 'ID not changed'; is $report->state, 'in progress', 'A state change'; is $report->category, 'Environmental Services'; FixMyStreet::Script::Reports::send(); @@ -856,8 +859,19 @@ subtest 'redirecting of reports between backends' => sub { is $c->param('description'), "$detail | Handover notes - Outgoing notes from Echo"; }; + subtest 'A second referral update comes in, should be a normal update' => sub { + $report->update({ external_id => 'bromley-id' }); + $mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in); + is $report->comments->count, 2, 'A new update'; + my @updates = $report->comments->order_by('id')->all; + is_deeply [ map { $_->state } @updates ], ['hidden', 'confirmed']; + is_deeply [ map { $_->problem_state } @updates ], ['in progress', 'Environmental Services']; + $report->discard_changes; + isnt $report->whensent, undef; # Got sent in last subtest + is $report->external_id, 'bromley-id', 'ID not changed'; + }; + subtest "comment on a closed echo report result in a resend under 'Street Services'" => sub { - my $event_guid = '05a10cb2-44c9-48d9-92a2-cc6788994bae'; my $event_id = 123; my $echo = Test::MockModule->new('Integrations::Echo'); @@ -924,8 +938,8 @@ subtest 'redirecting of reports between backends' => sub { }; subtest "Another update from Echo on this new sent report closes it again" => sub { - $report->update({ external_id => 'guid' }); - my $in = $mech->echo_notify_xml('guid', 2104, 15004, 67); + $report->update({ external_id => $event_guid }); + my $in = $mech->echo_notify_xml('guid', 2104, 15004, 67, 'FMS-' . $report->id); $mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in); is $report->comments->count, 3, 'A new update'; $report->discard_changes; @@ -936,7 +950,7 @@ subtest 'redirecting of reports between backends' => sub { }; subtest "Echo then redirect it back to Confirm" => sub { - my $in = $mech->echo_notify_xml('guid', 2104, 15004, 1252); + my $in = $mech->echo_notify_xml('guid', 2104, 15004, 1252, 'FMS-' . $report->id); $mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in); is $report->comments->count, 4, 'A new update'; $report->discard_changes; From 65a3a6f919f92d4900f72056b450f348be7ad0ce Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Wed, 27 Nov 2024 11:26:21 +0000 Subject: [PATCH 2/2] [Bromley] Include Veolia Notes in update if provided. --- perllib/FixMyStreet/Cobrand/Bromley.pm | 36 +++++++++++++---------- perllib/FixMyStreet/Roles/Cobrand/Echo.pm | 1 + t/cobrand/bromley.t | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 539add1022..c4afe74709 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -412,7 +412,7 @@ appropriately. sub open311_get_update_munging { my ($self, $comment, $state, $request) = @_; - # An update from Bromley with a special referral state + # An update from Bromley with a special referral state, means "resend to Echo" if ($state eq 'referred to veolia streets') { my $problem = $comment->problem; # Do we want to store the old category somewhere for display? @@ -427,24 +427,28 @@ sub open311_get_update_munging { return; } - # An update from Echo with resolution code 1252 + # Fetch any outgoing notes on the Echo event + # If we pulled the update, we already have it, otherwise look it up + my $event = $request->{echo_event} || do { + my $echo = $self->feature('echo'); + $echo = Integrations::Echo->new(%$echo); + my $event = $echo->GetEvent($request->{service_request_id}); # From the event, not the report + $echo->log($event->{Data}); + $event; + }; + my $data = Integrations::Echo::force_arrayref($event->{Data}, 'ExtensibleDatum'); + my $notes = ""; + foreach (@$data) { + $notes = $_->{Value} if $_->{DatatypeName} eq 'Veolia Notes'; + } + + # An update from Echo with resolution code 1252 means "refer to Bromley" my $code = $comment->get_extra_metadata('external_status_code') || ''; if ($code eq '1252') { my $problem = $comment->problem; $problem->category(REFERRED_TO_BROMLEY); # Will be LBB_RRE_FROM_VEOLIA_STREETS $problem->state('in progress'); $comment->problem_state('in progress'); - - # Fetch outgoing notes - my $echo = $self->feature('echo'); - $echo = Integrations::Echo->new(%$echo); - my $event = $echo->GetEvent($request->{service_request_id}); # From the event, not the report - $echo->log($event->{Data}); - my $data = Integrations::Echo::force_arrayref($event->{Data}, 'ExtensibleDatum'); - my $notes = ""; - foreach (@$data) { - $notes = $_->{Value} if $_->{DatatypeName} eq 'Veolia Notes'; - } $problem->set_extra_metadata(handover_notes => $notes); if (my $original_external_id = $problem->get_extra_metadata('original_bromley_external_id')) { @@ -453,14 +457,16 @@ sub open311_get_update_munging { $comment->problem_state(REFERRED_TO_BROMLEY); $comment->send_state('unprocessed'); } elsif ($self->_has_report_been_sent_to_echo($problem)) { - # Resending report from Echo to Bromley, don't need comment to be public + # Resending report from Echo to Bromley for first time, don't need comment to be public $comment->state('hidden'); $problem->resend; } else { - # Assume has already been sent to Bromley, no need to resend report + # Assume it has already been sent to Bromley, no need to resend report $comment->problem_state(REFERRED_TO_BROMLEY); $comment->send_state('unprocessed'); } + } elsif ($notes) { + $comment->text($comment->text . "\n\n" .$notes); } } diff --git a/perllib/FixMyStreet/Roles/Cobrand/Echo.pm b/perllib/FixMyStreet/Roles/Cobrand/Echo.pm index 3fccd7151c..4190aba106 100644 --- a/perllib/FixMyStreet/Roles/Cobrand/Echo.pm +++ b/perllib/FixMyStreet/Roles/Cobrand/Echo.pm @@ -747,6 +747,7 @@ sub waste_fetch_events { (my $external_id = $report->external_id) =~ s/^Echo-//; my $event = $cfg->{echo}->GetEvent($external_id); my $request = $self->construct_waste_open311_update($cfg, $event) or next; + $request->{echo_event} = $event; next if !$request->{status} || $request->{status} eq 'confirmed'; # Still in initial state next unless $self->waste_check_last_update( diff --git a/t/cobrand/bromley.t b/t/cobrand/bromley.t index 7a0f2d3313..f72fe34b09 100644 --- a/t/cobrand/bromley.t +++ b/t/cobrand/bromley.t @@ -946,7 +946,7 @@ subtest 'redirecting of reports between backends' => sub { is $report->state, 'fixed - council', 'A state change'; is $report->get_extra_metadata('external_status_code'), 67; my $comment = FixMyStreet::DB->resultset("Comment")->search(undef, { order_by => { -desc => 'id' } })->first; - is $comment->text, 'Template text'; + is $comment->text, "Template text\n\nOutgoing notes from Echo"; }; subtest "Echo then redirect it back to Confirm" => sub {