From 44674bb7b3abd62c6b42fd717090a47fe8f9b02e Mon Sep 17 00:00:00 2001 From: Nik Gupta Date: Mon, 20 May 2024 17:14:38 +0100 Subject: [PATCH] Include status change line in update emails using auto response templates. Except for waste categories. https://github.com/mysociety/fixmystreet/issues/4901 --- CHANGELOG.md | 1 + perllib/FixMyStreet/Script/Alerts.pm | 3 ++ t/app/controller/alert_new.t | 81 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51c2c4f1d23..f7b89f44e5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - Switch to OpenStreetMap for reverse geocoding. #4444 - Convert all uploaded images to JPEGs. - Redirect after POST when creating reports. #4362 + - Include status change line in report update emails using auto response templates. * v5.0 (10th May 2023) - Front end improvements: diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm index 426f8351943..00400f5730b 100644 --- a/perllib/FixMyStreet/Script/Alerts.pm +++ b/perllib/FixMyStreet/Script/Alerts.pm @@ -133,6 +133,9 @@ sub send_alert_type { $row->{item_text_original} = $row->{item_text}; $row->{item_text} = $row->{item_text} ? $row->{item_text} . "\n\n" . $update : $update; + if ($row->{item_private_email_text} && $report->cobrand_data ne 'waste') { + $row->{item_private_email_text} = $row->{item_private_email_text} . "\n\n" . $update; + } $last_problem_state = $row->{item_problem_state}; } next unless $row->{item_text}; diff --git a/t/app/controller/alert_new.t b/t/app/controller/alert_new.t index 0ada5775b0a..5af01d2b003 100644 --- a/t/app/controller/alert_new.t +++ b/t/app/controller/alert_new.t @@ -2,6 +2,9 @@ use utf8; use FixMyStreet::TestMech; use FixMyStreet::Script::Alerts; +use_ok( 'Open311::GetUpdates' ); +use_ok( 'Open311' ); + use t::Mock::Twilio; my $twilio = t::Mock::Twilio->new; LWP::Protocol::PSGI->register($twilio->to_psgi_app, host => 'api.twilio.com'); @@ -1044,5 +1047,83 @@ FixMyStreet::override_config { }; }; +subtest 'check state update line included in auto response template alerts' => sub { + my $requests_xml = qq{ + + + 1234 + investigating + Test Service + test_service_code + + + + 2010-04-14T06:37:38-08:00 + UPDATED_DATETIME + 37.762221815 + -122.4651145 + + + }; + my $dt = DateTime->now->add( minutes => -30 ); + + my $system_user = FixMyStreet::DB->resultset('User')->find_or_create( + { + email => 'system_user@example.com' + } + ); + my $template = FixMyStreet::DB->resultset("ResponseTemplate")->create({ + body => $body, + state => 'investigating', + title => 'Investigating email response', + text => "Cheers we'll have a gander.", + email_text => "Cheers we'll have a gander.", + auto_response => 1, + }); + my $resident_user = $mech->create_user_ok('response-template-test@example.org'); + my $contact = $mech->create_contact_ok( body_id => $body->id, category => 'Whatever', email => 'WHATEVER' ); + my ($report) = $mech->create_problems_for_body(1, $body->id, 'Testing', { + user => $resident_user, + state => 'confirmed', + external_id => 1234, + lastupdate => $dt->add( minutes => -1), + category => $contact->category, + }); + + + my $alert = FixMyStreet::DB->resultset('Alert')->create({ + user => $resident_user, + alert_type => 'new_updates', + parameter => $report->id, + confirmed => 1, + whensubscribed => $dt, + }); + ok $alert, "alert created"; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com' ); + + $requests_xml =~ s/UPDATED_DATETIME/$dt/; + Open311->_inject_response('/requests.xml', $requests_xml); + + FixMyStreet::override_config { + ALLOWED_COBRANDS => 'fixmystreet', + }, sub { + my $updates = Open311::GetUpdates->new( + system_user => $system_user, + current_open311 => $o, + current_body => $body, + blank_updates_permitted => 1, + ); + $updates->update_reports( [ $report ] ); + $report->discard_changes; + FixMyStreet::Script::Alerts::send_updates(); + }; + + $mech->email_count_is(1); + my $email = $mech->get_text_body_from_email; + like $email, qr/Cheers we'll have a gander/, ''; + like $email, qr/State changed to: Investigating/, ''; + +}; done_testing();