flag abused odd

This commit is contained in:
Asher Samuel 2025-07-15 15:39:47 +03:00
parent f63e35fb4e
commit 1c3f575195
5 changed files with 58 additions and 3 deletions

View File

@ -101,6 +101,10 @@ WHERE (event_id = $1)
SELECT *
FROM bet_outcomes
WHERE bet_id = $1;
-- name: GetBetOutcomeCountByOddID :one
SELECT COUNT(*)
FROM bet_outcomes
WHERE odd_id = $1;
-- name: GetBetCountByUserID :one
SELECT COUNT(*)
FROM bets

View File

@ -410,6 +410,19 @@ func (q *Queries) GetBetOutcomeByEventID(ctx context.Context, arg GetBetOutcomeB
return items, nil
}
const GetBetOutcomeCountByOddID = `-- name: GetBetOutcomeCountByOddID :one
SELECT COUNT(*)
FROM bet_outcomes
WHERE odd_id = $1
`
func (q *Queries) GetBetOutcomeCountByOddID(ctx context.Context, oddID int64) (int64, error) {
row := q.db.QueryRow(ctx, GetBetOutcomeCountByOddID, oddID)
var count int64
err := row.Scan(&count)
return count, err
}
const GetBetsForCashback = `-- name: GetBetsForCashback :many
SELECT id, amount, total_odds, status, user_id, is_shop_bet, cashed_out, outcomes_hash, fast_code, processed, created_at, updated_at, full_name, phone_number, outcomes
FROM bet_with_outcomes

View File

@ -299,6 +299,15 @@ func (s *Store) GetBetCountByOutcomesHash(ctx context.Context, outcomesHash stri
return count, nil
}
func (s *Store) GetBetOutcomeCountByOddID(ctx context.Context, oddID int64) (int64, error) {
count, err := s.queries.GetBetOutcomeCountByOddID(ctx, oddID)
if err != nil {
return 0, err
}
return count, nil
}
func (s *Store) UpdateCashOut(ctx context.Context, id int64, cashedOut bool) error {
err := s.queries.UpdateCashOut(ctx, dbgen.UpdateCashOutParams{
ID: id,

View File

@ -17,6 +17,7 @@ type BetStore interface {
GetBetByFastCode(ctx context.Context, fastcode string) (domain.GetBet, error)
GetBetOutcomeByEventID(ctx context.Context, eventID int64, is_filtered bool) ([]domain.BetOutcome, error)
GetBetOutcomeByBetID(ctx context.Context, betID int64) ([]domain.BetOutcome, error)
GetBetOutcomeCountByOddID(ctx context.Context, oddID int64) (int64, error)
GetBetCountByUserID(ctx context.Context, userID int64, outcomesHash string) (int64, error)
GetBetCountByOutcomesHash(ctx context.Context, outcomesHash string) (int64, error)
UpdateCashOut(ctx context.Context, id int64, cashedOut bool) error

View File

@ -398,6 +398,36 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
return domain.CreateBetRes{}, err
}
for i := range outcomes {
// flag odds with large amount of users betting on them
count, err := s.betStore.GetBetOutcomeCountByOddID(ctx, outcomes[i].OddID)
if err != nil {
s.mongoLogger.Error("failed to get count of bet outcome",
zap.Int64("bet_id", bet.ID),
zap.Int64("odd_id", outcomes[i].OddID),
zap.Error(err),
)
return domain.CreateBetRes{}, err
}
// TODO: fetch cap from settings in db
if count > 20 {
flag := domain.CreateFlagReq{
BetID: 0,
OddID: outcomes[i].OddID,
Reason: fmt.Sprintf("too many users targeting odd - (%d)", outcomes[i].OddID),
}
_, err := s.betStore.CreateFlag(ctx, flag)
if err != nil {
s.mongoLogger.Error("failed to create flag for bet",
zap.Int64("bet_id", bet.ID),
zap.Error(err),
)
}
}
}
// flag bets that have more than three outcomes
if len(outcomes) > 3 {
flag := domain.CreateFlagReq{
@ -425,9 +455,7 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
return domain.CreateBetRes{}, err
}
fmt.Println("total bet count: ", total_bet_count)
if total_bet_count > 3 {
if total_bet_count > 10 {
flag := domain.CreateFlagReq{
BetID: bet.ID,
OddID: 0,