diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 5 | ||||
-rw-r--r-- | t/open311/getservicerequestupdates.t | 79 |
3 files changed, 83 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index ae3b4fcd5..71fef3855 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ - Move summary failures to a separate script. - Add script to export/import body data. - Add fetch script that does combined job of fetch-comments and fetch-reports. + - Open311 improvements: + - match response templates on external status code over state - UK: - Added junction lookup, so you can search for things like "M60, Junction 2" diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index 09b1f6b26..9fa81ac9e 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -304,17 +304,20 @@ sub comment_text_for_request { my $ext_code_changed = $ext_code ne $old_ext_code; my $template; if ($state_changed || $ext_code_changed) { + my $order; my $state_params = { 'me.state' => $state }; if ($ext_code) { $state_params->{'me.external_status_code'} = $ext_code; + # make sure that empty string/nulls come last. + $order = { order_by => \"me.external_status_code DESC NULLS LAST" }; }; if (my $t = $problem->response_templates->search({ auto_response => 1, -or => $state_params, - })->first) { + }, $order )->first) { $template = $t->text; } } diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index 07c3b4cdd..35be0f7b5 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -932,8 +932,6 @@ subtest 'check that external_status_code triggers auto-responses' => sub { $problem->discard_changes; is $problem->comments->count, 1, 'one comment after fetching updates'; - my $comment = $problem->comments->first; - is $problem->comments->first->text, "Thank you for your report. We will provide an update within 24 hours.", "correct external status code on first comment"; }; @@ -991,6 +989,83 @@ foreach my $test ( { $problem->comments->delete; }; } + +my $response_template_in_progress = $bodies{2482}->response_templates->create({ + title => "Acknowledgement 1", + text => "Thank you for your report. We will provide an update within 48 hours.", + auto_response => 1, + state => "in progress" +}); + +for my $test ( + { + external_code => '090', + description => 'check numeric external status code in response template override state', + }, + { + external_code => 'futher', + description => 'check alpha external status code in response template override state', + }, +) { + subtest $test->{description} => 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></description> + <updated_datetime>UPDATED_DATETIME</updated_datetime> + <external_status_code></external_status_code> + </request_update> + <request_update> + <update_id>638345</update_id> + <service_request_id>@{[ $problem->external_id ]}</service_request_id> + <status>in_progress</status> + <description></description> + <updated_datetime>UPDATED_DATETIME2</updated_datetime> + <external_status_code>@{[ $test->{external_code} ]}</external_status_code> + </request_update> + </service_requests_updates> + }; + + my $response_template = $bodies{2482}->response_templates->create({ + # the default ordering uses the title of the report so + # make sure this comes second + title => "Acknowledgement 2", + text => "Thank you for your report. We will provide an update within 24 hours.", + auto_response => 1, + external_status_code => $test->{external_code} + }); + + $problem->comments->delete; + + my $dt2 = $dt->clone->add( minutes => 1 ); + $requests_xml =~ s/UPDATED_DATETIME/$dt/; + $requests_xml =~ s/UPDATED_DATETIME2/$dt2/; + + 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, 2, 'two comment after fetching updates'; + + my @comments = $problem->comments; + + is $comments[0]->text, "Thank you for your report. We will provide an update within 48 hours.", "correct external status code on first comment"; + is $comments[1]->text, "Thank you for your report. We will provide an update within 24 hours.", "correct external status code on second comment"; + $problem->comments->delete; + $response_template->delete; + }; +} + subtest 'check that first comment always updates state' => sub { my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> <service_requests_updates> |