Prevent matches from being stopped without a winner in playoffs
This commit is contained in:
parent
3cf2828dae
commit
df0b2bccb5
|
|
@ -13,6 +13,11 @@ class MatchesController < ApplicationController
|
||||||
# PATCH/PUT /matches/1
|
# PATCH/PUT /matches/1
|
||||||
def update
|
def update
|
||||||
new_state = match_params['state']
|
new_state = match_params['state']
|
||||||
|
if new_state == 'finished' && @match.current_leading_team.nil?
|
||||||
|
render json: { error: 'Stopping undecided Matches isn\'t allowed in playoff stage' },
|
||||||
|
status: :unprocessable_entity
|
||||||
|
return
|
||||||
|
end
|
||||||
if @match.update(match_params)
|
if @match.update(match_params)
|
||||||
if new_state == 'finished'
|
if new_state == 'finished'
|
||||||
result = PopulateMatchBelowAndSave.call(match: @match) unless @match.group_match?
|
result = PopulateMatchBelowAndSave.call(match: @match) unless @match.group_match?
|
||||||
|
|
|
||||||
|
|
@ -70,33 +70,66 @@ RSpec.describe MatchesController, type: :controller do
|
||||||
apply_authentication_headers_for @running_playoff_match.owner
|
apply_authentication_headers_for @running_playoff_match.owner
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
context 'on a decided match' do
|
||||||
@running_playoff_match.match_scores.each_with_index do |ms, i|
|
|
||||||
ms.points = i
|
|
||||||
ms.save!
|
|
||||||
end
|
|
||||||
put :update, params: { id: @running_playoff_match.to_param }.merge(finished)
|
|
||||||
@running_playoff_match.reload
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'updates the matches status' do
|
|
||||||
expect(response).to be_successful
|
|
||||||
expect(@running_playoff_match.state).to eq(finished[:state])
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'updates the match below' do
|
|
||||||
before do
|
before do
|
||||||
@match_below = @tournament.stages.find_by(level: @amount_of_stages - 1).matches
|
@running_playoff_match.match_scores.each_with_index do |ms, i|
|
||||||
.find_by(position: @running_playoff_match.position / 2).reload
|
ms.points = i
|
||||||
|
ms.save!
|
||||||
|
end
|
||||||
|
put :update, params: { id: @running_playoff_match.to_param }.merge(finished)
|
||||||
|
@running_playoff_match.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'with the right teams' do
|
it 'updates the matches status' do
|
||||||
expect(@running_playoff_match.winner).to be_a(Team)
|
expect(response).to be_successful
|
||||||
expect(@match_below.teams).to include(@running_playoff_match.winner)
|
expect(@running_playoff_match.state).to eq(finished[:state])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'with the right status' do
|
describe 'updates the match below' do
|
||||||
expect(@match_below.state).to eq('not_ready')
|
before do
|
||||||
|
@match_below = @tournament.stages.find_by(level: @amount_of_stages - 1).matches
|
||||||
|
.find_by(position: @running_playoff_match.position / 2).reload
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'with the right teams' do
|
||||||
|
expect(@running_playoff_match.winner).to be_a(Team)
|
||||||
|
expect(@match_below.teams).to include(@running_playoff_match.winner)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'with the right status' do
|
||||||
|
expect(@match_below.state).to eq('not_ready')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'on a undecided match' do
|
||||||
|
before do
|
||||||
|
@running_playoff_match.match_scores.each do |ms|
|
||||||
|
ms.points = 42
|
||||||
|
ms.save!
|
||||||
|
end
|
||||||
|
put :update, params: { id: @running_playoff_match.to_param }.merge(finished)
|
||||||
|
@running_playoff_match.reload
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns unprocessable entity' do
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(@running_playoff_match.state).to eq('in_progress')
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'doesn\'t update the match below' do
|
||||||
|
before do
|
||||||
|
@match_below = @tournament.stages.find_by(level: @amount_of_stages - 1).matches
|
||||||
|
.find_by(position: @running_playoff_match.position / 2).reload
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'teams' do
|
||||||
|
expect(@match_below.teams.empty?).to be(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'status' do
|
||||||
|
expect(@match_below.state).to eq('not_ready')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue