diff --git a/app/controllers/team_action_items_controller.rb b/app/controllers/team_action_items_controller.rb index b4f14ee..b63a5d0 100644 --- a/app/controllers/team_action_items_controller.rb +++ b/app/controllers/team_action_items_controller.rb @@ -8,8 +8,10 @@ class TeamActionItemsController < ApplicationController # PATCH /team_action_items/1 def update - if @team_action_item.update(team_action_item_params) - push_sync_if_needed!(@team_action_item.tournament) + if TeamActionItemUpdate.call( + team_action_item: @team_action_item, + completed: team_action_item_params.fetch(:completed) + ) render json: @team_action_item else render json: @team_action_item.errors, status: :unprocessable_entity @@ -25,8 +27,4 @@ class TeamActionItemsController < ApplicationController def team_action_item_params params.slice(:completed).permit(:completed) end - - def push_sync_if_needed!(tournament) - TournamentSyncEnqueue.call(tournament) - end end diff --git a/app/controllers/team_action_lists_controller.rb b/app/controllers/team_action_lists_controller.rb index 06c13c5..bff4b94 100644 --- a/app/controllers/team_action_lists_controller.rb +++ b/app/controllers/team_action_lists_controller.rb @@ -9,8 +9,10 @@ class TeamActionListsController < ApplicationController # PATCH /tournaments/:tournament_id/team_action_lists/:key/teams/:team_id def update_item - if @team_action_item.update(team_action_item_params) - push_sync_if_needed!(@tournament) + if TeamActionItemUpdate.call( + team_action_item: @team_action_item, + completed: team_action_item_params.fetch(:completed) + ) render json: @team_action_item else render json: @team_action_item.errors, status: :unprocessable_entity @@ -31,8 +33,4 @@ class TeamActionListsController < ApplicationController def team_action_item_params params.slice(:completed).permit(:completed) end - - def push_sync_if_needed!(tournament) - TournamentSyncEnqueue.call(tournament) - end end diff --git a/app/services/team_action_item_update.rb b/app/services/team_action_item_update.rb new file mode 100644 index 0000000..955051e --- /dev/null +++ b/app/services/team_action_item_update.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class TeamActionItemUpdate + class << self + def call(team_action_item:, completed:) + new(team_action_item: team_action_item, completed: completed).call + end + end + + def initialize(team_action_item:, completed:) + @team_action_item = team_action_item + @completed = completed + end + + def call + return false unless team_action_item.update(completed: completed) + + TournamentSyncEnqueue.call(team_action_item.tournament) + true + end + + private + + attr_reader :team_action_item, :completed +end diff --git a/spec/e2e/http/tournament_follow_sync_spec.rb b/spec/e2e/http/tournament_follow_sync_spec.rb index d0af1dd..3e95176 100644 --- a/spec/e2e/http/tournament_follow_sync_spec.rb +++ b/spec/e2e/http/tournament_follow_sync_spec.rb @@ -93,9 +93,12 @@ RSpec.describe 'Tournament follower sync HTTP E2E' do expect(locked_team_update[:status]).to eq(423) expect(locked_team_update.dig(:json, :error)).to eq('Tournament is in read only follower mode') - log_phase!('mark playoff token collected on source and verify follower mirrors it') + log_phase!('mark playoff token collected on source through business key route and verify follower mirrors it') source_token_item = playoff_token_list(source).fetch(:team_action_items).first - source_token_update = source_owner_client.patch("/team_action_items/#{source_token_item.fetch(:id)}", body: { completed: true }) + source_token_update = source_owner_client.patch( + "/tournaments/#{source.fetch(:id)}/team_action_lists/group_stage_survivor_playoff_tokens/teams/#{source_token_item.dig(:team, :id)}", + body: { completed: true } + ) expect(source_token_update[:status]).to eq(200) source = fetch_tournament(client: source_anonymous_client, tournament_id: source.fetch(:id)) @@ -104,7 +107,10 @@ RSpec.describe 'Tournament follower sync HTTP E2E' do expect(playoff_token_item_completed?(follower, team_name: source_token_item.dig(:team, :name))).to eq(true) follower_token_item = find_team_action_item(follower, team_name: source_token_item.dig(:team, :name)) - locked_token_update = follower_owner_client.patch("/team_action_items/#{follower_token_item.fetch(:id)}", body: { completed: false }) + locked_token_update = follower_owner_client.patch( + "/tournaments/#{follower.fetch(:id)}/team_action_lists/group_stage_survivor_playoff_tokens/teams/#{follower_token_item.dig(:team, :id)}", + body: { completed: false } + ) expect(locked_token_update[:status]).to eq(423) expect(locked_token_update.dig(:json, :error)).to eq('Tournament is in read only follower mode') diff --git a/spec/e2e/http/tournament_sync_test_spec.rb b/spec/e2e/http/tournament_sync_test_spec.rb index c5b996e..5507f5e 100644 --- a/spec/e2e/http/tournament_sync_test_spec.rb +++ b/spec/e2e/http/tournament_sync_test_spec.rb @@ -54,6 +54,42 @@ RSpec.describe 'Tournament sync test HTTP E2E' do .to match_array(after_leader.fetch(:teams).map { |team| team.fetch(:name) }) end + it 'syncs team action list changes made through item id route and business key route' do + follower = create_follower_tournament(name_prefix: 'Sync Action Follower') + leader = create_group_stage_tournament( + name_prefix: 'Sync Action Leader', + sync_target_url: "#{base_url}/tournaments/#{follower.fetch(:id)}/sync_state", + sync_auth_token: sync_token + ) + + finish_group_stage_and_create_playoffs!(leader.fetch(:id)) + + leader_after_playoffs = fetch_tournament(leader.fetch(:id)) + token_list = find_playoff_token_list!(leader_after_playoffs) + first_item = token_list.fetch(:team_action_items).first + second_item = token_list.fetch(:team_action_items).second + + update_by_item_id = client.patch("/team_action_items/#{first_item.fetch(:id)}", body: { completed: true }) + expect(update_by_item_id[:status]).to eq(200) + + update_by_business_key = client.patch( + "/tournaments/#{leader.fetch(:id)}/team_action_lists/group_stage_survivor_playoff_tokens/teams/#{second_item.dig(:team, :id)}", + body: { completed: true } + ) + expect(update_by_business_key[:status]).to eq(200) + + test_sync = client.post("/tournaments/#{leader.fetch(:id)}/test_sync") + expect(test_sync[:status]).to eq(200) + + leader_after_update = fetch_tournament(leader.fetch(:id)) + follower_after_update = fetch_tournament(follower.fetch(:id)) + + expect(playoff_token_item_completed?(leader_after_update, team_name: first_item.dig(:team, :name))).to eq(true) + expect(playoff_token_item_completed?(leader_after_update, team_name: second_item.dig(:team, :name))).to eq(true) + expect(playoff_token_item_completed?(follower_after_update, team_name: first_item.dig(:team, :name))).to eq(true) + expect(playoff_token_item_completed?(follower_after_update, team_name: second_item.dig(:team, :name))).to eq(true) + end + def login_client api_client = TurniereE2E::ApiClient.new(base_url: base_url) response = api_client.login!(email: owner_email, password: owner_password) @@ -103,4 +139,47 @@ RSpec.describe 'Tournament sync test HTTP E2E' do expect(response[:status]).to eq(200) response.fetch(:json) end + + def finish_group_stage_and_create_playoffs!(tournament_id) + tournament = fetch_tournament(tournament_id) + group_stage = tournament.fetch(:stages).find { |stage| stage.fetch(:level) == -1 } + + group_stage.fetch(:groups).each do |group| + group.fetch(:matches).each_with_index do |match, index| + play_match!(match.fetch(:id), first_score: 10 + index, second_score: index) + end + end + + finish_stage = client.patch("/stages/#{group_stage.fetch(:id)}", body: { state: 'finished' }) + expect(finish_stage[:status]).to eq(200) + end + + def play_match!(match_id, first_score:, second_score:) + match_response = anonymous_client.get("/matches/#{match_id}") + expect(match_response[:status]).to eq(200) + + start_response = client.patch("/matches/#{match_id}", body: { state: 'in_progress' }) + expect(start_response[:status]).to eq(200) + + ordered_scores = match_response.fetch(:json).fetch(:match_scores).sort_by { |score| score.fetch(:id) } + [first_score, second_score].each_with_index do |points, index| + score_update = client.patch("/match_scores/#{ordered_scores[index].fetch(:id)}", body: { points: points }) + expect(score_update[:status]).to eq(200) + end + + finish_response = client.patch("/matches/#{match_id}", body: { state: 'finished' }) + expect(finish_response[:status]).to eq(200) + end + + def find_playoff_token_list!(tournament) + tournament.fetch(:team_action_lists).find do |list| + list.fetch(:key) == 'group_stage_survivor_playoff_tokens' + end || raise('playoff token list missing') + end + + def playoff_token_item_completed?(tournament, team_name:) + list = find_playoff_token_list!(tournament) + item = list.fetch(:team_action_items).find { |candidate| candidate.dig(:team, :name) == team_name } + item.fetch(:completed) + end end