diff options
author | Matthew Somerville <matthew@mysociety.org> | 2019-10-16 14:04:27 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2019-12-17 12:47:49 +0000 |
commit | 8abf366f8fdecb8c4b498417ca830d79cf90a026 (patch) | |
tree | 2db796a4e258869decce5d841b8f24dee7030d5a | |
parent | 72e985a90f71a63176b19cd8fb35e10408ef1e48 (diff) |
[Bexley] Out-of-hours email.
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bexley.pm | 51 | ||||
-rw-r--r-- | t/cobrand/bexley.t | 54 |
2 files changed, 91 insertions, 14 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Bexley.pm b/perllib/FixMyStreet/Cobrand/Bexley.pm index 95c9aac84..ae15900d6 100644 --- a/perllib/FixMyStreet/Cobrand/Bexley.pm +++ b/perllib/FixMyStreet/Cobrand/Bexley.pm @@ -3,6 +3,11 @@ use parent 'FixMyStreet::Cobrand::Whitelabel'; use strict; use warnings; +use Encode; +use JSON::MaybeXS; +use LWP::Simple qw($ua); +use Path::Tiny; +use Time::Piece; sub council_area_id { 2494 } sub council_area { 'Bexley' } @@ -133,17 +138,20 @@ sub open311_post_send { my $dangerous = $row->get_extra_field_value('dangerous') || ''; my $p1_email = 0; + my $outofhours_email = 0; if ($row->category eq 'Abandoned and untaxed vehicles') { my $burnt = $row->get_extra_field_value('burnt') || ''; $p1_email = 1 if $burnt eq 'Yes'; } elsif ($row->category eq 'Dead animal') { $p1_email = 1; + $outofhours_email = 1; } elsif ($row->category eq 'Parks and open spaces') { my $reportType = $row->get_extra_field_value('reportType') || ''; $p1_email = 1 if $reportType =~ /locked in a park|Wild animal/; $p1_email = 1 if $dangerous eq 'Yes' && $reportType =~ /Playgrounds|park furniture|gates are broken|Vandalism|Other/; } elsif (!$lighting{$row->category}) { $p1_email = 1 if $dangerous eq 'Yes'; + $outofhours_email = 1 if $dangerous eq 'Yes'; } my @to; @@ -158,6 +166,9 @@ sub open311_post_send { my @flooding = split /,/, $emails->{flooding}; push @to, [ $_, 'FixMyStreet Bexley Flooding' ] for @flooding; } + if ($outofhours_email && _is_out_of_hours() && $emails->{outofhours}) { + push @to, [ $emails->{outofhours}, 'Bexley out of hours' ]; + } if ($contact->email =~ /^Uniform/ && $emails->{eh}) { my @eh = split ',', $emails->{eh}; push @to, [ $_, 'FixMyStreet Bexley EH' ] for @eh; @@ -202,4 +213,44 @@ sub dashboard_export_problems_add_columns { }; } +sub _is_out_of_hours { + my $time = localtime; + return 1 if $time->hour > 16 || ($time->hour == 16 && $time->min >= 45); + return 1 if $time->hour < 8; + return 1 if $time->wday == 1 || $time->wday == 7; + return 1 if _is_bank_holiday(); + return 0; +} + +sub _is_bank_holiday { + my $json = _get_bank_holiday_json(); + my $today = localtime->date; + for my $event (@{$json->{'england-and-wales'}{events}}) { + if ($event->{date} eq $today) { + return 1; + } + } +} + +sub _get_bank_holiday_json { + my $file = 'bank-holidays.json'; + my $cache_file = path(FixMyStreet->path_to("../data/$file")); + my $js; + if (-s $cache_file && -M $cache_file <= 7 && !FixMyStreet->config('STAGING_SITE')) { + # uncoverable statement + $js = $cache_file->slurp_utf8; + } else { + $ua->timeout(5); + $js = get("https://www.gov.uk/$file"); + # uncoverable branch false + $js = decode_utf8($js) if !utf8::is_utf8($js); + if ($js && !FixMyStreet->config('STAGING_SITE')) { + # uncoverable statement + $cache_file->spew_utf8($js); + } + } + $js = JSON->new->decode($js) if $js; + return $js; +} + 1; diff --git a/t/cobrand/bexley.t b/t/cobrand/bexley.t index 18026dbaf..d639f9be9 100644 --- a/t/cobrand/bexley.t +++ b/t/cobrand/bexley.t @@ -1,9 +1,12 @@ use CGI::Simple; use Test::MockModule; +use Test::MockTime qw(:all); use FixMyStreet::TestMech; use FixMyStreet::Script::Reports; use Catalyst::Test 'FixMyStreet::App'; +set_fixed_time('2019-10-16T17:00:00Z'); # Out of hours + use_ok 'FixMyStreet::Cobrand::Bexley'; use_ok 'FixMyStreet::Geocode::Bexley'; @@ -48,6 +51,7 @@ FixMyStreet::override_config { COBRAND_FEATURES => { open311_email => { bexley => { p1 => 'p1@bexley', lighting => 'thirdparty@notbexley.example.com,another@notbexley.example.com', + outofhours => 'outofhours@bexley', flooding => 'flooding@bexley', eh => 'eh@bexley', } } }, @@ -66,32 +70,32 @@ FixMyStreet::override_config { my $report; foreach my $test ( - { category => 'Abandoned and untaxed vehicles', email => 1, code => 'ABAN', + { category => 'Abandoned and untaxed vehicles', email => ['p1'], code => 'ABAN', extra => { 'name' => 'burnt', description => 'Was it burnt?', 'value' => 'Yes' } }, { category => 'Abandoned and untaxed vehicles', code => 'ABAN', extra => { 'name' => 'burnt', description => 'Was it burnt?', 'value' => 'No' } }, - { category => 'Dead animal', email => 1, code => 'ANIM' }, - { category => 'Something dangerous', email => 1, code => 'DANG', + { category => 'Dead animal', email => ['p1', 'outofhours'], code => 'ANIM' }, + { category => 'Something dangerous', email => ['p1', 'outofhours'], code => 'DANG', extra => { 'name' => 'dangerous', description => 'Was it dangerous?', 'value' => 'Yes' } }, { category => 'Something dangerous', code => 'DANG', extra => { 'name' => 'dangerous', description => 'Was it dangerous?', 'value' => 'No' } }, - { category => 'Parks and open spaces', email => 1, code => 'ConfirmPARK', + { category => 'Parks and open spaces', email => ['p1'], code => 'ConfirmPARK', extra => { 'name' => 'reportType', description => 'Type of report', 'value' => 'Wild animal' } }, { category => 'Parks and open spaces', code => 'ConfirmPARK', extra => { 'name' => 'reportType', description => 'Type of report', 'value' => 'Maintenance' } }, { category => 'Parks and open spaces', code => 'ConfirmPARK', extra => { 'name' => 'dangerous', description => 'Was it dangerous?', 'value' => 'Yes' } }, - { category => 'Parks and open spaces', email => 1, code => 'ConfirmPARK', + { category => 'Parks and open spaces', email => ['p1'], code => 'ConfirmPARK', extra => [ { 'name' => 'dangerous', description => 'Was it dangerous?', 'value' => 'Yes' }, { 'name' => 'reportType', description => 'Type of report', 'value' => 'Vandalism' }, ] }, - { category => 'Lamp post', code => 'LAMP', email => 'thirdparty.*another', + { category => 'Lamp post', code => 'LAMP', email => ['thirdparty', 'another'], extra => { 'name' => 'dangerous', description => 'Was it dangerous?', 'value' => 'No' } }, - { category => 'Lamp post', code => 'LAMP', email => 'thirdparty.*another', + { category => 'Lamp post', code => 'LAMP', email => ['thirdparty', 'another'], extra => { 'name' => 'dangerous', description => 'Was it dangerous?', 'value' => 'Yes' } }, - { category => 'Flytipping', code => 'UniformFLY', email => 'EH.*eh@bexley' }, - { category => 'Flooding in the road', code => 'FLOD', email => 'Flooding.*flooding' }, + { category => 'Flytipping', code => 'UniformFLY', email => ['eh'] }, + { category => 'Flooding in the road', code => 'FLOD', email => ['flooding'] }, ) { ($report) = $mech->create_problems_for_body(1, $body->id, 'On Road', { category => $test->{category}, cobrand => 'bexley', @@ -117,11 +121,8 @@ FixMyStreet::override_config { if (my $t = $test->{email}) { my $email = $mech->get_email; - if ($t eq 1) { - like $email->header('To'), qr/"Bexley P1 email".*bexley/; - } else { - like $email->header('To'), qr/$t/; - } + $t = join('@[^@]*', @$t); + like $email->header('To'), qr/^[^@]*$t@[^@]*$/; if ($test->{code} =~ /Confirm/) { like $mech->get_text_body_from_email($email), qr/Site code: Road ID/; } elsif ($test->{code} =~ /Uniform/) { @@ -229,4 +230,29 @@ subtest 'geocoder' => sub { ] }; }; +my $bex = Test::MockModule->new('FixMyStreet::Cobrand::Bexley'); +$bex->mock('get', sub { + return <<EOF +{ + "england-and-wales": { + "events": [ + { "date": "2019-12-25", "title": "Christmas Day", "notes": "", "bunting": true } + ] + } +} +EOF +}); + +subtest 'out of hours' => sub { + my $cobrand = FixMyStreet::Cobrand::Bexley->new; + set_fixed_time('2019-10-16T12:00:00Z'); + is $cobrand->_is_out_of_hours(), 0, 'not out of hours in the day'; + set_fixed_time('2019-10-16T04:00:00Z'); + is $cobrand->_is_out_of_hours(), 1, 'out of hours early in the morning'; + set_fixed_time('2019-10-13T12:00:00Z'); + is $cobrand->_is_out_of_hours(), 1, 'out of hours at weekends'; + set_fixed_time('2019-12-25T12:00:00Z'); + is $cobrand->_is_out_of_hours(), 1, 'out of hours on bank holiday'; +}; + done_testing(); |