diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 14 | ||||
-rw-r--r-- | t/open311/getservicerequestupdates.t | 34 |
3 files changed, 43 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f475bfa5..25a3d126f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,7 @@ - Add support for account_id parameter to POST Service Request calls. - Do not overwrite/remove protected meta data. #2598 - Spot multiple groups inside a <groups> element. + - Always update problem state from first comment #2832 - Backwards incompatible changes: - The FixMyStreet templating code will now escape all variables by default. If you need to output HTML in a variable directly, you will diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index 2489bd611..da3461f6e 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -203,12 +203,14 @@ sub process_update { $comment->problem_state($state); - # if the comment is older than the last update do not - # change the status of the problem as it's tricky to - # determine the right thing to do. Allow the same time in - # case report/update created at same time (in external - # system). Only do this if the report is currently visible. - if ( $comment->created >= $p->lastupdate && $p->state ne $state && $p->is_visible ) { + # we only want to update the problem state if that makes sense. We never want to unhide a problem. + # If the update is older than the last update then we also do not want to update the state. This + # is largely to avoid the situation where we miss some updates, make more updates and then catch + # the updates when we fetch the last 24 hours of updates. The exception to this is the first + # comment. This is to catch automated updates which happen faster than we get the external_id + # back from the endpoint and hence have an created time before the lastupdate. + if ( $p->is_visible && $p->state ne $state && + ( $comment->created >= $p->lastupdate || $p->comments->count == 0 ) ) { $p->state($state); } } diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index 6ed1d44fd..67a74e133 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -1009,6 +1009,40 @@ foreach my $test ( { $problem->comments->delete; }; } +subtest 'check that first comment always updates state' => sub { + my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> + <service_requests_updates> + <request_update> + <update_id>638344</update_id> + <service_request_id>@{[ $problem->external_id ]}</service_request_id> + <status>in_progress</status> + <description>This is a note</description> + <updated_datetime>UPDATED_DATETIME</updated_datetime> + </request_update> + </service_requests_updates> + }; + + $problem->state( 'confirmed' ); + $problem->lastupdate( $dt->clone->subtract( hours => 1 ) ); + $problem->update; + + $requests_xml =~ s/UPDATED_DATETIME/@{[$dt->clone->subtract( minutes => 62 )]}/; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $requests_xml } ); + + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, + ); + + $update->process_body; + + $problem->discard_changes; + is $problem->comments->count, 1, 'one comment after fetching updates'; + is $problem->state, 'in progress', 'correct problem status'; + $problem->comments->delete; +}; foreach my $test ( { desc => 'normally alerts are not suppressed', |