mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
1.8 KiB
1.8 KiB
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.tsmust 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 commonis CommonJS -- userequire-style imports
Task
-
Create a new webhook route handler at
js/cf-api/routes/stripeWebhook.tsthat:- Exports a router that handles POST
/webhooks/stripe - Uses the raw request body for Stripe signature verification
- Processes
customer.subscription.updatedandcustomer.subscription.deletedevents - Updates the subscription status in the database via Prisma
- Exports a router that handles POST
-
Register the webhook route in the Express app BEFORE the body parser middleware (this is critical for signature verification)
-
Create a test file at
js/cf-api/__tests__/stripeWebhook.test.tsthat:- Uses the dependency injection pattern (
setStripeDependencies()/resetStripeDependencies()) - Mocks the Stripe signature verification
- Tests both event types
- Uses the dependency injection pattern (
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())