feat(seguimiento): list opened on a group of the cohort, with its window, cut-off, cohort and revision

The follow-up list of breast cancer can now be opened on a selection that the analysis names: the
whole cohort (window 0, offered only while the selection is active), the cut-off the analysis was
computed at, the group's filter, and the cohort with its revision, which the server requires and
repeats.

- A cohort's list quotes `cohorte`, `escenario` and `revision` (not filters) on every page and in the
  export; an answer that loses a filter, changes the window or the cut-off, or is another cohort or
  revision is not drawn and cannot be exported.
- A new selection cancels the previous request; while it loads, and when it fails (403, 409, 503),
  no list of the previous query stays on screen.
- Inside the cohort the form refines the same cohort and says so; "Quitar selección" leaves it.
- "Volver al análisis" and a note when the list holds a different number of patients than the
  analysis counted.
- PDF and XLSX state the selection, the cut-off and "Toda la cohorte"; the file name tells a
  selection apart; an export in progress is cancelled, and a finished one discarded, when the query
  on screen changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Angel Capetillo Zavala
2026-11-13 14:07:31 -06:00
parent ee75e7a2a0
commit eb92b99a1d
13 changed files with 618 additions and 51 deletions
@@ -11,15 +11,20 @@ export interface SeguimientoOpcion { codigo: string; etiqueta: string; }
// `detalles` are shown under the value, each with its label (for example the date of a result).
export interface SeguimientoColumna { clave: string; etiqueta: string; detalles?: { clave: string; etiqueta: string }[]; }
export interface SeguimientoFiltro { clave: string; etiqueta: string; opciones: SeguimientoOpcion[]; }
// What a list of a cohort quotes back so that it opens the cohort that was counted: the cohort, its scenario and the
// revision of its rows. They are not filters; the answer repeats them, and a list that does not is not the one asked for.
export interface ContextoLista { cohorte: string; escenario: string; revision: string; }
export const CLAVES_CONTEXTO = ['cohorte', 'escenario', 'revision'] as const;
export interface SeguimientoTablaQuery {
corte?: string; meses: 1 | 3 | 6 | 12; page: number; page_size: number;
corte?: string; meses: MesesVentana; page: number; page_size: number;
// The filters the list announces in `filtros_disponibles`, by key; an empty value is no filter.
filtros?: Record<string, string | undefined>;
contexto?: ContextoLista;
}
export interface SeguimientoTablaResponse {
export interface SeguimientoTablaResponse extends Partial<ContextoLista> {
simulado: true;
corte: string;
ventana: { meses: 1 | 3 | 6 | 12; desde: string; hasta: string };
ventana: { meses: MesesVentana; desde: string; hasta: string };
filtros: Record<string, string | null>;
filtros_disponibles: SeguimientoFiltro[];
columnas: SeguimientoColumna[];
@@ -29,9 +34,14 @@ export interface SeguimientoTablaResponse {
}
export class SeguimientoInvalido extends Error {}
export const MESES_VENTANA = [1, 3, 6, 12] as const;
// 0 is the whole list, without a time window: what a curve or a heatmap cell of the analysis opens. The list offers
// it only while such a selection is active.
export const MESES_VENTANA = [1, 3, 6, 12, 0] as const;
export type MesesVentana = typeof MESES_VENTANA[number];
/** What the window is called on screen and in the files: the whole cohort or a number of months. */
export const etiquetaVentana = (meses: number) => meses === 0 ? 'Toda la cohorte' : `${meses} ${meses === 1 ? 'mes' : 'meses'}`;
export const PAGE_SIZE_MAX = 200;
const RESERVADAS = ['meses', 'page', 'page_size', 'corte'];
const RESERVADAS = ['meses', 'page', 'page_size', 'corte', ...CLAVES_CONTEXTO];
const record = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
const integer = (v: unknown, min = 0): v is number => typeof v === 'number' && Number.isSafeInteger(v) && v >= min;
@@ -43,7 +53,27 @@ const opcion = (v: unknown) => record(v) && text(v['codigo']) && text(v['etiquet
export function validQuery(q: SeguimientoTablaQuery) {
return (!q.corte || isoDate(q.corte)) && (MESES_VENTANA as readonly number[]).includes(q.meses) && integer(q.page, 1)
&& integer(q.page_size, 1) && q.page_size <= PAGE_SIZE_MAX && Object.keys(q.filtros ?? {}).every(clave);
&& integer(q.page_size, 1) && q.page_size <= PAGE_SIZE_MAX && Object.keys(q.filtros ?? {}).every(clave)
// A cohort's list needs a cut-off: the revision belongs to it.
&& (!q.contexto || (!!q.corte && CLAVES_CONTEXTO.every(k => text(q.contexto![k]))));
}
/**
* Whether an answer says it applied the window and the filters that were asked for (and the cut-off, when one was
* asked, and the cohort, scenario and revision of a cohort's list). A list or a file must not show rows of another
* query under the title of this one.
*/
export function consultaAplicada(query: SeguimientoTablaQuery, answer: SeguimientoTablaResponse): boolean {
const entries = (filters: Record<string, string | null | undefined>) =>
JSON.stringify(Object.entries(filters).filter(([, v]) => !!v).sort(([a], [b]) => a.localeCompare(b)));
return answer.ventana.meses === query.meses && (!query.corte || answer.corte === query.corte)
&& entries(query.filtros ?? {}) === entries(answer.filtros)
&& (!query.contexto || CLAVES_CONTEXTO.every(k => answer[k] === query.contexto![k]));
}
/** The cohort an answer belongs to, when it names one: what the next pages and the files must quote back. */
export function contextoDe(answer: SeguimientoTablaResponse): ContextoLista | undefined {
return answer.cohorte && answer.escenario && answer.revision ? { cohorte: answer.cohorte, escenario: answer.escenario, revision: answer.revision } : undefined;
}
export function seguimientoTablaResponse(v: unknown): SeguimientoTablaResponse {
@@ -54,6 +84,7 @@ export function seguimientoTablaResponse(v: unknown): SeguimientoTablaResponse {
&& Array.isArray(f['opciones']) && f['opciones'].every(opcion)) && new Set(available.map(f => (f as Record<string, unknown>)['clave'])).size === available.length;
const columnasOk = Array.isArray(columns) && columns.length > 0 && columns.every(c => record(c) && text(c['clave']) && text(c['etiqueta'])
&& (c['detalles'] === undefined || (Array.isArray(c['detalles']) && c['detalles'].every(d => record(d) && text(d['clave']) && text(d['etiqueta'])))));
if (CLAVES_CONTEXTO.some(k => v[k] !== undefined && !text(v[k]))) throw new SeguimientoInvalido('Respuesta de seguimiento inválida');
if (!isoDate(v['corte']) || !record(window) || !(MESES_VENTANA as readonly number[]).includes(window['meses'] as number)
|| !isoDate(window['desde']) || !isoDate(window['hasta'])
|| !record(p) || !integer(p['page'], 1) || !integer(p['page_size'], 1) || p['page_size'] > PAGE_SIZE_MAX || !integer(p['total_items']) || !integer(p['total_pages'])
@@ -73,6 +104,7 @@ export class SeguimientoTablaService {
if (!validQuery(query)) throw new SeguimientoInvalido('Parámetros de seguimiento inválidos');
let params = new HttpParams().set('meses', query.meses).set('page', query.page).set('page_size', query.page_size);
if (query.corte) params = params.set('corte', query.corte);
if (query.contexto) for (const k of CLAVES_CONTEXTO) params = params.set(k, query.contexto[k]);
for (const [k, v] of Object.entries(query.filtros ?? {})) if (v) params = params.set(k, v);
return this.http.get<unknown>(`/${requireDominio(dominio)}/seguimiento`, { params }).pipe(map(seguimientoTablaResponse));
}