diff options
author | Matthew Somerville <matthew@mysociety.org> | 2020-04-22 09:02:42 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2020-05-04 16:39:19 +0100 |
commit | 63df5a61f7be9da0498a036a18f8b01e69f103cf (patch) | |
tree | 1b23db349f1cad37d815a075ed340015c2ec7e4b | |
parent | dfdfa76375e39274d6b8ff2a06954b4d5530586d (diff) |
Allow template to be an initial update on reports.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | docs/_includes/admin-tasks-content.md | 6 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 48 | ||||
-rw-r--r-- | t/app/controller/report_new_update.t | 71 |
4 files changed, 121 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 62fdd47ec..fb8d8c6bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Disable staff private tickbox on new reports if category is private. #2961 - Move stats from main admin index to stats index. - Speed up dashboard export and report search. + - Allow a template to be an initial update on reports. #2973 - Bugfixes - Application user in Docker container can't install packages. #2914 - Look at all categories when sending reports. diff --git a/docs/_includes/admin-tasks-content.md b/docs/_includes/admin-tasks-content.md index 860d1f21c..7e3d47efe 100644 --- a/docs/_includes/admin-tasks-content.md +++ b/docs/_includes/admin-tasks-content.md @@ -748,6 +748,12 @@ process. In this instance, if your Open311 server returns extra text as part of the update, you may put the placeholder `{% raw %}{{description}}{% endraw %}` in the template here, and that placeholder will be replaced by the text from the Open311 server. +If you don’t have an Open311 connection, or your Open311 connection does not +provide an immediate initial update, there is a special case where if a +template is assigned to the Open state, and marked as ‘auto-response’, then it +will automatically be added as a first update to any new report created that +matches the template (ie. in the relevant category if assigned). This lets +you give e.g. estimated timescales or other useful information up front. #### Editing or deleting a template diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 69d20171a..fc1a78cd5 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -158,7 +158,7 @@ sub report_new_ajax : Path('mobile') : Args(0) { my $report = $c->stash->{report}; if ( $report->confirmed ) { - $c->forward( 'create_reporter_alert' ); + $c->forward( 'create_related_things' ); $c->stash->{ json_response } = { success => 1, report => $report->id }; } else { $c->forward( 'send_problem_confirm_email' ); @@ -1363,7 +1363,7 @@ sub process_confirmation : Private { ); # Subscribe problem reporter to email updates - $c->forward( '/report/new/create_reporter_alert' ); + $c->forward( '/report/new/create_related_things' ); # log the problem creation user in to the site if ( $data->{name} || $data->{password} ) { @@ -1623,7 +1623,7 @@ sub redirect_or_confirm_creation : Private { # If confirmed send the user straight there. if ( $report->confirmed ) { # Subscribe problem reporter to email updates - $c->forward( 'create_reporter_alert' ); + $c->forward( 'create_related_things' ); if ($c->stash->{contributing_as_another_user} && $report->user->email && $report->user->id != $c->user->id && !$c->cobrand->report_sent_confirmation_email) { @@ -1665,13 +1665,51 @@ sub redirect_or_confirm_creation : Private { $c->log->info($report->user->id . ' created ' . $report->id . ", $thing sent, " . ($c->stash->{token_data}->{password} ? 'password set' : 'password not set')); } -sub create_reporter_alert : Private { +sub create_related_things : Private { my ( $self, $c ) = @_; + my $problem = $c->stash->{report}; + + # If there is a special template, create a comment using that + foreach my $body (values %{$problem->bodies}) { + my $user = $body->comment_user or next; + + my %open311_conf = ( + endpoint => $body->endpoint || '', + api_key => $body->api_key || '', + jurisdiction => $body->jurisdiction || '', + extended_statuses => $body->send_extended_statuses, + ); + + my $cobrand = $body->get_cobrand_handler; + $cobrand->call_hook(open311_config_updates => \%open311_conf) + if $cobrand; + + my $open311 = Open311->new(%open311_conf); + my $updates = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $open311, + current_body => $body, + blank_updates_permitted => 1, + ); + + my $description = $updates->comment_text_for_request({}, $problem, 'confirmed', 'dummy', '', ''); + next unless $description; + + my $request = { + service_request_id => $problem->id, + update_id => 'auto-internal', + comment_time => DateTime->now, + status => 'open', + description => $description, + }; + $updates->process_update($request, $problem); + } + + # And now the reporter alert return if $c->stash->{no_reporter_alert}; return if $c->cobrand->call_hook('suppress_reporter_alerts'); - my $problem = $c->stash->{report}; my $alert = $c->model('DB::Alert')->find_or_create( { user => $problem->user, alert_type => 'new_updates', diff --git a/t/app/controller/report_new_update.t b/t/app/controller/report_new_update.t new file mode 100644 index 000000000..cbb31cea4 --- /dev/null +++ b/t/app/controller/report_new_update.t @@ -0,0 +1,71 @@ +use FixMyStreet::TestMech; + +# disable info logs for this test run +FixMyStreet::App->log->disable('info'); +END { FixMyStreet::App->log->enable('info'); } + +my $mech = FixMyStreet::TestMech->new; + +my $comment_user = $mech->create_user_ok('systemuser@example.org', name => 'Glos Council'); +my $body = $mech->create_body_ok(2226, 'Gloucestershire County Council', { + comment_user => $comment_user, +}); + +$mech->create_contact_ok( + body_id => $body->id, + category => 'Potholes', + email => 'potholes@example.com', +); + +my $user = $mech->log_in_ok('test-2@example.com'); + +subtest "test report creation with no initial auto-update" => sub { + my $report = make_report(); + my $comment = FixMyStreet::DB->resultset('Comment')->count; + is $comment, 0, 'No comments left'; + $report->delete; +}; + +my $template = FixMyStreet::DB->resultset("ResponseTemplate")->create({ + body => $body, + state => 'confirmed', + title => 'Initial email response', + text => 'Thanks for your report. We will investigate within 5 working days.', + auto_response => 1, +}); +ok $template, 'Template created'; + +subtest "test report creation with initial auto-update" => sub { + my $report = make_report(); + my $comment = FixMyStreet::DB->resultset('Comment')->single; + is $comment->text, 'Thanks for your report. We will investigate within 5 working days.'; + is $comment->problem->id, $report->id; + is $comment->user->id, $comment_user->id; + is $comment->external_id, 'auto-internal'; + is $comment->name, 'Glos Council'; +}; + +done_testing; + +sub make_report { + FixMyStreet::override_config { + ALLOWED_COBRANDS => 'fixmystreet', + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->get_ok('/around?pc=GL50+2PR'); + $mech->follow_link_ok({ text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + $mech->submit_form_ok({ + with_fields => { + title => "Test Report", + detail => 'Test report details.', + name => 'Joe Bloggs', + category => 'Potholes', + } + }, "submit good details"); + }; + + my $report = $user->problems->first; + ok $report, "Found the report"; + + return $report; +} |