From 5c871b7093ac2fd137b980979bb210cc1cad59a7 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Fri, 21 Mar 2014 17:30:07 +0000 Subject: Add Cobrand config to include update date in email alerts This is useful for sites where the updates aren't published on the site so you need a bit more context in the email alerts. Add inlude_time_in_update_alerts to default cobrand, off as default If this is set then the date is added after the name string in the email, passing in 'alert' as the type to dt_prettify in order to allow the cobrand to format it accordingly. This does require pulling out the alert confirmed time from the database in the send_alerts code where it wasn't before. --- perllib/FixMyStreet/Cobrand/Default.pm | 2 + perllib/FixMyStreet/DB/ResultSet/AlertType.pm | 22 +++++++ t/app/controller/alert_new.t | 91 +++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 60ed216d7..d2af4949e 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -837,6 +837,8 @@ If true then we never send an email to confirm an update sub never_confirm_updates { 0; } +sub include_time_in_update_alerts { 0; } + =head2 prettify_dt my $date = $c->prettify_dt( $datetime ); diff --git a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm index 8e18832a9..545b54c60 100644 --- a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm +++ b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm @@ -29,6 +29,7 @@ sub email_alerts ($) { $query .= " $item_table.id as item_id, $item_table.text as item_text, $item_table.name as item_name, $item_table.anonymous as item_anonymous, + $item_table.confirmed as item_confirmed, $head_table.* from alert inner join $item_table on alert.parameter::integer = $item_table.${head_table}_id @@ -114,6 +115,27 @@ sub email_alerts ($) { $data{problem_url} = $url . "/report/" . $row->{id}; } $data{data} .= $row->{item_name} . ' : ' if $row->{item_name} && !$row->{item_anonymous}; + if ( $cobrand->include_time_in_update_alerts ) { + # this is basically recreating the code from the inflate wrapper + # in the database model. + my $tz; + if ( FixMyStreet->config('TIME_ZONE') ) { + $tz = FixMyStreet->config('TIME_ZONE'); + } + + my $parser = DateTime::Format::Pg->new(); + my $dt = $parser->parse_timestamp( $row->{item_confirmed} ); + my $l_tz = DateTime::TimeZone->new( name => "local" ); + # We need to always set this otherwise we end up with the DateTime + # object being in the floating timezone in which case applying a + # subsequent timezone set will have no effect. + $dt->set_time_zone( $l_tz ); + if ( $tz ) { + my $tz_obj = DateTime::TimeZone->new( name => $tz ); + $dt->set_time_zone( $tz_obj ); + } + $data{data} .= $cobrand->prettify_dt( $dt, 'alert' ) . "\n\n"; + } $data{data} .= $row->{item_text} . "\n\n------\n\n"; # this is ward and council problems } else { diff --git a/t/app/controller/alert_new.t b/t/app/controller/alert_new.t index 532b071eb..594d4c7a6 100644 --- a/t/app/controller/alert_new.t +++ b/t/app/controller/alert_new.t @@ -683,4 +683,95 @@ subtest 'check new updates alerts for non public reports only go to report owner $mech->delete_user( $user3 ); }; +subtest 'check setting inlude dates in new updates cobrand option' => sub { + my $include_date_in_alert_override= Sub::Override->new( + "FixMyStreet::Cobrand::Default::include_time_in_update_alerts", + sub { return 1; } + ); + $mech->delete_user( 'reporter@example.com' ); + $mech->delete_user( 'alerts@example.com' ); + + my $user1 = FixMyStreet::App->model('DB::User') + ->find_or_create( { email => 'reporter@example.com', name => 'Reporter User' } ); + ok $user1, "created test user"; + + my $user2 = FixMyStreet::App->model('DB::User') + ->find_or_create( { email => 'alerts@example.com', name => 'Alert User' } ); + ok $user2, "created test user"; + + my $user3 = FixMyStreet::App->model('DB::User') + ->find_or_create( { email => 'updates@example.com', name => 'Update User' } ); + ok $user3, "created test user"; + + my $dt = DateTime->now->add( minutes => -30 ); + my $r_dt = $dt->clone->add( minutes => 20 ); + + my $dt_parser = FixMyStreet::App->model('DB')->schema->storage->datetime_parser; + + my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( { + postcode => 'EH1 1BB', + bodies_str => '2651', + areas => ',11808,135007,14419,134935,2651,20728,', + category => 'Street lighting', + title => 'Alert test for non public reports', + detail => 'Testing Detail', + used_map => 1, + name => $user2->name, + anonymous => 0, + state => 'confirmed', + confirmed => $dt_parser->format_datetime($r_dt), + lastupdate => $dt_parser->format_datetime($r_dt), + whensent => $dt_parser->format_datetime($r_dt->clone->add( minutes => 5 )), + lang => 'en-gb', + service => '', + cobrand => 'default', + cobrand_data => '', + send_questionnaire => 1, + latitude => '55.951963', + longitude => '-3.189944', + user_id => $user2->id, + } ); + + my $update = FixMyStreet::App->model('DB::Comment')->create( { + problem_id => $report->id, + user_id => $user3->id, + name => 'Anonymous User', + mark_fixed => 'false', + text => 'This is some more update text', + state => 'confirmed', + confirmed => $r_dt->clone->add( minutes => 8 ), + anonymous => 't', + } ); + + my $alert_user1 = FixMyStreet::App->model('DB::Alert')->create( { + user => $user1, + alert_type => 'new_updates', + parameter => $report->id, + confirmed => 1, + whensubscribed => $dt, + } ); + ok $alert_user1, "alert created"; + + + $mech->clear_emails_ok; + FixMyStreet::App->model('DB::AlertType')->email_alerts(); + $mech->email_count_is(1); + + # if we don't do this then we're applying the date inflation code and + # it's timezone munging to the DateTime object above and not the DateTime + # object that's inflated from the database value and these turn out to be + # different as the one above has a UTC timezone and not the floating one + # that those from the DB do. + $update->discard_changes(); + + my $date_in_alert = Utils::prettify_dt( $update->confirmed ); + my $email = $mech->get_email; + like $email->body, qr/$date_in_alert/, 'alert contains date'; + + $mech->delete_user( $user1 ); + $mech->delete_user( $user2 ); + $mech->delete_user( $user3 ); + $include_date_in_alert_override->restore(); +}; + done_testing(); -- cgit v1.2.3