Prevent duplicate bets on the same match
This commit is contained in:
parent
ba758c73ff
commit
167a2116f1
|
|
@ -18,5 +18,6 @@ class UserService
|
||||||
else
|
else
|
||||||
raise UserServiceError, 'The given team is not involved in the given match' unless match.teams.include? team
|
raise UserServiceError, 'The given team is not involved in the given match' unless match.teams.include? team
|
||||||
end
|
end
|
||||||
|
raise UserServiceError, 'This user already created a bet on this match' if match.bets.map(&:user).include? @user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -21,20 +21,34 @@ RSpec.describe UserService do
|
||||||
create(:playoff_match, match_scores: [create(:match_score, team: @team)])
|
create(:playoff_match, match_scores: [create(:match_score, team: @team)])
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:bet) do
|
before do
|
||||||
@service.bet! match, @team
|
@bet = @service.bet! match, @team
|
||||||
|
@user.reload
|
||||||
|
match.reload
|
||||||
|
@team.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'associates the bet with the given team' do
|
it 'associates the bet with the given team' do
|
||||||
expect(@team.bets).to include(bet)
|
expect(@team.bets).to include(@bet)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'associates the bet with the given match' do
|
it 'associates the bet with the given match' do
|
||||||
expect(match.bets).to include(bet)
|
expect(match.bets).to include(@bet)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'associates the bet with the creating user' do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -54,6 +68,7 @@ RSpec.describe UserService do
|
||||||
match = create(:group_match)
|
match = create(:group_match)
|
||||||
match.match_scores << create(:match_score, team: @team)
|
match.match_scores << create(:match_score, team: @team)
|
||||||
bet = @service.bet! match, nil
|
bet = @service.bet! match, nil
|
||||||
|
match.reload
|
||||||
expect(match.bets).to include(bet)
|
expect(match.bets).to include(bet)
|
||||||
expect(@user.bets).to include(bet)
|
expect(@user.bets).to include(bet)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue