Skip to content

[BUG] Backend Market Query Returns Deleted Markets in Bet Validation #649

Description

@Hahfyeex

Type: BUG
Severity: High
Component: backend/src/routes/bets.js

Context

The POST /api/bets route validates the market with: SELECT * FROM markets WHERE id = AND resolved = FALSE AND end_date > NOW(). This query does not filter out soft-deleted markets (where deleted_at IS NOT NULL). A market that has been soft-deleted can still receive new bets because the bet validation query does not check the deleted_at column. Bets placed on deleted markets will never be paid out.

Root Cause

The bet validation query in bets.js was written before the soft delete feature was implemented. It does not include the deleted_at IS NULL condition.

Fix Guide

  1. Update the bet validation query to add AND deleted_at IS NULL: SELECT * FROM markets WHERE id = AND resolved = FALSE AND end_date > NOW() AND deleted_at IS NULL.
  2. Return 400 with error: Market is not available for betting if the market is soft-deleted.
  3. Add a unit test that attempts to place a bet on a soft-deleted market and verifies it returns 400.
  4. Audit all other market queries in the codebase and add deleted_at IS NULL where missing.

Guidelines

  • Key requirement: deleted_at IS NULL Filter / Bet Validation Update / Full Query Audit.

Definition of Done

  • Bets on soft-deleted markets return 400 with a clear error message.
  • All market queries in the codebase include the deleted_at IS NULL filter.
  • Unit test covers the soft-deleted market bet rejection scenario.
  • Test coverage > 95%.

PR and Checkout

git checkout -b fix/soft-delete-bet-validation
git add .
git commit -m "fix: add deleted_at IS NULL filter to bet validation market query"
git push origin fix/soft-delete-bet-validation

Open a PR against main and include the issue number in the PR description.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions