Skip to content

Commit 8945f83

Browse files
committed
[Enfield] Initial cobrand.
1 parent 061f8ce commit 8945f83

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package FixMyStreet::Cobrand::Enfield;
2+
use parent 'FixMyStreet::Cobrand::UKCouncils';
3+
4+
sub council_area_id { 2495 }
5+
sub council_area { 'Enfield'; }
6+
sub council_name { return 'Enfield Council'; }
7+
sub council_url { return 'enfield'; }
8+
sub base_url { return FixMyStreet->config('BASE_URL'); }
9+
10+
sub open311_config {
11+
my ($self, $row, $h, $params, $contact) = @_;
12+
13+
$params->{multi_photos} = 1;
14+
$params->{upload_files} = 1;
15+
}
16+
17+
sub open311_update_missing_data {
18+
my ($self, $row, $h, $contact) = @_;
19+
20+
if ($row->get_extra_field_value('pac')) {
21+
if (my $result = $self->_lookup_os('uprn', $row)) {
22+
if (my $uprn = $result->{LPI}{UPRN}) {
23+
$row->update_extra_field({ name => 'uprn', description => 'UPRN', value => $uprn });
24+
}
25+
}
26+
} else {
27+
if (my $result = $self->_lookup_os('usrn', $row)) {
28+
if (my $usrn = $result->{LPI}{USRN}) {
29+
$row->update_extra_field({ name => 'usrn', description => 'USRN', value => $usrn });
30+
}
31+
}
32+
}
33+
}
34+
35+
sub open311_extra_data_include {
36+
my ($self, $row, $h) = @_;
37+
38+
my $open311_only = [
39+
{ name => 'report_url',
40+
value => $h->{url} },
41+
{ name => 'title',
42+
value => $row->title },
43+
{ name => 'description',
44+
value => $row->detail },
45+
];
46+
47+
return $open311_only;
48+
}
49+
50+
my $BASE = 'https://api.os.uk/search/places/v1/nearest?dataset=LPI&srs=WGS84&radius=1000';
51+
my $CODES = 'fq=CLASSIFICATION_CODE:LP01+CLASSIFICATION_CODE:LP03+CLASSIFICATION_CODE:LL+CLASSIFICATION_CODE:CC06';
52+
53+
sub _lookup_os {
54+
my ($self, $type, $row) = @_;
55+
if (my $key = $self->feature('os_places_api_key')) {
56+
my $url = "$BASE&key=$key&point=" . $row->latitude . ',' . $row->longitude;
57+
if ($type eq 'uprn') {
58+
$url .= "&$CODES";
59+
}
60+
my $j = FixMyStreet::Geocode::cache('osplaces', $url);
61+
return $j ? $j->{results}[0] : undef;
62+
}
63+
return undef;
64+
}
65+
66+
sub open311_post_send {
67+
my ($self, $row, $h, $sender) = @_;
68+
69+
# Check Open311 was successful
70+
return unless $row->external_id;
71+
return if $row->get_extra_metadata('extra_email_sent');
72+
73+
my $email = $self->feature('open311_email') || return;
74+
75+
$row->push_extra_fields({ name => 'fixmystreet_id', description => 'FMS reference', value => $row->id });
76+
77+
my $sender = FixMyStreet::SendReport::Email->new(
78+
use_verp => 0,
79+
use_replyto => 1,
80+
to => [ [ $email, 'FixMyStreet' ] ],
81+
);
82+
83+
$sender->send($row, $h);
84+
if ($sender->success) {
85+
$row->set_extra_metadata(extra_email_sent => 1);
86+
}
87+
88+
$row->remove_extra_field('fixmystreet_id');
89+
}
90+
91+
1;

perllib/FixMyStreet/TestMech.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ sub create_body_ok {
675675
sub create_problems_for_body {
676676
my ( $mech, $count, $body, $title, $params ) = @_;
677677

678+
$body = $body->id if ref $body;
678679
my $dt = $params->{dt} || DateTime->now();
679680

680681
my $email = $mech->uniquify_email('[email protected]', (caller)[1]);

t/cobrand/enfield.t

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use FixMyStreet::TestMech;
2+
use Test::MockModule;
3+
use CGI::Simple;
4+
use FixMyStreet::Cobrand::Enfield;
5+
use FixMyStreet::Script::Reports;
6+
7+
my $mech = FixMyStreet::TestMech->new;
8+
9+
my $body = $mech->create_body_ok(2495, 'Enfield Council',
10+
{ send_method => 'Open311', cobrand => 'enfield',
11+
api_key => 'key', endpoint => 'endpoint', jurisdiction => 'j', });
12+
13+
$mech->create_contact_ok( body => $body, category => 'Other', email => 'OTHER' );
14+
15+
my $gc = Test::MockModule->new('FixMyStreet::Geocode');
16+
$gc->mock('cache', sub {
17+
my $type = shift;
18+
return {
19+
results => [
20+
{ LPI => {
21+
"UPRN" => "uprn",
22+
"USRN" => "usrn",
23+
} }
24+
],
25+
};
26+
});
27+
28+
FixMyStreet::override_config {
29+
ALLOWED_COBRANDS => [ 'fixmystreet', 'enfield' ],
30+
BASE_URL => 'http://www.fixmystreet.com',
31+
STAGING_FLAGS => {
32+
send_reports => 1,
33+
},
34+
COBRAND_FEATURES => {
35+
open311_email => {
36+
enfield => '[email protected]',
37+
},
38+
os_places_api_key => {
39+
enfield => 'KEY',
40+
},
41+
}
42+
}, sub {
43+
my $enfield = FixMyStreet::Cobrand::Enfield->new;
44+
45+
subtest 'Post normal report' => sub {
46+
my ($p) = $mech->create_problems_for_body(1, $body, 'Title');
47+
48+
FixMyStreet::Script::Reports::send();
49+
50+
$p->discard_changes;
51+
is $p->get_extra_field_value('usrn'), 'usrn';
52+
53+
my $req = Open311->test_req_used;
54+
my $cgi = CGI::Simple->new($req->content);
55+
is $cgi->param('attribute[title]'), $p->title;
56+
is $cgi->param('attribute[usrn]'), 'usrn';
57+
58+
my $email = $mech->get_email;
59+
is $email->header('To'), 'FixMyStreet <[email protected]>';
60+
like $email->as_string, qr/USRN: usrn/;
61+
};
62+
63+
subtest 'Post cemetery report' => sub {
64+
my ($p) = $mech->create_problems_for_body(1, $body, 'Title', {
65+
});
66+
$p->update_extra_field({ name => 'pac', value => 1 });
67+
$p->update;
68+
# email sent post
69+
70+
FixMyStreet::Script::Reports::send();
71+
72+
$p->discard_changes;
73+
is $p->get_extra_field_value('uprn'), 'uprn';
74+
75+
my $req = Open311->test_req_used;
76+
my $cgi = CGI::Simple->new($req->content);
77+
is $cgi->param('attribute[title]'), $p->title;
78+
is $cgi->param('attribute[uprn]'), 'uprn';
79+
};
80+
};
81+
82+
done_testing();

0 commit comments

Comments
 (0)