Skip to content

Commit

Permalink
[Waste] Make sure server receives sorted list.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Oct 11, 2023
1 parent 65f1b39 commit 95ce157
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
29 changes: 27 additions & 2 deletions perllib/FixMyStreet/App/Controller/Waste/Bulky.pm
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ sub cancel_small : PathPart('cancel') : Chained('setup_small') : Args(1) {

sub process_bulky_data : Private {
my ($self, $c, $form) = @_;
my $data = $form->saved_data;
my $data = renumber_items($form->saved_data, $c->cobrand->bulky_items_maximum);

Check warning on line 246 in perllib/FixMyStreet/App/Controller/Waste/Bulky.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Waste/Bulky.pm#L246

Added line #L246 was not covered by tests

$c->cobrand->call_hook("waste_munge_bulky_data", $data);

Expand Down Expand Up @@ -283,9 +283,34 @@ sub process_bulky_data : Private {
return 1;
}

=head2 renumber_items
This function is used to make sure that the incoming item data uses 1, 2, 3,
... in case the user had deleted a middle item and sent us 1, 3, 4, 6, ...
=cut

sub renumber_items {
my ($data, $max) = @_;

Check warning on line 294 in perllib/FixMyStreet/App/Controller/Waste/Bulky.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Waste/Bulky.pm#L294

Added line #L294 was not covered by tests

my $c = 1;
my %items;
for (1..$max) {

Check warning on line 298 in perllib/FixMyStreet/App/Controller/Waste/Bulky.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Waste/Bulky.pm#L296-L298

Added lines #L296 - L298 were not covered by tests
next unless $data->{"item_$_"};
$items{"item_$c"} = $data->{"item_$_"};
$items{"item_notes_$c"} = $data->{"item_notes_$_"};
$items{"item_photo_$c"} = $data->{"item_photo_$_"};
$c++;

Check warning on line 303 in perllib/FixMyStreet/App/Controller/Waste/Bulky.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Waste/Bulky.pm#L300-L303

Added lines #L300 - L303 were not covered by tests
}
my $data_itemless = { map { $_ => $data->{$_} } grep { !/^item_(notes_|photo_)?\d/ } keys %$data };
$data = { %$data_itemless, %items };

Check warning on line 306 in perllib/FixMyStreet/App/Controller/Waste/Bulky.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Waste/Bulky.pm#L305-L306

Added lines #L305 - L306 were not covered by tests

return $data;

Check warning on line 308 in perllib/FixMyStreet/App/Controller/Waste/Bulky.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Waste/Bulky.pm#L308

Added line #L308 was not covered by tests
}

sub process_bulky_amend : Private {
my ($self, $c, $form) = @_;
my $data = $form->saved_data;
my $data = renumber_items($form->saved_data, $c->cobrand->bulky_items_maximum);

Check warning on line 313 in perllib/FixMyStreet/App/Controller/Waste/Bulky.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Waste/Bulky.pm#L313

Added line #L313 was not covered by tests

$c->stash->{override_confirmation_template} = 'waste/bulky/confirmation.html';

Expand Down
4 changes: 2 additions & 2 deletions t/app/controller/waste_brent_small_items.t
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ FixMyStreet::override_config {
'item_1' => 'Tied bag of domestic batteries (min 10 - max 100)',
'item_2' => 'Toaster',
'item_3' => 'Podback Bag',
'item_4' => 'Small electricals: Other item under 30x30x30 cm',
'item_notes_4' => 'A widget',
'item_5' => 'Small electricals: Other item under 30x30x30 cm',
'item_notes_5' => 'A widget',
},
},
);
Expand Down

0 comments on commit 95ce157

Please sign in to comment.