Skip to content

Commit

Permalink
[Echo] Allow duplicate posted update if state change.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Jun 4, 2024
1 parent d9b2a01 commit 359d970
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 58 deletions.
2 changes: 1 addition & 1 deletion perllib/FixMyStreet/App/Controller/Waste/Echo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ sub check_existing_update : Private {
my $cfg = { updates => $updates };
$c->detach('soap_ok')
unless $c->cobrand->waste_check_last_update(
$cfg, $p, $request->{status}, $request->{external_status_code});
'push', $cfg, $p, $request->{status}, $request->{external_status_code});
}

__PACKAGE__->meta->make_immutable;
Expand Down
8 changes: 5 additions & 3 deletions perllib/FixMyStreet/Roles/Cobrand/Echo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ sub waste_fetch_events {

next if !$request->{status} || $request->{status} eq 'confirmed'; # Still in initial state
next unless $self->waste_check_last_update(
$cfg, $report, $request->{status}, $request->{external_status_code});
'fetch', $cfg, $report, $request->{status}, $request->{external_status_code});

my $last_updated = construct_bin_date($event->{LastUpdatedDate});
$request->{comment_time} = $last_updated;
Expand Down Expand Up @@ -738,7 +738,7 @@ sub waste_get_event_type {
# We only have the report's current state, no history, so must check current
# against latest received update to see if state the same, and skip if so
sub waste_check_last_update {
my ($self, $cfg, $report, $status, $resolution_id) = @_;
my ($self, $type, $cfg, $report, $status, $resolution_id) = @_;

Check warning on line 741 in perllib/FixMyStreet/Roles/Cobrand/Echo.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Roles/Cobrand/Echo.pm#L741

Added line #L741 was not covered by tests

my $latest = $report->comments->search(
{ external_id => 'waste', },
Expand All @@ -748,7 +748,9 @@ sub waste_check_last_update {
if ($latest) {
my $state = $cfg->{updates}->current_open311->map_state($status);
my $code = $latest->get_extra_metadata('external_status_code') || '';
if ($latest->problem_state eq $state && $code eq $resolution_id) {
my $problem_state = $latest->problem->state;

Check warning on line 751 in perllib/FixMyStreet/Roles/Cobrand/Echo.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Roles/Cobrand/Echo.pm#L751

Added line #L751 was not covered by tests
if ($latest->problem_state eq $state && $code eq $resolution_id
&& !($type eq 'push' && $problem_state ne $state)) {
print " Latest update matches fetched state, skipping\n" if $cfg->{verbose};
return;
}
Expand Down
123 changes: 69 additions & 54 deletions t/cobrand/bromley.t
Original file line number Diff line number Diff line change
Expand Up @@ -828,66 +828,81 @@ subtest 'redirecting of reports between backends' => sub {
is $c->param('service_code'), 'LBB_RRE_FROM_VEOLIA_STREETS';
like $c->param('description'), qr/Handover notes - Outgoing notes from Echo/;
};
};

};

subtest "comment on a closed echo report result in a resend under 'Referred to Veolia Streets'" => 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,
ResolvedDate => { DateTime => '2024-03-21T12:00:00Z' },
Id => $event_id,
} } );
subtest "comment on a closed echo report result in a resend under 'Referred to Veolia Streets'" => 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,
ResolvedDate => { DateTime => '2024-03-21T12:00:00Z' },
Id => $event_id,
} } );

($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->add_to_comments({
text => 'Completed',
user => $user,
send_state => 'processed',
problem_state => 'fixed - council',
external_id => 'waste',
});
$report->state('fixed - council');
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;
};

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'), 'Referred to Veolia Streets', '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->get_extra_metadata('open311_category_override'), 'Referred to Veolia Streets', 'category override applied';
is $report->send_state, 'unprocessed', 'report set to be resent';
$report->discard_changes;
is $report->send_state, 'sent', 'report was resent';

$comment->discard_changes;
is $comment->send_state, 'skipped', "skipped sending comment";
my $req = Open311->test_req_used;
my $c = CGI::Simple->new($req->content);
my $detail = $report->detail;
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\r\nBromley pkg-tcobrandbromleyt-bromley\@example.com\r\n$detail/, 'Comment on closed report included in new report description';
};

FixMyStreet::override_config {
STAGING_FLAGS => { send_reports => 1 },
ALLOWED_COBRANDS => [ 'bromley' ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
FixMyStreet::Script::Reports::send();
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, '');
$mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in);
is $report->comments->count, 3, 'A new update';
$report->discard_changes;
is $report->state, 'fixed - council', 'A state change';
};
};

$report->discard_changes;
is $report->send_state, 'sent', 'report was resent';

my $req = Open311->test_req_used;
my $c = CGI::Simple->new($req->content);
my $detail = $report->detail;
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\r\nBromley pkg-tcobrandbromleyt-bromley\@example.com\r\n$detail/, 'Comment on closed report included in new report description';
};

done_testing();

0 comments on commit 359d970

Please sign in to comment.