diff options
-rwxr-xr-x | bin/fixmystreet.com/buckinghamshire-flytipping | 80 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 24 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bromley.pm | 39 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Buckinghamshire.pm | 52 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Warwickshire.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm | 44 | ||||
-rw-r--r-- | perllib/Open311/PopulateServiceList.pm | 58 | ||||
-rw-r--r-- | t/cobrand/bucks.t | 27 | ||||
-rw-r--r-- | t/cobrand/warwickshire.t | 84 | ||||
-rw-r--r-- | t/open311/populate-service-list.t | 151 | ||||
-rw-r--r-- | web/cobrands/buckinghamshire/assets.js | 28 |
11 files changed, 472 insertions, 123 deletions
diff --git a/bin/fixmystreet.com/buckinghamshire-flytipping b/bin/fixmystreet.com/buckinghamshire-flytipping new file mode 100755 index 000000000..72e8b7e7b --- /dev/null +++ b/bin/fixmystreet.com/buckinghamshire-flytipping @@ -0,0 +1,80 @@ +#!/usr/bin/env perl +# +# If a district flytipping report within Buckinghamshire has not been closed +# after three weeks, close it with a message. If it's older than six weeks, +# use a different message and suppress any alerts. + +use v5.14; +use warnings; + +BEGIN { + use File::Basename qw(dirname); + use File::Spec; + my $d = dirname(File::Spec->rel2abs($0)); + require "$d/../../setenv.pl"; +} + +use constant BUCKS_AREA_ID => 2217; +use constant DISTRICT_IDS => (2255, 2256, 2257, 2258); +use constant TIME_OPEN => '3 weeks'; +use constant TIME_OPEN_ALERT => '6 weeks'; + +use FixMyStreet::DB; +use FixMyStreet::Script::ArchiveOldEnquiries; +use Getopt::Long::Descriptive; + +my ($opts, $usage) = describe_options( + '%c %o', + ['commit|c', "actually close reports and send emails. Omitting this flag will do a dry-run"], + ['help|h', "print usage message and exit" ], +); +print($usage->text), exit if $opts->help; + +my $body = FixMyStreet::DB->resultset("Body")->for_areas(BUCKS_AREA_ID)->first; +die "Could not find Bucks body" unless $body; + +my @districts = FixMyStreet::DB->resultset("Body")->for_areas(DISTRICT_IDS)->all; +my @district_ids = map { $_->id } @districts; +die "Did not find all districts" unless @district_ids == 4; + +find_problems(TIME_OPEN_ALERT, TIME_OPEN, 'Auto-closure', 1); +find_problems(undef, TIME_OPEN_ALERT, 'Auto-closure (old)', 0); + +sub find_problems { + my ($from, $to, $title, $retain_alerts) = @_; + + my $template = FixMyStreet::DB->resultset("ResponseTemplate")->search({ + body_id => $body->id, title => $title, + })->first; + die "Could not find Bucks Flytipping template" unless $template; + + $to = "current_timestamp - '$to'::interval"; + my $time_param; + if ($from) { + $from = "current_timestamp - '$from'::interval"; + $time_param = [ -and => { '>=', \$from }, { '<', \$to } ], + } else { + $time_param = { '<', \$to }; + } + + # Fetch all Flytipping problems sent only to districts, between $from and $to + my $q = FixMyStreet::DB->resultset("Problem")->search( + \[ "? @> regexp_split_to_array(bodies_str, ',')", [ {} => \@district_ids ] ] + )->search({ + state => [ FixMyStreet::DB::Result::Problem->open_states() ], + category => 'Flytipping', + confirmed => $time_param, + }); + + # Provide some variables to the archiving script + FixMyStreet::Script::ArchiveOldEnquiries::update_options({ + user => $body->comment_user, + user_name => $body->comment_user->name, + closure_text => $template->text, + retain_alerts => $retain_alerts, + commit => $opts->commit, + }); + + # Close the reports + FixMyStreet::Script::ArchiveOldEnquiries::close_problems($q); +} diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index a23a9046f..e6c4fe50d 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -1069,26 +1069,12 @@ sub set_report_extras : Private { my ($self, $c, $contacts, $param_prefix) = @_; $param_prefix ||= ""; - my @extra; - foreach my $contact (@$contacts) { - my $metas = $contact->get_metadata_for_input; - foreach my $field ( @$metas ) { - if ( lc( $field->{required} ) eq 'true' && !$c->cobrand->category_extra_hidden($field)) { - unless ( $c->get_param($param_prefix . $field->{code}) ) { - $c->stash->{field_errors}->{ $field->{code} } = _('This information is required'); - } - } - push @extra, { - name => $field->{code}, - description => $field->{description}, - value => $c->get_param($param_prefix . $field->{code}) || '', - }; - } - } + my @metalist = map { [ $_->get_metadata_for_input, $param_prefix ] } @$contacts; + push @metalist, map { [ $_->get_extra_fields, "extra[" . $_->id . "]" ] } @{$c->stash->{report_extra_fields}}; - foreach my $extra_fields (@{ $c->stash->{report_extra_fields} }) { - my $metas = $extra_fields->get_extra_fields; - $param_prefix = "extra[" . $extra_fields->id . "]"; + my @extra; + foreach my $item (@metalist) { + my ($metas, $param_prefix) = @$item; foreach my $field ( @$metas ) { if ( lc( $field->{required} ) eq 'true' && !$c->cobrand->category_extra_hidden($field)) { unless ( $c->get_param($param_prefix . $field->{code}) ) { diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 11c685c91..386e1a269 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -196,5 +196,44 @@ sub open311_pre_send { } } +sub open311_contact_meta_override { + my ($self, $service, $contact, $meta) = @_; + + $contact->set_extra_metadata( id_field => 'service_request_id_ext'); + + # Lights we want to store feature ID, PROW on all categories. + push @$meta, { + code => 'prow_reference', + datatype => 'string', + description => 'Right of way reference', + order => 101, + required => 'false', + variable => 'true', + automated => 'hidden_field', + }; + push @$meta, { + code => 'feature_id', + datatype => 'string', + description => 'Feature ID', + order => 100, + required => 'false', + variable => 'true', + automated => 'hidden_field', + } if $service->{service_code} eq 'SLRS'; + + my @override = qw( + requested_datetime + report_url + title + last_name + email + report_title + public_anonymity_required + email_alerts_requested + ); + my %ignore = map { $_ => 1 } @override; + @$meta = grep { !$ignore{$_->{code}} } @$meta; +} + 1; diff --git a/perllib/FixMyStreet/Cobrand/Buckinghamshire.pm b/perllib/FixMyStreet/Cobrand/Buckinghamshire.pm index c8432c4d7..75564fddd 100644 --- a/perllib/FixMyStreet/Cobrand/Buckinghamshire.pm +++ b/perllib/FixMyStreet/Cobrand/Buckinghamshire.pm @@ -109,6 +109,58 @@ sub open311_config_updates { $params->{mark_reopen} = 1; } +sub open311_contact_meta_override { + my ($self, $service, $contact, $meta) = @_; + + push @$meta, { + code => 'road-placement', + datatype => 'singlevaluelist', + description => 'Is the fly-tip located on', + order => 100, + required => 'true', + variable => 'true', + values => [ + { key => 'road', name => 'The road' }, + { key => 'off-road', name => 'Off the road/on a verge' }, + ], + } if $service->{service_name} eq 'Flytipping'; +} + +sub process_open311_extras { + my ($self, $c, $body, $extra) = @_; + + $self->flytipping_body_fix( + $c->stash->{report}, + $c->get_param('road-placement'), + $c->stash->{field_errors}, + ); +} + +sub flytipping_body_fix { + my ($self, $report, $road_placement, $errors) = @_; + + return unless $report->category eq 'Flytipping'; + + if ($report->bodies_str =~ /,/) { + # Sent to both councils in the area + my @bodies = values %{$report->bodies}; + my $county = (grep { $_->name =~ /^Buckinghamshire/ } @bodies)[0]; + my $district = (grep { $_->name !~ /^Buckinghamshire/ } @bodies)[0]; + # Decide which to send to based upon the answer to the extra question: + if ($road_placement eq 'road') { + $report->bodies_str($county->id); + } elsif ($road_placement eq 'off-road') { + $report->bodies_str($district->id); + } + } else { + # If the report is only being sent to the district, we do + # not care about the road question, if it is missing + if (!$report->to_body_named('Buckinghamshire')) { + delete $errors->{'road-placement'}; + } + } +} + sub filter_report_description { my ($self, $description) = @_; diff --git a/perllib/FixMyStreet/Cobrand/Warwickshire.pm b/perllib/FixMyStreet/Cobrand/Warwickshire.pm index 73f66f3da..c301450bc 100644 --- a/perllib/FixMyStreet/Cobrand/Warwickshire.pm +++ b/perllib/FixMyStreet/Cobrand/Warwickshire.pm @@ -34,4 +34,12 @@ sub contact_name { 'Warwickshire County Council (do not reply)'; } sub send_questionnaires { 0 } +sub open311_contact_meta_override { + my ($self, $service, $contact, $meta) = @_; + + $contact->set_extra_metadata( id_field => 'external_id'); + + @$meta = grep { $_->{code} ne 'closest_address' } @$meta; +} + 1; diff --git a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm index 03bc511a0..dd44b9651 100644 --- a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm +++ b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm @@ -1,11 +1,8 @@ package FixMyStreet::Script::ArchiveOldEnquiries; -use strict; +use v5.14; use warnings; -require 5.8.0; -use FixMyStreet; -use FixMyStreet::App; use FixMyStreet::DB; use FixMyStreet::Cobrand; use FixMyStreet::Map; @@ -17,17 +14,17 @@ my $opts = { }; sub query { - return { - bodies_str => { 'LIKE', "%".$opts->{body}."%"}, - -and => [ + my $rs = shift; + return $rs->to_body($opts->{body})->search({ + -and => [ lastupdate => { '<', $opts->{email_cutoff} }, lastupdate => { '>', $opts->{closure_cutoff} }, ], - state => [ FixMyStreet::DB::Result::Problem->open_states() ], - }; + state => [ FixMyStreet::DB::Result::Problem->open_states() ], + }); } -sub archive { +sub update_options { my $params = shift; if ( $params ) { $opts = { @@ -35,13 +32,19 @@ sub archive { %$params, }; } +} + +sub archive { + my $params = shift; + update_options($params); unless ( $opts->{commit} ) { printf "Doing a dry run; emails won't be sent and reports won't be closed.\n"; printf "Re-run with --commit to actually archive reports.\n\n"; } - my @user_ids = FixMyStreet::DB->resultset('Problem')->search(query(), + my $rs = FixMyStreet::DB->resultset('Problem'); + my @user_ids = query($rs)->search(undef, { distinct => 1, columns => ['user_id'], @@ -55,7 +58,7 @@ sub archive { }); my $user_count = $users->count; - my $problem_count = FixMyStreet::DB->resultset('Problem')->search(query(), + my $problem_count = query($rs)->search(undef, { columns => ['id'], rows => $opts->{limit}, @@ -71,8 +74,7 @@ sub archive { } } - my $problems_to_close = FixMyStreet::DB->resultset('Problem')->search({ - bodies_str => { 'LIKE', "%".$opts->{body}."%"}, + my $problems_to_close = $rs->to_body($opts->{body})->search({ lastupdate => { '<', $opts->{closure_cutoff} }, state => [ FixMyStreet::DB::Result::Problem->open_states() ], }, { @@ -87,7 +89,8 @@ sub archive { sub send_email_and_close { my ($user) = @_; - my $problems = $user->problems->search(query(), { + my $problems = $user->problems; + $problems = query($problems)->search(undef, { order_by => { -desc => 'confirmed' }, }); @@ -135,22 +138,27 @@ sub close_problems { return unless $opts->{commit}; my $problems = shift; + my $extra = { auto_closed_by_script => 1 }; + $extra->{is_superuser} = 1 if !$opts->{user_name}; + while (my $problem = $problems->next) { my $timestamp = \'current_timestamp'; my $comment = $problem->add_to_comments( { - text => '', + text => $opts->{closure_text} || '', created => $timestamp, confirmed => $timestamp, user_id => $opts->{user}, - name => _('an administrator'), + name => $opts->{user_name} || _('an administrator'), mark_fixed => 0, anonymous => 0, state => 'confirmed', problem_state => 'closed', - extra => { is_superuser => 1 }, + extra => $extra, } ); $problem->update({ state => 'closed', send_questionnaire => 0 }); + next if $opts->{retain_alerts}; + # Stop any alerts being sent out about this closure. my @alerts = FixMyStreet::DB->resultset('Alert')->search( { alert_type => 'new_updates', diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm index 0bbd902ed..d506111ec 100644 --- a/perllib/Open311/PopulateServiceList.pm +++ b/perllib/Open311/PopulateServiceList.pm @@ -233,65 +233,15 @@ sub _add_meta_to_contact { # Some Open311 endpoints, such as Bromley and Warwickshire send <metadata> # for attributes which we *don't* want to display to the user (e.g. as - # fields in "category_extras" - $self->_add_meta_to_contact_cobrand_overrides($contact, \@meta); + # fields in "category_extras"), or need additional attributes adding not + # returned by the server for whatever reason. + $self->_current_body_cobrand && $self->_current_body_cobrand->call_hook( + open311_contact_meta_override => $self->_current_service, $contact, \@meta); $contact->set_extra_fields(@meta); $contact->update; } -sub _add_meta_to_contact_cobrand_overrides { - my ( $self, $contact, $meta ) = @_; - - if ($self->_current_body->name eq 'Bromley Council') { - $contact->set_extra_metadata( id_field => 'service_request_id_ext'); - # Lights we want to store feature ID, PROW on all categories. - push @$meta, { - code => 'prow_reference', - datatype => 'string', - description => 'Right of way reference', - order => 101, - required => 'false', - variable => 'true', - automated => 'hidden_field', - }; - push @$meta, { - code => 'feature_id', - datatype => 'string', - description => 'Feature ID', - order => 100, - required => 'false', - variable => 'true', - automated => 'hidden_field', - } if $self->_current_service->{service_code} eq 'SLRS'; - } elsif ($self->_current_body->name eq 'Warwickshire County Council') { - $contact->set_extra_metadata( id_field => 'external_id'); - } - - my %override = ( - #2482 - 'Bromley Council' => [qw( - requested_datetime - report_url - title - last_name - email - report_title - public_anonymity_required - email_alerts_requested - ) ], - #2243, - 'Warwickshire County Council' => [qw( - closest_address - ) ], - ); - - if (my $override = $override{ $self->_current_body->name }) { - my %ignore = map { $_ => 1 } @{ $override }; - @$meta = grep { ! $ignore{ $_->{ code } } } @$meta; - } -} - sub _normalize_service_name { my $self = shift; diff --git a/t/cobrand/bucks.t b/t/cobrand/bucks.t index a894bd377..d9273fbf8 100644 --- a/t/cobrand/bucks.t +++ b/t/cobrand/bucks.t @@ -80,10 +80,31 @@ subtest 'flytipping off road sent to extra email' => sub { is $report->external_id, undef, 'Report has right external ID'; }; -}; - $cobrand = FixMyStreet::Cobrand::Buckinghamshire->new(); +subtest 'Flytipping extra question used if necessary' => sub { + my $errors = { 'road-placement' => 'This field is required' }; + + $report->update({ bodies_str => $body->id }); + $cobrand->flytipping_body_fix($report, 'road', $errors); + is $errors->{'road-placement'}, 'This field is required', 'Error stays if sent to county'; + + $report->update({ bodies_str => $district->id }); + $report->discard_changes; # As e.g. ->bodies has been remembered. + $cobrand->flytipping_body_fix($report, 'road', $errors); + is $errors->{'road-placement'}, undef, 'Error removed if sent to district'; + + $report->update({ bodies_str => $body->id . ',' . $district->id }); + $report->discard_changes; # As e.g. ->bodies has been remembered. + $cobrand->flytipping_body_fix($report, 'road', $errors); + is $report->bodies_str, $body->id, 'Sent to both becomes sent to county on-road'; + + $report->update({ bodies_str => $district->id . ',' . $body->id }); + $report->discard_changes; # As e.g. ->bodies has been remembered. + $cobrand->flytipping_body_fix($report, 'off-road', $errors); + is $report->bodies_str, $district->id, 'Sent to both becomes sent to district off-road'; +}; + for my $test ( { desc => 'filters basic emails', @@ -151,6 +172,6 @@ for my $test ( }; } - +}; done_testing(); diff --git a/t/cobrand/warwickshire.t b/t/cobrand/warwickshire.t new file mode 100644 index 000000000..79c9f31e0 --- /dev/null +++ b/t/cobrand/warwickshire.t @@ -0,0 +1,84 @@ +#!/usr/bin/env perl + +use FixMyStreet::Test; +use FixMyStreet::DB; + +use_ok( 'Open311::PopulateServiceList' ); +use_ok( 'Open311' ); + +my $processor = Open311::PopulateServiceList->new(); +ok $processor, 'created object'; + +my $warks = FixMyStreet::DB->resultset('Body')->create({ + name => 'Warwickshire County Council', +}); +$warks->body_areas->create({ area_id => 2243 }); + +subtest 'check Warwickshire override' => sub { + my $processor = Open311::PopulateServiceList->new(); + + my $meta_xml = '<?xml version="1.0" encoding="utf-8"?> +<service_definition> + <service_code>100</service_code> + <attributes> + <attribute> + <variable>true</variable> + <code>closest_address</code> + <datatype>string</datatype> + <required>true</required> + <order>1</order> + <description>Closest address</description> + </attribute> + <attribute> + <variable>true</variable> + <code>size</code> + <datatype>string</datatype> + <required>true</required> + <order>2</order> + <description>How big is the pothole</description> + </attribute> + </attributes> +</service_definition> + '; + + my $contact = FixMyStreet::DB->resultset('Contact')->create({ + body_id => $warks->id, + email => '100', + category => 'Pothole', + state => 'confirmed', + editor => $0, + whenedited => \'current_timestamp', + note => 'test contact', + }); + + my $o = Open311->new( + jurisdiction => 'mysociety', + endpoint => 'http://example.com', + test_mode => 1, + test_get_returns => { 'services/100.xml' => $meta_xml } + ); + + $processor->_current_open311( $o ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'warwickshire' ], + }, sub { + $processor->_current_body( $warks ); + }; + $processor->_current_service( { service_code => 100, service_name => 'Pothole' } ); + $processor->_add_meta_to_contact( $contact ); + + my $extra = [ { + variable => 'true', + code => 'size', + datatype => 'string', + required => 'true', + order => 2, + description => 'How big is the pothole', + } ]; + + $contact->discard_changes; + is_deeply $contact->get_extra_fields, $extra, 'No closest_address field returned for Warks'; + is $contact->get_extra_metadata('id_field'), 'external_id', 'id_field set correctly'; +}; + +done_testing(); diff --git a/t/open311/populate-service-list.t b/t/open311/populate-service-list.t index 1415c7b2a..9747dfd2f 100644 --- a/t/open311/populate-service-list.t +++ b/t/open311/populate-service-list.t @@ -27,24 +27,28 @@ use_ok( 'Open311' ); my $processor = Open311::PopulateServiceList->new(); ok $processor, 'created object'; -my $body = FixMyStreet::DB->resultset('Body')->find_or_create( { - id => 1, +my $body = FixMyStreet::DB->resultset('Body')->create({ name => 'Body Numero Uno', } ); -$body->body_areas->find_or_create({ +$body->body_areas->create({ area_id => 1 } ); my $BROMLEY = 'Bromley Council'; -my $bromley = FixMyStreet::DB->resultset('Body')->find_or_create( { - id => 2482, +my $bromley = FixMyStreet::DB->resultset('Body')->create( { name => $BROMLEY, } ); -$bromley->update({ name => $BROMLEY }); -$bromley->body_areas->find_or_create({ +$bromley->body_areas->create({ area_id => 2482 } ); +my $bucks = FixMyStreet::DB->resultset('Body')->create({ + name => 'Buckinghamshire County Council', +}); +$bucks->body_areas->create({ + area_id => 2217 +}); + for my $test ( { desc => 'groups not set for new contacts', cobrand => 'tester', groups => 0, delete => 1 }, { desc => 'groups set for new contacts', cobrand => 'testergroups', groups => 1, delete => 1}, @@ -55,7 +59,7 @@ for my $test ( ALLOWED_COBRANDS => [ $test->{cobrand} ], }, sub { subtest 'check basic functionality, ' . $test->{desc} => sub { - FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete() if $test->{delete}; + FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->delete() if $test->{delete}; my $service_list = get_xml_simple_object( get_standard_xml() ); @@ -63,7 +67,7 @@ for my $test ( $processor->_current_body( $body ); $processor->process_services( $service_list ); - my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->count(); is $contact_count, 3, 'correct number of contacts'; for my $expects ( @@ -71,7 +75,7 @@ for my $test ( { code => "002", group => $test->{groups} ? "street" : undef }, { code => "003", group => $test->{groups} ? "street" : undef }, ) { - my $contact = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1, email => $expects->{code} } )->first; + my $contact = FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id, email => $expects->{code} } )->first; is $contact->get_extra->{group}, $expects->{group}, "Group set correctly"; } }; @@ -79,11 +83,11 @@ for my $test ( } subtest 'check non open311 contacts marked as deleted' => sub { - FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->delete(); my $contact = FixMyStreet::DB->resultset('Contact')->create( { - body_id => 1, + body_id => $body->id, email => 'contact@example.com', category => 'An old category', state => 'confirmed', @@ -99,19 +103,19 @@ subtest 'check non open311 contacts marked as deleted' => sub { $processor->_current_body( $body ); $processor->process_services( $service_list ); - my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->count(); is $contact_count, 4, 'correct number of contacts'; - $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1, state => 'deleted' } )->count(); + $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id, state => 'deleted' } )->count(); is $contact_count, 1, 'correct number of deleted contacts'; }; subtest 'check email changed if matching category' => sub { - FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->delete(); my $contact = FixMyStreet::DB->resultset('Contact')->create( { - body_id => 1, + body_id => $body->id, email => '009', category => 'Cans left out 24x7', state => 'confirmed', @@ -133,16 +137,16 @@ subtest 'check email changed if matching category' => sub { is $contact->email, '001', 'email unchanged'; is $contact->state, 'confirmed', 'contact still confirmed'; - my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->count(); is $contact_count, 3, 'correct number of contacts'; }; subtest 'check category name changed if updated' => sub { - FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->delete(); my $contact = FixMyStreet::DB->resultset('Contact')->create( { - body_id => 1, + body_id => $body->id, email => '001', category => 'Bins left out 24x7', state => 'confirmed', @@ -165,16 +169,16 @@ subtest 'check category name changed if updated' => sub { is $contact->category, 'Cans left out 24x7', 'category changed'; is $contact->state, 'confirmed', 'contact still confirmed'; - my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->count(); is $contact_count, 3, 'correct number of contacts'; }; subtest 'check conflicting contacts not changed' => sub { - FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->delete(); my $contact = FixMyStreet::DB->resultset('Contact')->create( { - body_id => 1, + body_id => $body->id, email => 'existing@example.com', category => 'Cans left out 24x7', state => 'confirmed', @@ -188,7 +192,7 @@ subtest 'check conflicting contacts not changed' => sub { my $contact2 = FixMyStreet::DB->resultset('Contact')->create( { - body_id => 1, + body_id => $body->id, email => '001', category => 'Bins left out 24x7', state => 'confirmed', @@ -216,7 +220,7 @@ subtest 'check conflicting contacts not changed' => sub { is $contact2->category, 'Bins left out 24x7', 'second contact category unchanged'; is $contact2->state, 'confirmed', 'second contact still confirmed'; - my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => $body->id } )->count(); is $contact_count, 4, 'correct number of contacts'; }; @@ -360,7 +364,7 @@ for my $test ( my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { - body_id => 1, + body_id => $body->id, email => '100', category => 'Cans left out 24x7', state => 'confirmed', @@ -433,7 +437,7 @@ subtest 'check attribute ordering' => sub { my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { - body_id => 1, + body_id => $body->id, email => '001', category => 'Bins left out 24x7', state => 'confirmed', @@ -534,7 +538,7 @@ subtest 'check Bromley skip code' => sub { my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { - body_id => 1, + body_id => $body->id, email => '001', category => 'Bins left out 24x7', state => 'confirmed', @@ -552,7 +556,11 @@ subtest 'check Bromley skip code' => sub { ); $processor->_current_open311( $o ); - $processor->_current_body( $bromley ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'bromley' ], + }, sub { + $processor->_current_body( $bromley ); + }; $processor->_current_service( { service_code => 100 } ); $processor->_add_meta_to_contact( $contact ); @@ -620,6 +628,93 @@ subtest 'check Bromley skip code' => sub { is_deeply $contact->get_extra_fields, $extra, 'all meta data saved for non bromley'; }; +subtest 'check Buckinghamshire extra code' => sub { + my $processor = Open311::PopulateServiceList->new(); + + my $meta_xml = '<?xml version="1.0" encoding="utf-8"?> +<service_definition> + <service_code>100</service_code> + <attributes> + <attribute> + <variable>true</variable> + <code>type</code> + <datatype>string</datatype> + <required>true</required> + <datatype_description>Type of bin</datatype_description> + <order>1</order> + <description>Type of bin</description> + </attribute> + </attributes> +</service_definition> + '; + + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create({ + body_id => $body->id, + email => '001', + category => 'Flytipping', + state => 'confirmed', + editor => $0, + whenedited => \'current_timestamp', + note => 'test contact', + }); + + my $o = Open311->new( + jurisdiction => 'mysociety', + endpoint => 'http://example.com', + test_mode => 1, + test_get_returns => { 'services/100.xml' => $meta_xml } + ); + + $processor->_current_open311( $o ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'buckinghamshire' ], + }, sub { + $processor->_current_body( $bucks ); + }; + $processor->_current_service( { service_code => 100, service_name => 'Flytipping' } ); + $processor->_add_meta_to_contact( $contact ); + + my $extra = [ { + variable => 'true', + code => 'type', + datatype => 'string', + required => 'true', + datatype_description => 'Type of bin', + order => 1, + description => 'Type of bin' + }, { + variable => 'true', + code => 'road-placement', + datatype => 'singlevaluelist', + required => 'true', + order => 100, + description => 'Is the fly-tip located on', + values => [ + { key => 'road', name => 'The road' }, + { key => 'off-road', name => 'Off the road/on a verge' }, + ], + } ]; + + $contact->discard_changes; + is_deeply $contact->get_extra_fields, $extra, 'extra Bucks field returned for flytipping'; + + $processor->_current_service( { service_code => 100, service_name => 'Street lights' } ); + $processor->_add_meta_to_contact( $contact ); + + $extra = [ { + variable => 'true', + code => 'type', + datatype => 'string', + required => 'true', + datatype_description => 'Type of bin', + order => 1, + description => 'Type of bin' + } ]; + + $contact->discard_changes; + is_deeply $contact->get_extra_fields, $extra, 'no extra Bucks field returned otherwise'; +}; + sub get_standard_xml { return qq{<?xml version="1.0" encoding="utf-8"?> <services> diff --git a/web/cobrands/buckinghamshire/assets.js b/web/cobrands/buckinghamshire/assets.js index 32c551d1a..2f69ba6f6 100644 --- a/web/cobrands/buckinghamshire/assets.js +++ b/web/cobrands/buckinghamshire/assets.js @@ -377,6 +377,11 @@ fixmystreet.assets.add($.extend(true, {}, defaults, { found: function(layer, feature) { fixmystreet.body_overrides.allow_send(layer.fixmystreet.body); fixmystreet.body_overrides.remove_only_send(); + + // Make sure Flytipping related things reset + $('#category_meta').show(); + $('#form_road-placement').attr('required', ''); + if (fixmystreet.assets.selectedFeature()) { hide_responsibility_errors(); enable_report_form(); @@ -414,10 +419,19 @@ fixmystreet.assets.add($.extend(true, {}, defaults, { fixmystreet.body_overrides.allow_send(layer.fixmystreet.body); hide_responsibility_errors(); enable_report_form(); - } else if (is_only_body(layer.fixmystreet.body)){ + } else if (is_only_body(layer.fixmystreet.body)) { show_responsibility_error("#js-not-a-road"); disable_report_form(); } + + // If flytipping is picked, we don't want to ask the extra question + var cat = $('select#form_category').val(); + if (cat === 'Flytipping') { + $('#category_meta').hide(); + $('#form_road-placement').removeAttr('required'); + } else { + $('#category_meta').show(); + } } }, usrn: { @@ -428,6 +442,18 @@ fixmystreet.assets.add($.extend(true, {}, defaults, { filter_value: types_to_show, })); +// As with the road found/not_found above, we want to change the destination +// depending upon the answer to the extra question shown when on a road +$("#problem_form").on("change", "#form_road-placement", function() { + if (this.value == 'road') { + fixmystreet.body_overrides.allow_send(defaults.body); + fixmystreet.body_overrides.only_send(defaults.body); + } else if (this.value == 'off-road') { + fixmystreet.body_overrides.do_not_send(defaults.body); + fixmystreet.body_overrides.remove_only_send(); + } +}); + fixmystreet.assets.add($.extend(true, {}, defaults, { http_options: { params: { |