Fix timer timezone handling

This commit is contained in:
Tobias Huber 2026-04-13 23:30:29 +02:00
parent 19f744daeb
commit 58267b6b3a
3 changed files with 69 additions and 31 deletions

View File

@ -96,7 +96,7 @@ class TournamentsController < ApplicationController
# GET /tournaments/:id/timer_end # GET /tournaments/:id/timer_end
def timer_end def timer_end
render json: { timer_end: @tournament.timer_end } render json: { timer_end: serialize_timer_end(@tournament.timer_end) }
end end
# PATCH /tournaments/:id/set_timer_end # PATCH /tournaments/:id/set_timer_end
@ -115,6 +115,10 @@ class TournamentsController < ApplicationController
{ timer_end: params[:timer_end] } { timer_end: params[:timer_end] }
end end
def serialize_timer_end(timer_end)
timer_end&.iso8601
end
def organize_teams_in_groups(teams) def organize_teams_in_groups(teams)
# each team gets put into an array of teams depending on the group specified in team[:group] # each team gets put into an array of teams depending on the group specified in team[:group]
teams.group_by { |team| team['group'] }.values.map do |group| teams.group_by { |team| team['group'] }.values.map do |group|
@ -177,9 +181,8 @@ class TournamentsController < ApplicationController
error: 'playoff_teams_amount, instant_finalists_amount and intermediate_round_participants_amount don\'t match' error: 'playoff_teams_amount, instant_finalists_amount and intermediate_round_participants_amount don\'t match'
}, status: :unprocessable_entity }, status: :unprocessable_entity
end end
end
def validate_set_timer_end_params def validate_set_timer_end_params
timer_end = params[:timer_end] timer_end = params[:timer_end]
timer_end_seconds = params[:timer_end_seconds] timer_end_seconds = params[:timer_end_seconds]
@ -207,10 +210,13 @@ def validate_set_timer_end_params
elsif !parsed_time.future? elsif !parsed_time.future?
return render json: { error: 'Timer end must be in the future' }, status: :unprocessable_entity return render json: { error: 'Timer end must be in the future' }, status: :unprocessable_entity
end end
params[:timer_end] = parsed_time
rescue ArgumentError rescue ArgumentError
return render json: { error: 'Invalid datetime format' }, status: :unprocessable_entity return render json: { error: 'Invalid datetime format' }, status: :unprocessable_entity
end end
else else
return render json: { error: 'Timer end is required' }, status: :unprocessable_entity return render json: { error: 'Timer end is required' }, status: :unprocessable_entity
end end
end
end end

View File

@ -2,9 +2,13 @@
class TournamentSerializer < SimpleTournamentSerializer class TournamentSerializer < SimpleTournamentSerializer
attributes :description, :playoff_teams_amount, attributes :description, :playoff_teams_amount,
:instant_finalists_amount, :intermediate_round_participants_amount, :timer_end :instant_finalists_amount, :intermediate_round_participants_amount
has_many :stages has_many :stages
attribute :timer_end do
object.timer_end&.iso8601
end
attribute :owner_username do attribute :owner_username do
object.owner.username object.owner.username
end end

View File

@ -142,6 +142,34 @@ RSpec.describe TournamentsController, type: :controller do
end end
end end
describe 'GET #timer_end' do
it 'returns timer_end in ISO 8601 format' do
timer_end = Time.utc(2026, 4, 13, 18, 0, 0)
@tournament.update!(timer_end: timer_end)
get :timer_end, params: { id: @tournament.to_param }
expect(response).to be_successful
expect(deserialize_response(response)[:timer_end]).to eq(timer_end.iso8601)
end
end
describe 'PATCH #set_timer_end' do
before(:each) do
apply_authentication_headers_for @user
end
it 'stores timezone-aware timer_end values without shifting them' do
timer_end = '2030-04-13T18:00:00+02:00'
patch :set_timer_end, params: { id: @tournament.to_param, timer_end: timer_end }
expect(response).to be_successful
expect(@tournament.reload.timer_end.iso8601).to eq(Time.iso8601(timer_end).utc.iso8601)
expect(deserialize_response(response)[:timer_end]).to eq(@tournament.timer_end.iso8601)
end
end
describe 'POST #create' do describe 'POST #create' do
let(:create_playoff_tournament_data) do let(:create_playoff_tournament_data) do
{ {