diff options
Diffstat (limited to 'perllib/FixMyStreet/Cobrand/Hackney.pm')
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Hackney.pm | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Hackney.pm b/perllib/FixMyStreet/Cobrand/Hackney.pm index f08cd0dd9..dbf070628 100644 --- a/perllib/FixMyStreet/Cobrand/Hackney.pm +++ b/perllib/FixMyStreet/Cobrand/Hackney.pm @@ -3,6 +3,7 @@ use parent 'FixMyStreet::Cobrand::Whitelabel'; use strict; use warnings; +use mySociety::EmailUtil qw(is_valid_email); sub council_area_id { return 2508; } sub council_area { return 'Hackney'; } @@ -33,7 +34,7 @@ sub open311_config { } sub open311_extra_data { - my ($self, $row, $h, $extra) = @_; + my ($self, $row, $h, $extra, $contact) = @_; my $open311_only = [ { name => 'report_url', @@ -46,6 +47,13 @@ sub open311_extra_data { value => $row->category }, ]; + # Make sure contact 'email' set correctly for Open311 + if (my $sent_to = $row->get_extra_metadata('sent_to')) { + $row->unset_extra_metadata('sent_to'); + my $code = $sent_to->{$contact->email}; + $contact->email($code) if $code; + } + return $open311_only; } @@ -80,4 +88,56 @@ sub open311_filter_contacts_for_deletion { }); } +sub lookup_site_code_config { + my ($self, $type) = @_; + my $property_map = { + park => "greenspaces:hackney_park", + estate => "housing:lbh_estate", + }; + { + buffer => 3, # metres + url => "https://map.hackney.gov.uk/geoserver/wfs", + srsname => "urn:ogc:def:crs:EPSG::27700", + typename => $property_map->{$type}, + property => ( $type eq "park" ? "park_id" : "id" ), + accept_feature => sub { 1 }, + accept_types => { Polygon => 1 }, + outputformat => "json", + } +} + +sub get_body_sender { + my ( $self, $body, $problem ) = @_; + + my $contact = $body->contacts->search( { category => $problem->category } )->first; + + my $parts = join '\s*', qw(^ park : (.*?) ; estate : (.*?) ; other : (.*?) $); + my $regex = qr/$parts/i; + if (my ($park, $estate, $other) = $contact->email =~ $regex) { + my $to = $other; + if (my $park_id = $self->lookup_site_code($problem, 'park')) { + $to = $park; + } elsif (my $estate_id = $self->lookup_site_code($problem, 'estate')) { + $to = $estate; + } + $problem->set_extra_metadata(sent_to => { $contact->email => $to }); + if (is_valid_email($to)) { + return { method => 'Email', contact => $contact }; + } + } + return $self->SUPER::get_body_sender($body, $problem); +} + +# Translate email address to actual delivery address +sub munge_sendreport_params { + my ($self, $row, $h, $params) = @_; + + my $sent_to = $row->get_extra_metadata('sent_to') or return; + $row->unset_extra_metadata('sent_to'); + for my $recip (@{$params->{To}}) { + my ($email, $name) = @$recip; + $recip->[0] = $sent_to->{$email} if $sent_to->{$email}; + } +} + 1; |