Identify follow-up patients by pac_id

Rows name their patient with pac_id (a decimal string) or the legacy
id_seudonimo. pac_id wins; an invalid pac_id is never replaced by the
legacy field, and a page that repeats a patient or mixes both fields is
not drawn or exported. The table, patient record, PDF and spreadsheet
follow the announced identifier column and label. CaCu requests risk
only with a pac_id.
This commit is contained in:
Angel Capetillo Zavala
2026-11-13 18:19:27 -06:00
parent caf5f954b7
commit 3b246b4f1f
31 changed files with 392 additions and 89 deletions
@@ -0,0 +1,54 @@
// How a follow-up row says who its patient is: `pac_id`, or the legacy `id_seudonimo` that servers may still send.
/** The fields that can identify a patient on a row, in order of precedence. */
export const CAMPOS_IDENTIDAD = ['pac_id', 'id_seudonimo'] as const;
export type CampoIdentidad = typeof CAMPOS_IDENTIDAD[number];
export interface IdentidadPaciente {
/** The field the identifier came from: `pac_id` wins over the legacy `id_seudonimo`. */
campo: CampoIdentidad;
/** The identifier as sent, scoped to the domain it was read in. `pac_id` is a decimal string. */
id: string;
}
/** What the row's identifier is called on screen and in the files, by the field it comes from. */
export const ETIQUETA_IDENTIDAD: Readonly<Record<CampoIdentidad, string>> = {
pac_id: 'Identificador de paciente',
id_seudonimo: 'Identificador seudónimo',
};
// No leading zeros and no sign: the value is compared and sent as text, so it has one spelling.
const PAC_ID = /^(0|[1-9][0-9]{0,17})$/;
/** The fields of a row that matter here; a typed row or a plain record both fit. */
export interface FilaConIdentidad { readonly pac_id?: unknown; readonly id_seudonimo?: unknown }
const texto = (v: unknown): v is string => typeof v === 'string' && v.length > 0;
/**
* The identity of a row, or `null` when it has none that can be trusted: a `pac_id` that is not a decimal string
* (a number is refused, not converted) or no identifier at all. A present `pac_id` is never replaced by
* `id_seudonimo` when it is invalid.
*/
export function identidadDe(fila: FilaConIdentidad): IdentidadPaciente | null {
const pacId = fila.pac_id;
if (pacId !== undefined && pacId !== null) return typeof pacId === 'string' && PAC_ID.test(pacId) ? { campo: 'pac_id', id: pacId } : null;
return texto(fila.id_seudonimo) ? { campo: 'id_seudonimo', id: fila.id_seudonimo } : null;
}
/**
* The identities of a page of rows, or `null` when the page cannot be shown: a row without identity, rows that do not
* identify their patient by the same field, or two rows of the same patient (they would open the same record).
*/
export function identidadesDe(filas: readonly FilaConIdentidad[]): IdentidadPaciente[] | null {
const identidades = filas.map(identidadDe);
if (identidades.some(i => i === null)) return null;
const validas = identidades as IdentidadPaciente[];
if (validas.some(i => i.campo !== validas[0].campo)) return null;
return new Set(validas.map(i => i.id)).size === validas.length ? validas : null;
}
/** The column that holds the link to the record: the one of the row's own field, else any identifier column, else the first. */
export function columnaEnlace(columnas: readonly { clave: string }[], campo: CampoIdentidad): string | undefined {
return columnas.find(c => c.clave === campo)?.clave
?? columnas.find(c => (CAMPOS_IDENTIDAD as readonly string[]).includes(c.clave))?.clave
?? columnas[0]?.clave;
}