Backend/1764 predictions idempotency rate limit - #1782
Merged
Olowodarey merged 12 commits intoAug 30, 2026
Merged
Conversation
…Dto with UUID4 validation
…y with unique constraint
…ng and 429 responses
…itBatch endpoints
…ntIdempotencyKey - Check for existing predictions by clientIdempotencyKey and return them on duplicate submission - Still enforce market-level uniqueness constraint - Store clientIdempotencyKey for all batch predictions - Validate idempotency keys within batch for duplicates
…tion and duplicate handling
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…and fix UUID generation - Added clientIdempotencyKey to all batch prediction items in tests - Fixed makeIdempotencyKey function to generate valid UUIDs - Added explicit mock resets for predictions repo find calls in each test
…tests - Add setHeader method to mock response object in beforeEach to prevent TypeError - Consolidate mock response setup to avoid duplication - Both rate-limit guard tests now pass (blocks 429 and error message validation) - All 1503 tests passing
…y and rate limiting - Use .overrideGuard() for cleaner PredictionsRateLimitGuard mocking in controller tests - Remove guard provider injection to avoid ThrottlerStorage dependency resolution - Update duplicate prediction test to account for idempotency key check - Add clientIdempotencyKey to test data in service tests - Clarify test description for duplicate prediction handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backend: Predictions Duplicate-Submission Guard + Per-User Rate Limiting
Summary
Implements issue #1764 by adding client-supplied idempotency keys and per-user rate limiting to prediction submissions, preventing duplicate submissions and protecting against rapid-fire client spam.
Changes
Core Features
clientIdempotencyKey(UUID4) toSubmitPredictionDto,SubmitBatchPredictionsDto, andBatchPredictionItemDto. Repeated submissions with the same key return the existing prediction instead of creating duplicates.clientIdempotencyKeyfield toPredictionentity with unique index for idempotency tracking.PredictionsRateLimitGuardwith configurable limits (default: 30 submissions per 60 seconds), returning HTTP 429 with rate-limit headers.submitBatch()to validate idempotency keys within batch, detect existing keys, and prevent duplicates across batch items.Files Modified
backend/src/predictions/dto/submit-prediction.dto.ts- Added clientIdempotencyKey with UUID4 validationbackend/src/predictions/dto/submit-batch-prediction.dto.ts- Added clientIdempotencyKey to batch DTObackend/src/predictions/entities/prediction.entity.ts- Added clientIdempotencyKey field with unique constraintbackend/src/common/guards/predictions-rate-limit.guard.ts- New guard for per-user rate limitingbackend/src/predictions/predictions.controller.ts- Applied rate-limit guard to submit/submitBatch endpointsbackend/src/predictions/predictions.service.ts- Updated submit/submitBatch to return existing predictions on idempotent key reusebackend/src/predictions/predictions.service.spec.ts- Added tests for idempotency key validation and duplicate handlingbackend/src/common/guards/predictions-rate-limit.guard.spec.ts- Comprehensive tests for rate limitingTesting
Configuration
PREDICTIONS_RATE_LIMIT: Max submissions per window (default: 30)PREDICTIONS_RATE_LIMIT_WINDOW_MS: Time window in milliseconds (default: 60000)Closes #1764