diff options
author | Dave Arter <davea@mysociety.org> | 2019-11-07 15:08:29 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2019-12-09 12:50:07 +0000 |
commit | ed6a1b7ce9cf30bbc9f26a1e3600b2a4059a97dc (patch) | |
tree | e91877172b0e9772ea3a912059701e8e75e04141 | |
parent | f4157d76048d14711883cc2bba75bef663696aae (diff) |
[TfL] Direct reports to email addresses on a per-borough basis
Adds a new cobrand feature that allows the destination email address
for a report to be determined on its MapIt area IDs, not just the
category it was made in.
-rw-r--r-- | perllib/FixMyStreet/Cobrand/TfL.pm | 57 | ||||
-rw-r--r-- | t/cobrand/tfl.t | 80 |
2 files changed, 135 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/Cobrand/TfL.pm b/perllib/FixMyStreet/Cobrand/TfL.pm index 92fbc1529..cab1e3ada 100644 --- a/perllib/FixMyStreet/Cobrand/TfL.pm +++ b/perllib/FixMyStreet/Cobrand/TfL.pm @@ -276,4 +276,61 @@ sub report_new_munge_before_insert { $report->set_extra_fields(@$extra); } +=head2 munge_sendreport_params + +TfL want reports made in certain categories sent to different email addresses +depending on what London Borough they were made in. To achieve this we have +some config in COBRAND_FEATURES that specifies what address to direct reports +to based on the MapIt area IDs it's in. + +Contacts that use this technique have a short code in their email field, +which is looked up in the `borough_email_addresses` hash. + +For example, if you wanted Pothole reports in Bromley and Barnet to be sent to +one email address, and Pothole reports in Hounslow to be sent to another, +create a contact with category = "Potholes" and email = "BOROUGHPOTHOLES" and +use the following config in general.yml: + +COBRAND_FEATURES: + borough_email_addresses: + tfl: + BOROUGHPOTHOLES: + - email: bromleybarnetpotholes@example.org + areas: + - 2482 # Bromley + - 2489 # Barnet + - email: hounslowpotholes@example.org + areas: + - 2483 # Hounslow + +=cut + +sub munge_sendreport_params { + my ($self, $row, $h, $params) = @_; + + my $addresses = $self->feature('borough_email_addresses'); + return unless $addresses; + + my @report_areas = grep { $_ } split ',', $row->areas; + + my $to = $params->{To}; + my @munged_to = (); + for my $recip ( @$to ) { + my ($email, $name) = @$recip; + if (my $teams = $addresses->{$email}) { + for my $team (@$teams) { + my %team_area_ids = map { $_ => 1 } @{ $team->{areas} }; + if ( grep { $team_area_ids{$_} } @report_areas ) { + $recip = [ + $team->{email}, + $name + ]; + } + } + } + push @munged_to, $recip; + } + $params->{To} = \@munged_to; +} + 1; diff --git a/t/cobrand/tfl.t b/t/cobrand/tfl.t index b8e88a300..abfb924ff 100644 --- a/t/cobrand/tfl.t +++ b/t/cobrand/tfl.t @@ -9,6 +9,10 @@ END { FixMyStreet::App->log->enable('info'); } my $mech = FixMyStreet::TestMech->new; my $body = $mech->create_body_ok(2482, 'TfL'); +FixMyStreet::DB->resultset('BodyArea')->find_or_create({ + area_id => 2483, # Hounslow + body_id => $body->id, +}); my $superuser = $mech->create_user_ok('superuser@example.com', name => 'Super User', is_superuser => 1); my $staffuser = $mech->create_user_ok('counciluser@example.com', name => 'Council User', from_body => $body, password => 'password'); $staffuser->user_body_permissions->create({ @@ -126,13 +130,39 @@ $contact4->set_extra_fields( } ); $contact4->update; +my $contact5 = $mech->create_contact_ok( + body_id => $body->id, + category => 'Trees', + email => 'AOAT', +); +my $contact6 = $mech->create_contact_ok( + body_id => $body->id, + category => 'Grit bins', + email => 'AOAT,gritbins@example.com', +); FixMyStreet::override_config { - ALLOWED_COBRANDS => 'tfl', + ALLOWED_COBRANDS => [ 'tfl', 'bromley', 'fixmystreet'], MAPIT_URL => 'http://mapit.uk/', - COBRAND_FEATURES => { internal_ips => { tfl => [ '127.0.0.1' ] } }, + COBRAND_FEATURES => { + internal_ips => { tfl => [ '127.0.0.1' ] }, + borough_email_addresses => { tfl => { + AOAT => [ + { + email => 'hounslow@example.com', + areas => [ 2483 ], + }, + { + email => 'bromley@example.com', + areas => [ 2482 ], + }, + ], + } }, + }, }, sub { +$mech->host("tfl.fixmystreet.com"); + subtest "test report creation and reference number" => sub { $mech->log_in_ok( $user->email ); $mech->get_ok('/around'); @@ -233,6 +263,52 @@ subtest "change category, report resent to new location" => sub { $mech->log_out_ok; }; +for my $test ( + [ 'BR1 3UH', 'tfl.fixmystreet.com', 'Trees', 'TfL <bromley@example.com>', 'Bromley borough team' ], + [ 'BR1 3UH', 'www.fixmystreet.com', 'Trees', 'TfL <bromley@example.com>', 'Bromley borough team' ], + [ 'BR1 3UH', 'bromley.fixmystreet.com', 'Trees', 'TfL <bromley@example.com>', 'Bromley borough team' ], + [ 'TW7 5JN', 'tfl.fixmystreet.com', 'Trees', 'TfL <hounslow@example.com>', 'Hounslow borough team' ], + [ 'TW7 5JN', 'www.fixmystreet.com', 'Trees', 'TfL <hounslow@example.com>', 'Hounslow borough team' ], + [ 'TW7 5JN', 'tfl.fixmystreet.com', 'Grit bins', 'TfL <hounslow@example.com>, TfL <gritbins@example.com>', 'Hounslow borough team and additional address' ], + [ 'TW7 5JN', 'www.fixmystreet.com', 'Grit bins', 'TfL <hounslow@example.com>, TfL <gritbins@example.com>', 'Hounslow borough team and additional address' ], +) { + my ($postcode, $host, $category, $to, $name ) = @$test; + subtest "test report is sent to $name" => sub { + $mech->host($host); + $mech->log_in_ok( $user->email ); + $mech->get_ok('/around'); + $mech->submit_form_ok( { with_fields => { pc => $postcode, } }, "submit location" ); + $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 for borough team', + detail => 'Test report details.', + may_show_name => '1', + category => $category, + $host eq 'bromley.fixmystreet.com' ? ( + fms_extra_title => 'DR', + first_name => "Joe", + last_name => "Bloggs", + ) : ( + name => 'Joe Bloggs', + ), + } + }, + "submit good details" + ); + + $mech->clear_emails_ok; + FixMyStreet::Script::Reports::send(); + my @email = $mech->get_email; + is $email[0]->header('To'), $to, 'Sent to correct address'; + $mech->clear_emails_ok; + FixMyStreet::DB->resultset("Problem")->find({ title => 'Test Report for borough team'})->delete; + }; +} + +$mech->host("tfl.fixmystreet.com"); + subtest 'check lookup by reference' => sub { my $id = FixMyStreet::DB->resultset("Problem")->first->id; |