From 63ed4e0e3ed666270b151e3f9a91b34eb62100e1 Mon Sep 17 00:00:00 2001 From: Nik Gupta Date: Thu, 7 Mar 2024 14:17:38 +0000 Subject: [PATCH] [Bromley] Create 'Investigation Required' events for comments on closed Echo-backed reports. See https://github.com/mysociety/societyworks/issues/3267. --- perllib/FixMyStreet/Cobrand/Bromley.pm | 47 ++++++++++++++++- perllib/FixMyStreet/SendReport/Open311.pm | 2 +- t/cobrand/bromley.t | 63 +++++++++++++++++++++++ 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 3fc937b41c0..e825bec7b0e 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -183,7 +183,7 @@ sub open311_config { } sub open311_extra_data_include { - my ($self, $row, $h) = @_; + my ($self, $row, $h, $contact) = @_; my $title = $row->title; @@ -220,7 +220,7 @@ sub open311_extra_data_include { value => $row->user->email } ]; - if ( $row->category eq 'Garden Subscription' ) { + if ( $contact->category eq 'Garden Subscription' ) { if ( $row->get_extra_metadata('contributed_as') && $row->get_extra_metadata('contributed_as') eq 'anonymous_user' ) { push @$open311_only, { name => 'contributed_as', value => 'anonymous_user' }; } @@ -236,6 +236,15 @@ sub open311_extra_data_include { push @$open311_only, { name => 'fms_extra_title', value => $row->user->title }; } + # Investigation required needs a reference to the relevant event ID. + if ($contact->category eq 'Investigation Required' && $self->_has_report_been_sent_to_echo($row)) { + # We need to look up the ID of the Echo event to send (we currently have the GUID). + my $cfg = $self->feature('echo'); + my $echo = Integrations::Echo->new(%$cfg); + my $event = $echo->GetEvent($row->external_id); + push @$open311_only, { name => 'Event_ID', value => $event->{Id} }; + } + return $open311_only; } @@ -261,6 +270,14 @@ sub open311_pre_send { my $text = $row->detail . "\n\nPrivate comments: $private_comments"; $row->detail($text); } + + if (my $comment_id = $row->get_extra_metadata('echo_report_reopened_with_comment')) { + my $comment = FixMyStreet::DB->resultset('Comment')->find($comment_id); + if ($comment && $comment->text) { + my $text = "Closed report has a new comment: " . $comment->text . "\n\n" . $row->detail; + $row->detail($text); + } + } } sub _include_user_title_in_extra { @@ -374,16 +391,42 @@ sub open311_contact_meta_override { @$meta = grep { !$ignore{$_->{code}} } @$meta; } +=head2 _has_report_been_sent_to_echo + +Assumes a report has been sent to Echo if the external ID is a GUID. + +=cut + +sub _has_report_been_sent_to_echo { + my ($self, $report) = @_; + my $guid_regex = qr/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; + return $report->external_id && $report->external_id =~ /$guid_regex/; +} + =head2 should_skip_sending_update Do not send updates to the backend if they were made by a staff user and don't have any text (public or private). +Also, if an update is on a closed echo-backed report, skip it and instead +set the report to be resent under the 'Investigation Required' category, since we +can't update closed echo events. + =cut sub should_skip_sending_update { my ($self, $update) = @_; + my $report = $update->problem; + if ($report->is_closed && $self->_has_report_been_sent_to_echo($report)) { + $report->set_extra_metadata('open311_category_override' => 'Investigation Required'); + $report->set_extra_metadata('echo_report_reopened_with_comment' => $update->id); + $report->state('confirmed'); + $report->resend; + $report->update; + return 1; + } + my $private_comments = $update->get_extra_metadata('private_comments'); my $has_text = $update->text || $private_comments; return $update->user->from_body && !$has_text; diff --git a/perllib/FixMyStreet/SendReport/Open311.pm b/perllib/FixMyStreet/SendReport/Open311.pm index f4bdfcea355..4f3f4df4955 100644 --- a/perllib/FixMyStreet/SendReport/Open311.pm +++ b/perllib/FixMyStreet/SendReport/Open311.pm @@ -30,7 +30,7 @@ sub send { fixmystreet_body => $body, ); - my $contact = $self->fetch_category($body, $row) or return; + my $contact = $self->fetch_category($body, $row, $row->get_extra_metadata('open311_category_override')) or return; my $cobrand = $body->get_cobrand_handler || $row->get_cobrand_logged; $cobrand->call_hook(open311_config => $row, $h, \%open311_params, $contact); diff --git a/t/cobrand/bromley.t b/t/cobrand/bromley.t index 1ff0237b80b..694cb160489 100644 --- a/t/cobrand/bromley.t +++ b/t/cobrand/bromley.t @@ -612,4 +612,67 @@ for my $test ( }; } +subtest "mark-open comment on a closed echo report result in a resend under 'Investigation Required'" => sub { + my $event_guid = '05a10cb2-44c9-48d9-92a2-cc6788994bae'; + my $event_id = 123; + + my $echo = Test::MockModule->new('Integrations::Echo'); + + $echo->mock('GetEvent', sub { { + Guid => $event_guid, + Id => $event_id, + } } ); + + $mech->create_contact_ok( + body_id => $body->id, + category => 'Investigation Required', + email => 'investigation_required', + ); + + my ($report) = $mech->create_problems_for_body(1, $body->id, 'echo report', { + cobrand => 'bromley', + whensent => 'now()', + send_state => 'sent', + send_method_used => 'Open311', + external_id => $event_guid, + }); + $report->state('closed'); + my $comment = $report->add_to_comments({ + text => 'comment on closed event', + user => $user, + mark_open => 1, + }); + $report->update; + FixMyStreet::override_config { + ALLOWED_COBRANDS => 'bromley', + }, sub { + my $updates = Open311::PostServiceRequestUpdates->new(); + $updates->send; + }; + + $report->discard_changes; + is $report->get_extra_metadata('open311_category_override'), 'Investigation Required', 'category override applied'; + is $report->send_state, 'unprocessed', 'report set to be resent'; + + $comment->discard_changes; + is $comment->send_state, 'skipped', "skipped sending comment"; + + FixMyStreet::override_config { + STAGING_FLAGS => { send_reports => 1 }, + ALLOWED_COBRANDS => [ 'bromley' ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + FixMyStreet::Script::Reports::send(); + }; + + $report->discard_changes; + is $report->send_state, 'sent', 'report was resent'; + + my $req = Open311->test_req_used; + my $c = CGI::Simple->new($req->content); + is $c->param('attribute[Event_ID]'), $event_id, 'old event ID included in attributes'; + like $c->param('description'), qr/Closed report has a new comment: comment on closed event/, 'private comments included in description'; +}; + + done_testing();