Update group_scores on match_score change if match is a group match

This commit is contained in:
Daniel Schädler 2019-05-27 23:25:45 +02:00
parent b94bb9cdcd
commit 3cad50776f
4 changed files with 70 additions and 1 deletions

View File

@ -13,7 +13,10 @@ class MatchScoresController < ApplicationController
# PATCH/PUT /scores/1
def update
if @match_score.update(match_score_params)
GroupStageService.calculate_group_points(@match_score)
if @match_score.part_of_group_match?
match_score_group = @match_score.match.group
UpdateGroupsGroupScoresAndSave.call(group: match_score_group)
end
render json: @match_score
else
render json: @match_score.errors, status: :unprocessable_entity

View File

@ -0,0 +1,15 @@
# frozen_string_literal: true
class UpdateGroupsGroupScores
include Interactor
def call
group = context.group
begin
group_scores = GroupStageService.update_group_scores(group)
context.object_to_save = group_scores
rescue StandardError
context.fail!
end
end
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class UpdateGroupsGroupScoresAndSave
include Interactor::Organizer
organize UpdateGroupsGroupScores, SaveApplicationRecordObject
end

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
RSpec.describe UpdateGroupsGroupScores do
before do
@group = create(:group)
@group_scores = create_list(:group_score, 2)
end
context 'save succeeds' do
let(:context) do
UpdateGroupsGroupScores.call(group: @group)
end
before do
allow(GroupStageService)
.to receive(:update_group_scores).with(@group)
.and_return(@group_scores)
end
it 'succeeds' do
expect(context).to be_a_success
end
it 'provides the objects to save' do
expect(context.object_to_save).to eq(@group_scores)
end
end
context 'exception is thrown' do
let(:context) do
UpdateGroupsGroupScores.call(group: @group)
end
before do
allow(GroupStageService)
.to receive(:update_group_scores).with(@group)
.and_throw('This failed :(')
end
it 'fails' do
test = context.failure?
expect(test).to eq(true)
end
end
end