aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Arter <davea@mysociety.org>2020-03-26 21:37:16 +0000
committerMatthew Somerville <matthew@mysociety.org>2020-03-31 18:08:07 +0100
commit1bd47700a1fbb08d29651c9d7e5e62e0d2c06401 (patch)
tree5370a9eb07388b2b1822c51b9ec44ff5f7076c83
parent0b94cef3707587a3704c952da8b8642ddbac1712 (diff)
[TfL] Factor borough_email_addresses handler into reusable Role
-rw-r--r--perllib/FixMyStreet/Cobrand/TfL.pm60
-rw-r--r--perllib/FixMyStreet/Roles/BoroughEmails.pm68
2 files changed, 71 insertions, 57 deletions
diff --git a/perllib/FixMyStreet/Cobrand/TfL.pm b/perllib/FixMyStreet/Cobrand/TfL.pm
index 40c2a5257..72d1dbc86 100644
--- a/perllib/FixMyStreet/Cobrand/TfL.pm
+++ b/perllib/FixMyStreet/Cobrand/TfL.pm
@@ -4,6 +4,9 @@ use parent 'FixMyStreet::Cobrand::Whitelabel';
use strict;
use warnings;
+use Moo;
+with 'FixMyStreet::Roles::BoroughEmails';
+
use POSIX qw(strcoll);
use FixMyStreet::MapIt;
@@ -391,63 +394,6 @@ 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;
-}
-
sub report_new_is_on_tlrn {
my ( $self ) = @_;
diff --git a/perllib/FixMyStreet/Roles/BoroughEmails.pm b/perllib/FixMyStreet/Roles/BoroughEmails.pm
new file mode 100644
index 000000000..ba941f64f
--- /dev/null
+++ b/perllib/FixMyStreet/Roles/BoroughEmails.pm
@@ -0,0 +1,68 @@
+package FixMyStreet::Roles::BoroughEmails;
+use Moo::Role;
+
+=head1 NAME
+
+FixMyStreet::Roles::BoroughEmails - role for directing reports according to the
+borough_email_addresses COBRAND_FEATURE
+
+=cut
+
+=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;