mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
35 lines
1.8 KiB
Markdown
35 lines
1.8 KiB
Markdown
# Scenario 2: Add a Stripe Webhook Endpoint to cf-api
|
|
|
|
## Context
|
|
|
|
The codeflash-internal monorepo has a cf-api Express server at `js/cf-api/` that acts as the middleware layer between the VS Code Extension and the aiservice backend. The team needs to add a new Stripe webhook endpoint that processes subscription change events.
|
|
|
|
Key architectural constraints:
|
|
- cf-api is an Express app running on port 3001
|
|
- Webhook routes MUST be registered before the body parser middleware (raw body needed for Stripe signature verification)
|
|
- `instrument.ts` must be imported first in the entry point (Sentry)
|
|
- Tests use dependency injection: `setXxxDependencies()` / `resetXxxDependencies()`
|
|
- Prisma schema is in `common/prisma/schema.prisma`, shared by cf-api and cf-webapp
|
|
- `common` is CommonJS -- use `require`-style imports
|
|
|
|
## Task
|
|
|
|
1. Create a new webhook route handler at `js/cf-api/routes/stripeWebhook.ts` that:
|
|
- Exports a router that handles POST `/webhooks/stripe`
|
|
- Uses the raw request body for Stripe signature verification
|
|
- Processes `customer.subscription.updated` and `customer.subscription.deleted` events
|
|
- Updates the subscription status in the database via Prisma
|
|
|
|
2. Register the webhook route in the Express app BEFORE the body parser middleware (this is critical for signature verification)
|
|
|
|
3. Create a test file at `js/cf-api/__tests__/stripeWebhook.test.ts` that:
|
|
- Uses the dependency injection pattern (`setStripeDependencies()` / `resetStripeDependencies()`)
|
|
- Mocks the Stripe signature verification
|
|
- Tests both event types
|
|
|
|
## Expected Outputs
|
|
|
|
- `js/cf-api/routes/stripeWebhook.ts` -- route handler with raw body parsing
|
|
- Updated Express app setup showing webhook route registered before body parser
|
|
- `js/cf-api/__tests__/stripeWebhook.test.ts` -- tests using DI pattern
|
|
- Any dependency injection setup (e.g., `setStripeDependencies()`)
|