Enqueue sync after transaction commit

This commit is contained in:
Daniel Schädler 2026-04-14 20:59:30 +02:00
parent 9cfd0c9444
commit 8bdd7fd1be
4 changed files with 11 additions and 5 deletions

View File

@ -66,6 +66,7 @@ class MatchesController < ApplicationController
# PATCH/PUT /matches/1
def update
new_state = match_params['state']
synced_tournament = nil
Match.transaction do
if @match.update(match_params)
@ -78,12 +79,13 @@ class MatchesController < ApplicationController
end
render json: @match
push_sync_if_needed!(@match.tournament)
synced_tournament = @match.tournament
else
render json: @match.errors, status: :unprocessable_entity
raise ActiveRecord::Rollback
end
end
push_sync_if_needed!(synced_tournament) unless synced_tournament.nil?
end
private

View File

@ -19,17 +19,19 @@ class StagesController < ApplicationController
return
end
synced_tournament = nil
Stage.transaction do
if @stage.update(stage_params)
handle_group_stage_end
render json: @stage
push_sync_if_needed!(@stage.tournament)
synced_tournament = @stage.tournament
else
render json: @stage.errors, status: :unprocessable_entity
raise ActiveRecord::Rollback
end
end
push_sync_if_needed!(synced_tournament) unless synced_tournament.nil?
else
render json: {
error: 'The state attribute may only be changed to finished'

View File

@ -85,6 +85,7 @@ class TournamentsController < ApplicationController
# PATCH/PUT /tournaments/1
def update
synced_tournament = nil
Tournament.transaction do
if only_playoff_teams_amount_changed
@tournament.instant_finalists_amount, @tournament.intermediate_round_participants_amount =
@ -94,13 +95,14 @@ class TournamentsController < ApplicationController
)
end
if @tournament.update(tournament_params)
push_sync_if_needed!(@tournament)
synced_tournament = @tournament
render json: @tournament
else
render json: @tournament.errors, status: :unprocessable_entity
raise ActiveRecord::Rollback
end
end
push_sync_if_needed!(synced_tournament) unless synced_tournament.nil?
end
# DELETE /tournaments/1

View File

@ -55,11 +55,11 @@ RSpec.describe StagesController, type: :controller do
end
it 'adds new stages to the tournament' do
expect(running_group_stage.tournament.stages.size).to be > 1
expect(running_group_stage.tournament.reload.stages.size).to be > 1
end
it 'adds the right teams' do
expect(running_group_stage.tournament.stages.max_by(&:level).teams)
expect(running_group_stage.tournament.reload.stages.max_by(&:level).teams)
.to match_array(GroupStageService.get_advancing_teams(running_group_stage))
end
end