pediatric-ai-scribe-v3/src
Daniel c1d61f5a72 refactor(ts): day 3 batch 1 — convert 4 small route files
Migrates the 4 smallest and best-understood routes to TypeScript:
  src/routes/logs.ts
  src/routes/userPreferences.ts
  src/routes/sessions.ts
  src/routes/extensions.ts   (11 Playwright tests cover this one)

Pattern established for the remaining 25 routes:

  import express = require('express');            // CJS-style, fully typed
  import type { Request, Response } from 'express';
  const db = require('../db/database');           // stays `any` until Day 4
  const { authMiddleware } = require('../middleware/auth');

  const router = express.Router();
  router.get('/foo', async function (req: Request, res: Response) {
    // req.user is typed via src/types/express.d.ts augmentation
  });

  export = router;                                 // CJS-compatible export

Why `import = require()` instead of `import from`:

Express is imported via the namespace-import syntax so tsc preserves
`require("express")` in the emitted CJS. Using plain ES import would
emit `require("express").default` which doesn't exist on Express'
CommonJS default export. The pattern keeps the compiled dist/*.js
byte-identical to what vanilla Node expects.

Why `const { authMiddleware } = require(...)` for other imports:

The middleware + utils files are still .js and their module.exports
shape isn't fully typed yet (Day 4 work). Using `require` avoids
dragging Day 4 work forward; the destructuring still gives us the
variable name we want.

New file — src/types/express.d.ts:

Augments Express.Request with `user?: AuthUser` and `sessionId?: string`
(attached by authMiddleware). Route handlers now type-check against
`req.user!.id` instead of requiring a runtime cast.

`req.user!` uses non-null assertion because authMiddleware guarantees
user is set for every mounted route; strict mode on Day 5 will keep
the assertion but add a type-guard check inside authMiddleware itself
so it propagates.

Verification

- npm run typecheck → 0 errors
- scripts/lint-references.js → green
- Compiled dist/src/routes/logs.js diffs against the original .js by
  whitespace + var→const only. Behavior preserved.

Nothing deployed. Prod + e2e containers still run the previous image.

Progress: 1/30 (server.ts) + 4/29 routes = 5 of 54 total files migrated.
Remaining batches go in subsequent Day 3 commits.
2026-04-23 19:31:20 +02:00
..
db feat(extensions): personal Pagers & Extensions directory with soft-delete 2026-04-22 18:54:26 +02:00
middleware Enforce server-side LLM model whitelist + scope idle timeout to writes 2026-04-14 05:15:55 +02:00
routes refactor(ts): day 3 batch 1 — convert 4 small route files 2026-04-23 19:31:20 +02:00
types refactor(ts): day 3 batch 1 — convert 4 small route files 2026-04-23 19:31:20 +02:00
utils feat(pe-guide): step-by-step OSCE checklist, Bates/Nelson/Hutchison sourced 2026-04-22 17:15:27 +02:00