59 lines
3.1 KiB
TypeScript
59 lines
3.1 KiB
TypeScript
import { isoDate } from '../cacu-seguimiento.models';
|
|
import { HorizonteRiesgoCacu, RiesgoPacienteCacu } from './paciente-riesgo.models';
|
|
|
|
const record = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
const finite = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
|
|
const nullableNumber = (v: unknown) => v === null || finite(v);
|
|
const nullableText = (v: unknown) => v === null || typeof v === 'string';
|
|
const text = (v: unknown) => typeof v === 'string';
|
|
const estado = (v: unknown) => v === 'preliminary' || v === 'final';
|
|
const probability = (v: unknown): v is number => finite(v) && v >= 0 && v <= 1;
|
|
const censuras = ['prevalente', 'incidente', 'izquierda', 'derecha', 'derechaBasal', 'no_informativo'];
|
|
|
|
function utcTimestamp(v: unknown) {
|
|
return typeof v === 'string'
|
|
&& /^\d{4}-\d{2}-\d{2}T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|\+00:00)$/.test(v)
|
|
&& isoDate(v.slice(0, 10)) && Number.isFinite(Date.parse(v));
|
|
}
|
|
|
|
function cohorteValida(v: unknown) {
|
|
return v === null || record(v)
|
|
&& v['url'] === 'cacu/cohorte_vigilancia' && text(v['version']) && isoDate(v['fecha_indice'])
|
|
&& (v['tipo_censura'] === null || censuras.includes(v['tipo_censura'] as string))
|
|
&& nullableNumber(v['c']) && nullableNumber(v['l']) && nullableNumber(v['r'])
|
|
&& nullableText(v['pap_0']) && nullableText(v['hpv_0']);
|
|
}
|
|
|
|
function horizonteValido(v: unknown) {
|
|
if (!record(v) || !finite(v['horizonte_anios']) || !probability(v['probabilidad']) || !estado(v['estado'])) return false;
|
|
for (const key of ['ic_bajo', 'ic_alto']) {
|
|
if (Object.hasOwn(v, key) && v[key] !== null && !probability(v[key])) return false;
|
|
}
|
|
// Missing/null endpoints are preserved. A complete interval must contain the probability.
|
|
return !(probability(v['ic_bajo']) && probability(v['ic_alto']))
|
|
|| v['ic_bajo'] <= v['probabilidad'] && v['probabilidad'] <= v['ic_alto'];
|
|
}
|
|
|
|
function riesgoValido(v: unknown) {
|
|
if (v === null) return true;
|
|
if (!record(v) || !Number.isSafeInteger(v['lote_id']) || !text(v['modelo_nombre']) || !text(v['modelo_version'])
|
|
|| !utcTimestamp(v['evaluado_en']) || !estado(v['estado_lote']) || !Array.isArray(v['horizontes'])) return false;
|
|
const horizontes = v['horizontes'];
|
|
return horizontes.every(horizonteValido)
|
|
&& horizontes.every((h, i) => i === 0 || horizontes[i - 1]['horizonte_anios'] < h['horizonte_anios']);
|
|
}
|
|
|
|
/** Validate at the boundary. Reject real data, coercions and malformed fields; retain additive fields. */
|
|
export function riesgoPacienteResponse(v: unknown): RiesgoPacienteCacu {
|
|
if (!record(v) || v['simulado'] !== true || !cohorteValida(v['cohorte']) || !riesgoValido(v['riesgo'])) {
|
|
throw new Error('Respuesta de riesgo individual DEV inválida');
|
|
}
|
|
return structuredClone(v) as unknown as RiesgoPacienteCacu;
|
|
}
|
|
|
|
/** Presentation helper: an incomplete interval must never become a fabricated range. */
|
|
export function tieneIntervalo(h: HorizonteRiesgoCacu): boolean {
|
|
return probability(h.ic_bajo) && probability(h.ic_alto)
|
|
&& h.ic_bajo <= h.probabilidad && h.probabilidad <= h.ic_alto;
|
|
}
|