55 lines
2.9 KiB
TypeScript
55 lines
2.9 KiB
TypeScript
// 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,18})$/;
|
|
/** 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;
|
|
}
|