Skip to content

Commit

Permalink
[Bromley] Create 'Investigation Required' events for comments on clos…
Browse files Browse the repository at this point in the history
…ed Echo-backed reports.

See mysociety/societyworks#3267.
  • Loading branch information
neprune committed Mar 7, 2024
1 parent 80c46ec commit 63ed4e0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
47 changes: 45 additions & 2 deletions perllib/FixMyStreet/Cobrand/Bromley.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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' };
}
Expand All @@ -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} };

Check warning on line 245 in perllib/FixMyStreet/Cobrand/Bromley.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Cobrand/Bromley.pm#L242-L245

Added lines #L242 - L245 were not covered by tests
}

return $open311_only;
}

Expand All @@ -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);

Check warning on line 275 in perllib/FixMyStreet/Cobrand/Bromley.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Cobrand/Bromley.pm#L275

Added line #L275 was not covered by tests
if ($comment && $comment->text) {
my $text = "Closed report has a new comment: " . $comment->text . "\n\n" . $row->detail;
$row->detail($text);

Check warning on line 278 in perllib/FixMyStreet/Cobrand/Bromley.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Cobrand/Bromley.pm#L277-L278

Added lines #L277 - L278 were not covered by tests
}
}
}

sub _include_user_title_in_extra {
Expand Down Expand Up @@ -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/;

Check warning on line 403 in perllib/FixMyStreet/Cobrand/Bromley.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Cobrand/Bromley.pm#L401-L403

Added lines #L401 - L403 were not covered by tests
}

=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;

Check warning on line 427 in perllib/FixMyStreet/Cobrand/Bromley.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Cobrand/Bromley.pm#L422-L427

Added lines #L422 - L427 were not covered by tests
}

my $private_comments = $update->get_extra_metadata('private_comments');
my $has_text = $update->text || $private_comments;
return $update->user->from_body && !$has_text;
Expand Down
2 changes: 1 addition & 1 deletion perllib/FixMyStreet/SendReport/Open311.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 63 additions & 0 deletions t/cobrand/bromley.t
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 63ed4e0

Please sign in to comment.