import { renderPdf, PdfDefinition } from './pdf-engine'; import { COLORS, institutionalDocument, loadLogo } from './pdf-template'; import { displayText, exportColumns } from './tracking-export-table'; import { TrackingSnapshot, ventanaConsultada } from './tracking-export.models'; export const TRACKING_TITLE = 'Cáncer de mama'; const rule = { hLineWidth: () => 0.5, vLineWidth: () => 0, hLineColor: () => COLORS.rule, paddingTop: () => 4, paddingBottom: () => 4 }; const zebra = { ...rule, fillColor: (row: number) => (row === 0 ? undefined : row % 2 === 0 ? COLORS.band : undefined) }; /** What the query was: cut-off, window, each announced filter with its chosen option, and how many rows follow. */ function parameters(snapshot: TrackingSnapshot) { const rows: unknown[][] = [ ...(snapshot.seleccion ? [['Selección del análisis', snapshot.seleccion]] : []), ['Fecha de corte', snapshot.corte], ['Ventana consultada', ventanaConsultada(snapshot)], ...snapshot.filtros_disponibles.map(f => [f.etiqueta, f.opciones.find(o => o.codigo === snapshot.filtros[f.clave])?.etiqueta ?? 'Todos']), ['Registros', String(snapshot.items.length)], ]; return { table: { widths: [130, '*'], body: rows.map(([k, v]) => [{ text: k, bold: true }, v]) }, layout: rule }; } /** The follow-up table: the columns the answer announced, each value with its labelled details under it. */ function dataTable(snapshot: TrackingSnapshot) { const columns = exportColumns(snapshot); const cell = (column: (typeof columns)[number], item: Record) => { const value = item[column.key]; const parts: unknown[] = [value === null || value === undefined ? { text: displayText(value), italics: true, color: COLORS.muted } : { text: String(value) }]; for (const detail of column.details) { const v = item[detail.key]; if (v !== null && v !== undefined) parts.push({ text: `${detail.label}: ${v}`, style: 'small' }); } return parts.length === 1 ? parts[0] : { stack: parts }; }; return { table: { headerRows: 1, dontBreakRows: true, widths: columns.map((_, i) => (i === 0 ? 62 : '*')), body: [ columns.map(c => ({ text: c.label, style: 'th' })), ...snapshot.items.map(item => columns.map(c => cell(c, item))), ], }, layout: zebra, }; } /** The document definition of a follow-up report. Exported so its content can be checked without rendering. */ export function trackingDocument(snapshot: TrackingSnapshot, now: Date, logo?: string): PdfDefinition { return institutionalDocument({ title: TRACKING_TITLE, logo, now, orientation: 'landscape' }, [ { text: 'Resultados de seguimiento', style: 'h1' }, parameters(snapshot), { text: 'Pacientes', style: 'h2' }, snapshot.items.length ? dataTable(snapshot) : { text: 'No hay registros para los filtros aplicados.', style: 'muted' }, { text: '“No disponible” indica dato no registrado, no ausencia clínica.', style: 'small', margin: [0, 10, 0, 0] }, ]); } /** A PDF with the follow-up rows over several pages, repeating the header row. */ export async function buildTrackingPdf(snapshot: TrackingSnapshot, now = new Date(), logo?: string): Promise { return renderPdf(trackingDocument(snapshot, now, logo ?? await loadLogo())); }