Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 45 additions & 47 deletions lib/Bracket/Service/BracketValidator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,22 @@ sub validate_region_payload {
return { ok => 0, errors => $parse_errors } if @{$parse_errors};

my $region_game_ids = _region_game_ids_for_region($schema, $region_id);

my @errors;
my %existing = map { $_->game->id => $_->pick->id }
$schema->resultset('Pick')->search({ player => $player_id })->all;
my %effective = (%existing, %{$pick_map});

my @changed_games;
foreach my $game_id (sort { $a <=> $b } keys %{$pick_map}) {
if (!$region_game_ids->{$game_id}) {
push @errors, "Game ${game_id} is outside region ${region_id}";
next;
}
push @changed_games, $game_id;
}

my $games_to_validate = _affected_games_in_scope(
$schema,
\@changed_games,
sub {
my @errors = _validate_scoped_payload(
schema => $schema,
player_id => $player_id,
pick_map => $pick_map,
validation_region_id => $region_id,
is_in_scope => sub {
my ($game_id) = @_;
return $region_game_ids->{$game_id};
}
},
out_of_scope_error => sub {
my ($game_id) = @_;
return "Game ${game_id} is outside region ${region_id}";
},
);

foreach my $game_id (sort { $a <=> $b } keys %{$games_to_validate}) {
next if !exists $effective{$game_id};

my $team_id = $effective{$game_id};
push @errors, _validate_pick_for_game($schema, $game_id, $team_id, $region_id, \%effective);
}

@errors = grep { defined $_ && $_ ne '' } @errors;
return @errors
? { ok => 0, errors => \@errors }
: { ok => 1, normalized_picks => $pick_map };
return @errors ? { ok => 0, errors => \@errors } : { ok => 1, normalized_picks => $pick_map };
}

sub validate_final4_payload {
Expand All @@ -67,40 +48,57 @@ sub validate_final4_payload {
}

my %allowed_games = map { $_ => 1 } @{$final4_game_ids};
my @errors = _validate_scoped_payload(
schema => $schema,
player_id => $player_id,
pick_map => $pick_map,
validation_region_id => undef,
is_in_scope => sub {
my ($game_id) = @_;
return $allowed_games{$game_id};
},
out_of_scope_error => sub {
my ($game_id) = @_;
return "Game ${game_id} is not a Final Four game";
},
);

return @errors ? { ok => 0, errors => \@errors } : { ok => 1, normalized_picks => $pick_map };
}

sub _validate_scoped_payload {
my (%args) = @_;

my $schema = $args{schema};
my $player_id = $args{player_id};
my $pick_map = $args{pick_map} || {};
my $validation_region_id = $args{validation_region_id};
my $is_in_scope = $args{is_in_scope} || sub { return 0 };
my $out_of_scope_error = $args{out_of_scope_error} || sub { return 'Game is outside allowed scope' };

my @errors;
my %existing = map { $_->game->id => $_->pick->id }
$schema->resultset('Pick')->search({ player => $player_id })->all;
my %effective = (%existing, %{$pick_map});

my @changed_games;
foreach my $game_id (sort { $a <=> $b } keys %{$pick_map}) {
if (!$allowed_games{$game_id}) {
push @errors, "Game ${game_id} is not a Final Four game";
if (!$is_in_scope->($game_id)) {
push @errors, $out_of_scope_error->($game_id);
next;
}
push @changed_games, $game_id;
}

my $games_to_validate = _affected_games_in_scope(
$schema,
\@changed_games,
sub {
my ($game_id) = @_;
return $allowed_games{$game_id};
}
);

my $games_to_validate = _affected_games_in_scope($schema, \@changed_games, $is_in_scope);
foreach my $game_id (sort { $a <=> $b } keys %{$games_to_validate}) {
next if !exists $effective{$game_id};

my $team_id = $effective{$game_id};
push @errors, _validate_pick_for_game($schema, $game_id, $team_id, undef, \%effective);
push @errors, _validate_pick_for_game($schema, $game_id, $team_id, $validation_region_id, \%effective);
}

@errors = grep { defined $_ && $_ ne '' } @errors;
return @errors
? { ok => 0, errors => \@errors }
: { ok => 1, normalized_picks => $pick_map };
return grep { defined $_ && $_ ne '' } @errors;
}

sub _extract_pick_map {
Expand Down
21 changes: 21 additions & 0 deletions t/bracket_validator.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ my $invalid_region = Bracket::Service::BracketValidator->validate_region_payload
ok(!$invalid_region->{ok}, 'invalid region payload fails');
like(join(' ', @{$invalid_region->{errors}}), qr/not a valid advancement/, 'invalid region failure is continuity-related');

my $outside_region = Bracket::Service::BracketValidator->validate_region_payload(
$schema,
$player_id,
1,
{
p16 => 17,
}
);
ok(!$outside_region->{ok}, 'region payload rejects out-of-scope game id');
like(join(' ', @{$outside_region->{errors}}), qr/outside region 1/, 'region out-of-scope error is explicit');

# Seed a full East path where UConn (team 15) advances deep, then ensure stale
# downstream picks are rejected when an earlier game changes to Furman (team 16).
for my $row (
Expand Down Expand Up @@ -167,6 +178,16 @@ my $invalid_final4 = Bracket::Service::BracketValidator->validate_final4_payload
ok(!$invalid_final4->{ok}, 'invalid final4 payload fails');
like(join(' ', @{$invalid_final4->{errors}}), qr/not a valid advancement/, 'invalid final4 failure is continuity-related');

my $outside_final4 = Bracket::Service::BracketValidator->validate_final4_payload(
$schema,
$player_id,
{
p1 => 1,
}
);
ok(!$outside_final4->{ok}, 'final4 payload rejects non-final4 game id');
like(join(' ', @{$outside_final4->{errors}}), qr/not a Final Four game/, 'final4 out-of-scope error is explicit');

my $bad_param = Bracket::Service::BracketValidator->validate_region_payload(
$schema,
$player_id,
Expand Down
Loading