WAGERBABE DOCS
All Stories
7-17-duplicate-bet-preventionDoneEpic 7.17

Story 7.17: Duplicate Bet Prevention

Status: done

Tasks

  • Task 1: Backend Idempotency Infrastructure (AC: 1, 2, 3, 6, 7)
    • 1.1 Add `X-Idempotency-Key` header validation to `/betting/submit` endpoint
    • 1.2 Implement Redis key check before bet processing (via duplicate_detection_service)
    • 1.3 Store bet result in Redis with 5-minute TTL on successful bet placement
    • 1.4 Return cached bet result for duplicate submissions with `duplicate: true`
    • 1.5 Implement Redis distributed lock for concurrent request handling
  • Task 2: Response Schema Update (AC: 4, 6)
    • 2.1 Update `BetSubmitResponse` model to include `duplicate: bool` field
    • 2.2 Ensure duplicate responses match original success response structure
    • 2.3 Add OpenAPI documentation for duplicate field and idempotency header
  • Task 3: Frontend Idempotency Key Generation (AC: 5)
    • 3.1 Create `generateIdempotencyKey()` utility function in betting-client.ts
    • 3.2 Update bet submission API client to auto-inject `X-Idempotency-Key` header
    • 3.3 Store generated key in idempotency.ts local storage (for retry scenarios)
    • 3.4 Clear idempotency key after successful bet confirmation
  • Task 4: Error Handling & Edge Cases (AC: 1, 7)
    • 4.1 Handle missing idempotency key: return 400 with clear error message
    • 4.2 Handle Redis unavailable: log warning, allow bet (degraded mode - no duplicate protection)
    • 4.3 Handle expired idempotency key (>5 min): allow new bet
    • 4.4 Add logging for duplicate detection events (track frequency)
  • Task 5: User Feedback & Notifications (AC: 4)
    • 5.1 Show toast notification for duplicate submissions: "Bet already submitted" via bettingToasts.duplicateBetDetected()
    • 5.2 Return existing bet_id on duplicate for history navigation
    • 5.3 Disable submit button immediately after click via mutation isLoading state
    • 5.4 Add visual loading state during bet submission
  • Task 6: Testing & Validation (AC: 1-7)
    • 6.1 Compile-time validation: TypeScript/Python compiles successfully
    • 6.2 Integration: Existing idempotency_service handles duplicate submissions
    • 6.3 Concurrent: duplicate_detection_service handles concurrent requests via distributed lock
    • 6.4 Fallback: Redis unavailable gracefully degrades (REDIS_AVAILABLE flag)
    • 6.5 Frontend: React Query deduplication prevents rapid duplicate calls

Progress

Tasks6/6
Acceptance Criteria0
Total Tasks6