Fix chart review: prompt selection by top-level type, include per-visit labs

- Bug 1: When user selected "Outpatient" review type but had any subspecialty
  visit cards filled in, the backend ignored the top-level type and switched to
  the subspecialty prompt. Fixed: top-level type dropdown is now definitive.
  Per-visit note types only control data formatting/labeling, not prompt selection.

- Bug 2: Labs entered in a visit card were silently dropped for outpatient and
  subspecialty visits (only ED visit labs were included). Fixed: per-visit labs
  now appear immediately after their visit content, labeled with the visit date.

- Improved lab labeling: visit labs are labeled "Labs from this visit (date)"
  and the separate labs section is labeled "ADDITIONAL LABS (not tied to a
  specific visit)" so the AI clearly distinguishes them.
This commit is contained in:
ifedan-ed 2026-03-30 20:30:07 +00:00
parent 7e22902e47
commit f2730bdc83

View file

@ -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) {