diff --git a/src/routes/chartReview.js b/src/routes/chartReview.js index 8c96e6c..c141a1a 100644 --- a/src/routes/chartReview.js +++ b/src/routes/chartReview.js @@ -19,31 +19,38 @@ router.post('/generate-chart-review', authMiddleware, async (req, res) => { } = req.body; const today = new Date().toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); - let prompt; let clinicalData = `Today's date: ${today}\nPatient: ${patientAge || 'Unknown'}, ${patientGender || 'Unknown'}`; if (pmh) clinicalData += `\nPMH: ${pmh}`; - if (type === 'ed' || (edVisits && edVisits.length > 0)) { + // Use the top-level review type to select the prompt — + // the per-visit types only control data formatting, not the prompt. + let prompt; + if (type === 'ed') { prompt = PROMPTS.chartReviewED; - (edVisits || []).forEach(v => { - clinicalData += `\n\n=== ED VISIT (${v.date}) ===\n${v.content}`; - if (v.labs) clinicalData += `\nLabs: ${v.labs}`; - }); - } else if (type === 'subspecialty' || (subspecialty && subspecialty.length > 0)) { + } else if (type === 'subspecialty') { prompt = PROMPTS.chartReviewSubspecialty; - (subspecialty || []).forEach(s => { - clinicalData += `\n\n=== ${(s.specialty || 'Subspecialty').toUpperCase()} — ${s.specialistName || 'Unknown'} (${s.date}) ===\n${s.content}`; - }); } else { prompt = PROMPTS.chartReviewOutpatient; - (visits || []).forEach(v => { - clinicalData += `\n\n=== ${(v.type || 'Visit').toUpperCase()} (${v.date}) ===\n${v.content}`; - }); } + // Include ALL visit data regardless of per-visit type — + // an outpatient chart review should include subspecialty consults too + (visits || []).forEach(v => { + clinicalData += `\n\n=== OUTPATIENT VISIT (${v.date}) ===\n${v.content}`; + if (v.labs && v.labs.trim()) clinicalData += `\n--- Labs from this visit (${v.date}) ---\n${v.labs}`; + }); + (subspecialty || []).forEach(s => { + clinicalData += `\n\n=== SUBSPECIALTY: ${(s.specialty || 'Subspecialty').toUpperCase()} — ${s.specialistName || 'Unknown'} (${s.date}) ===\n${s.content}`; + if (s.labs && s.labs.trim()) clinicalData += `\n--- Labs from this visit (${s.date}) ---\n${s.labs}`; + }); + (edVisits || []).forEach(v => { + clinicalData += `\n\n=== ED VISIT (${v.date}) ===\n${v.content}`; + if (v.labs && v.labs.trim()) clinicalData += `\n--- Labs from this visit (${v.date}) ---\n${v.labs}`; + }); + if (labs && labs.length > 0) { - clinicalData += '\n\n=== LABS ==='; - labs.forEach(l => { clinicalData += `\n${l.date}: ${l.values}`; }); + clinicalData += '\n\n=== ADDITIONAL LABS (not tied to a specific visit) ==='; + labs.forEach(l => { clinicalData += `\n${l.date ? l.date + ': ' : ''}${l.values}`; }); } if (additionalInstructions) {