Skip to content

feat(CP-27): dashboard projection API with Monte Carlo#14

Merged
ncamaa merged 1 commit intomainfrom
feature/CP-27-dashboard-projection-api
Apr 12, 2026
Merged

feat(CP-27): dashboard projection API with Monte Carlo#14
ncamaa merged 1 commit intomainfrom
feature/CP-27-dashboard-projection-api

Conversation

@ncamaa
Copy link
Copy Markdown
Collaborator

@ncamaa ncamaa commented Apr 12, 2026

Summary

  • Add GET /api/plans/:planId/projection endpoint that loads all plan data from the database, transforms Prisma models into engine input types, runs the 36-year deterministic projection, and returns chart-ready arrays with a summary object
  • Add GET /api/plans/:planId/projection/monte-carlo endpoint that runs 1000-iteration Monte Carlo simulation with randomized return rates (Box-Muller normal distribution) and returns success rate, median final value, and p5/p25/p50/p75/p95 percentile bands
  • Implement runMonteCarloSimulation() in the engine layer (src/engine/monteCarlo.ts) - pure function, no DB access
  • Handle incomplete/empty plan data gracefully (valid minimal projection with zeros instead of errors)
  • Register both endpoints in OpenAPI/Swagger documentation

New files

  • src/engine/monteCarlo.ts - Monte Carlo simulation engine
  • src/api/projection/projectionModel.ts - Zod schemas for request/response
  • src/api/projection/projectionService.ts - Data loading, Prisma-to-engine transformation, reference data loading
  • src/api/projection/projectionController.ts - Request handlers
  • src/api/projection/projectionRouter.ts - Express routes + OpenAPI registry
  • src/engine/__tests__/monteCarlo.test.ts - MC engine unit tests
  • src/api/projection/__tests__/projectionService.test.ts - Projection data transformation + edge case tests

Modified files

  • src/engine/index.ts - Export runMonteCarloSimulation
  • src/server.ts - Mount projection router
  • src/api-docs/openAPIDocumentGenerator.ts - Register projection registry

Test plan

  • 17 new unit tests covering empty plans, minimal plans, edge cases, and Monte Carlo output structure
  • All 228 tests pass
  • pnpm build succeeds (prisma generate + tsc + tsup)
  • Biome lint/format clean
  • cspell spelling clean
  • curl test against running dev server with sample plan data
  • Verify Swagger UI shows new Projection endpoints

Wire the projection engine to REST endpoints that load all plan data,
transform Prisma models to engine input types, and return chart-ready
results for the frontend dashboard.

- GET /api/plans/:planId/projection - deterministic 36-year projection
- GET /api/plans/:planId/projection/monte-carlo - 1000-iteration MC sim
- Monte Carlo engine (Box-Muller normal distribution on return rates)
- Graceful handling of empty/partial plan data
- OpenAPI/Swagger documentation registered
- Unit tests for empty plan, minimal plan, edge cases, and MC output
@ncamaa ncamaa merged commit 7ce78ca into main Apr 12, 2026
5 checks passed
@ncamaa ncamaa deleted the feature/CP-27-dashboard-projection-api branch April 12, 2026 10:46
ncamaa added a commit that referenced this pull request Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant