Add option to also pass seconds to timer_end

This commit is contained in:
Daniel Schädler 2025-03-10 12:37:19 +01:00
parent bdcc6e1182
commit 4d1e2c5164
2 changed files with 76 additions and 31 deletions

View File

@ -181,16 +181,32 @@ end
def validate_set_timer_end_params
timer_end = params[:timer_end]
return render json: { error: 'Timer end is required' }, status: :unprocessable_entity unless timer_end.present?
timer_end_seconds = params[:timer_end_seconds]
# throw error if both timer_end and timer_end_seconds are present
if timer_end.present? && timer_end_seconds.present?
return render json: { error: 'Only one of timer_end or timer_end_seconds is allowed' }, status: :unprocessable_entity
end
if timer_end_seconds.present?
begin
parsed_time = Time.zone.now + timer_end_seconds.to_i
params[:timer_end] = parsed_time
rescue ArgumentError
return render json: { error: 'Invalid seconds format' }, status: :unprocessable_entity
end
elsif timer_end.present?
begin
parsed_time = Time.zone.parse(timer_end)
if parsed_time.nil?
render json: { error: 'Invalid datetime format' }, status: :unprocessable_entity
return render json: { error: 'Invalid datetime format' }, status: :unprocessable_entity
elsif !parsed_time.future?
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
rescue ArgumentError
render json: { error: 'Invalid datetime format' }, status: :unprocessable_entity
return render json: { error: 'Invalid datetime format' }, status: :unprocessable_entity
end
else
return render json: { error: 'Timer end is required' }, status: :unprocessable_entity
end
end

View File

@ -483,7 +483,7 @@ RSpec.describe TournamentsController, type: :controller do
apply_authentication_headers_for @tournament.owner
@request.env['HTTP_ACCEPT'] = 'application/json'
end
context 'timer_end' do
context 'when timer_end is missing' do
it 'returns unprocessable entity' do
patch :set_timer_end, params: { id: @tournament.id }
@ -517,6 +517,35 @@ RSpec.describe TournamentsController, type: :controller do
end
end
end
context 'when timer_end_seconds is provided' do
it 'returns unprocessable entity for invalid seconds format' do
patch :set_timer_end, params: { id: @tournament.id, timer_end_seconds: 'invalid' }
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)).to include('error' => 'Invalid seconds format')
end
it 'returns unprocessable entity for negative seconds' do
patch :set_timer_end, params: { id: @tournament.id, timer_end_seconds: -3600 }
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)).to include('error' => 'Timer end must be in the future')
end
it 'updates the timer_end with valid seconds' do
valid_timer_end_seconds = 3600
expected_timer_end = (Time.zone.now + valid_timer_end_seconds).change(usec: 0)
patch :set_timer_end, params: { id: @tournament.id, timer_end_seconds: valid_timer_end_seconds }
expect(response).to have_http_status(:ok)
expect(@tournament.reload.timer_end.change(usec: 0)).to eq(expected_timer_end)
end
end
context 'when both timer_end and timer_end_seconds are provided' do
it 'returns unprocessable entity' do
patch :set_timer_end, params: { id: @tournament.id, timer_end: 1.day.from_now, timer_end_seconds: 3600 }
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)).to include('error' => 'Only one of timer_end or timer_end_seconds is allowed')
end
end
end
describe 'GET #timer_end' do
before do