Prevent duplicate bets on the same match

This commit is contained in:
Thor77 2019-06-14 16:47:31 +02:00
parent ba758c73ff
commit 167a2116f1
No known key found for this signature in database
GPG Key ID: 5051E71B46AA669A
2 changed files with 21 additions and 5 deletions

View File

@ -18,5 +18,6 @@ class UserService
else
raise UserServiceError, 'The given team is not involved in the given match' unless match.teams.include? team
end
raise UserServiceError, 'This user already created a bet on this match' if match.bets.map(&:user).include? @user
end
end

View File

@ -21,20 +21,34 @@ RSpec.describe UserService do
create(:playoff_match, match_scores: [create(:match_score, team: @team)])
end
let(:bet) do
@service.bet! match, @team
before do
@bet = @service.bet! match, @team
@user.reload
match.reload
@team.reload
end
it 'associates the bet with the given team' do
expect(@team.bets).to include(bet)
expect(@team.bets).to include(@bet)
end
it 'associates the bet with the given match' do
expect(match.bets).to include(bet)
expect(match.bets).to include(@bet)
end
it 'associates the bet with the creating user' do
expect(@user.bets).to include(bet)
expect(@user.bets).to include(@bet)
end
context 'with an already existing bet' do
it 'throws an exception' do
match = create(:playoff_match, match_scores: [create(:match_score, team: @team)])
@service.bet! match, @team
match.reload
expect do
@service.bet! match, @team
end.to raise_error(UserServiceError, 'This user already created a bet on this match')
end
end
end
@ -54,6 +68,7 @@ RSpec.describe UserService do
match = create(:group_match)
match.match_scores << create(:match_score, team: @team)
bet = @service.bet! match, nil
match.reload
expect(match.bets).to include(bet)
expect(@user.bets).to include(bet)
end