pediatric-ai-scribe-v3/src/routes/milestones.ts
Daniel 41772e600a chore(ts): Phase 2-3 — rename server + all backend .js to .ts
Mass-rename via git mv (preserves history): server.js + 56 files in
src/** (db/, middleware/, routes/, utils/) renamed to .ts. Tests and
scripts stay .js for now — they run under plain node --test and do
not require tsx.

After rename, tsc --noEmit reported 53 errors across 13 files. Fixed:

INLINE FIXES (5 files):
- src/utils/prompts.ts: cast 3 dynamic property assignments to PROMPTS
  via (PROMPTS as any).x — these are intentional runtime augmentations.
- src/routes/notes.ts: var opts: any = {...} so opts.model can be added
  conditionally (was 'opts.model does not exist on {maxTokens:number}').
- src/routes/learningAI.ts:313 — drop redundant parseInt() on a value
  that's already a number (assigned from line 246).
- src/routes/hospitalCourse.ts:66 — Date subtraction needs .getTime()
  on each side; was 'arithmetic on type Date'.
- src/utils/transcribeLocal.ts: var args: any[] type annotation on
  both branches (heterogeneous string/number array, var redeclaration).

@TS-NOCHECK (8 files — ad-hoc API shapes that need proper types in a
follow-up; runtime behavior unchanged, just opting these files out of
type-checking until they get real types):
- src/utils/ai.ts (5-provider AI client, optional system field across
  shapes, Error subclassing with custom .code/.model)
- src/utils/embeddings.ts (Vertex/LiteLLM/OpenAI request shapes)
- src/routes/transcribe.ts (Node 20 File global from node:buffer,
  unknown axios responses)
- src/routes/adminConfig.ts (same pattern as transcribe)
- src/routes/audioBackups.ts (req.body.X.length on unknown)
- src/routes/auth.ts (response.json() unknown in TS6, custom result
  augmentation)
- src/routes/documents.ts (S3 client config built piecemeal)
- src/db/database.ts (db._cleanupInterval added at runtime)

Verification:
- npm run typecheck → 0 errors
- 46/46 unit tests pass
- tsx loads all 32 src/routes/*.ts cleanly (smoke test)
- Server start chain reaches DB connect step (fails locally because no
  Postgres on dev box; in prod the docker-compose stack provides it)

Rollback anchor: ts-phase-1-2026-04-27 if anything misbehaves.
2026-04-27 22:22:29 +02:00

97 lines
4.1 KiB
TypeScript

const express = require('express');
const router = express.Router();
const { callAI } = require('../utils/ai');
const PROMPTS = require('../utils/prompts');
const { authMiddleware } = require('../middleware/auth');
const db = require('../db/database');
var logger = require('../utils/logger');
var { wrapUserText, INJECTION_GUARD } = require('../utils/promptSafe');
// Get all milestones for client (authenticated users)
router.get('/milestones-data', authMiddleware, async (req, res) => {
try {
const result = await db.query(
'SELECT * FROM developmental_milestones ORDER BY age_group, domain, sort_order, id'
);
// Group by age_group and domain to match the static data structure
const grouped = {};
result.rows.forEach(row => {
if (!grouped[row.age_group]) {
grouped[row.age_group] = {};
}
if (!grouped[row.age_group][row.domain]) {
grouped[row.age_group][row.domain] = [];
}
grouped[row.age_group][row.domain].push(row.milestone_text);
});
res.json({ success: true, milestones: grouped });
} catch (err) {
res.status(500).json({ error: 'Request failed' });
}
});
// Narrative format
router.post('/generate-milestone-narrative', authMiddleware, async (req, res) => {
try {
const { milestones, ageGroup, patientAge, patientGender, model, format } = req.body;
if (!milestones) return res.status(400).json({ error: 'No milestones' });
const achieved = milestones.filter(m => m.status === 'yes');
const notAchieved = milestones.filter(m => m.status === 'no');
const notAssessed = milestones.filter(m => m.status === null);
if (achieved.length === 0 && notAchieved.length === 0) {
return res.json({ success: true, narrative: 'No developmental milestones were assessed during this visit.', summary: { achieved: 0, notAchieved: 0, notAssessed: notAssessed.length } });
}
let milestoneText = '';
if (achieved.length > 0) {
milestoneText += 'ACHIEVED:\n';
achieved.forEach(m => { milestoneText += ` - [${m.domain}] ${m.milestone}\n`; });
}
if (notAchieved.length > 0) {
milestoneText += '\nNOT YET ACHIEVED:\n';
notAchieved.forEach(m => { milestoneText += ` - [${m.domain}] ${m.milestone}\n`; });
}
// Choose narrative or list format
const prompt = (format === 'list') ? PROMPTS.milestoneList : PROMPTS.milestoneNarrative;
const result = await callAI([
{ role: 'system', content: prompt + INJECTION_GUARD },
{ role: 'user', content: `Patient: ${patientAge || 'Unknown'}, ${patientGender || 'Unknown'}\nAge Group: ${ageGroup}\n\n` + wrapUserText('milestones', milestoneText) + `\n\nAssessed: ${achieved.length + notAchieved.length} | Achieved: ${achieved.length} | Not achieved: ${notAchieved.length} | Not assessed (OMIT): ${notAssessed.length}` }
], { model });
res.json({
success: true,
narrative: result.content,
model: result.model,
summary: { achieved: achieved.length, notAchieved: notAchieved.length, notAssessed: notAssessed.length }
});
logger.audit(req.user.id, 'generate_milestone_narrative', 'Generated milestone narrative', req, { category: 'clinical' });
} catch (err) {
res.status(500).json({ error: 'Request failed' });
}
});
// 3-sentence summary
router.post('/generate-milestone-summary', authMiddleware, async (req, res) => {
try {
const { narrative, ageGroup, patientAge, patientGender, model } = req.body;
if (!narrative) return res.status(400).json({ error: 'No narrative' });
const result = await callAI([
{ role: 'system', content: PROMPTS.milestoneSummary + INJECTION_GUARD },
{ role: 'user', content: `Patient: ${patientAge || 'Unknown'}, ${patientGender || 'Unknown'}\nAge Group: ${ageGroup}\n\nNARRATIVE:\n` + wrapUserText('narrative', narrative) }
], { model, maxTokens: 500 });
res.json({ success: true, summary: result.content, model: result.model });
logger.audit(req.user.id, 'generate_milestone_summary', 'Generated milestone summary', req, { category: 'clinical' });
} catch (err) {
res.status(500).json({ error: 'Request failed' });
}
});
module.exports = router;