feat: non-prod sample market seed endpoint#281
Open
Davichi-1 wants to merge 1 commit into
Open
Conversation
Add POST /api/admin/seed for development/staging/test environments that inserts a small, fixed batch of sample markets for E2E and demos. - Non-prod only: 404 in production (route hidden before auth) plus a service-level SeedNotAllowedError guard as defense-in-depth. - Idempotent: stable primary keys + ON CONFLICT DO NOTHING; repeat calls insert nothing and report rows as skipped. - Tracked: seeded rows tagged with metadata.seeded=true + batch version and listable via seedService.listSeeded(). - Secure: admin JWT guard, per-token rate limit, strict body validation with the standard error envelope, structured logging + audit trail with correlation ids. - Documented in docs/seed.md; covered by route + service tests. Closes Predictify-org#160 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
@Davichi-1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Overview
This PR adds an admin sample-market seed endpoint to the backend so E2E suites and demos have predictable data to work against in non-production environments. It introduces
POST /api/admin/seed, which inserts a small, fixed batch of sample markets. The operation is idempotent, tracks the rows it seeds, and is unavailable in production.Related Issue
Closes #160
Changes
⚙️ Seed Service
src/services/seedService.tsseed-market-001…005) +ON CONFLICT (id) DO NOTHING, so repeat runs insert nothing and report rows asskipped.metadata.seeded = trueplus aseedBatchVersion; exposeslistSeeded()to read them back.SeedNotAllowedErrorguard so the service can never write sample data to a production database, even if called directly.market.createdstructured log event per inserted row and writes anadmin.seed_marketsaudit entry, both carrying a correlation id.SeedRepository) with a Drizzle implementation, kept injectable for testing.🌐 Seed API Surface
[ADD]
src/routes/admin/seed.tsAdded
POST /api/admin/seedto trigger the seed.Security layers (outermost first): production guard →
404before auth (route is not even probeable); per-admin-token rate limit (30/min, IP fallback);requireAdminJWT guard.Input validation at the boundary with a strict body schema; unexpected fields return
400 validation_errorin the standard error envelope.[MODIFY]
src/index.tsRegistered the new router at
/api/admin/seed.🧪 Tests
[ADD]
tests/seedService.test.tsCovers fresh seed, idempotent re-run (no inserts, no events), actor propagation, the production guard, the error shape, and the Drizzle repository.
[ADD]
tests/adminSeed.test.tsCovers the admin guard (missing/non-admin/expired JWT), success + context forwarding, idempotent response, body validation, production
404, rate-limit429, and error mapping.📚 Documentation
docs/seed.mdVerification Results
404in production, runs before auth)ON CONFLICT DO NOTHING, repeat = 0 inserts)metadata.seeded, listable)docs/seed.md)🤖 Generated with Claude Code