Files
motor-de-encuestas/lib/dashboard-utils.ts
2026-06-05 07:18:10 -03:00

51 lines
2.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { DashboardFilters } from './dashboard-types'
/** Convierte filtros a query string para las API routes */
export function filtersToQuery(surveyId: string, filters: DashboardFilters): string {
const p = new URLSearchParams({ survey_id: surveyId })
if (filters.depto) p.set('depto', filters.depto)
if (filters.genero?.length) p.set('genero', filters.genero.join(','))
if (filters.etario?.length) p.set('etario', filters.etario.join(','))
if (filters.modalidad?.length) p.set('modalidad', filters.modalidad.join(','))
return p.toString()
}
/** Parsea query string a objeto DashboardFilters */
export function queryToFilters(searchParams: URLSearchParams): DashboardFilters {
const f: DashboardFilters = {}
if (searchParams.has('depto')) f.depto = searchParams.get('depto')!
const g = searchParams.get('genero')
if (g) f.genero = g.split(',').filter(Boolean)
const e = searchParams.get('etario')
if (e) f.etario = e.split(',').filter(Boolean)
const m = searchParams.get('modalidad')
if (m) f.modalidad = m.split(',').filter(Boolean)
return f
}
/** Normaliza filtros a JSONB-compatible object para las RPC */
export function filtersToRpc(filters: DashboardFilters): Record<string, unknown> {
return {
depto: filters.depto ?? null,
genero: filters.genero ?? [],
etario: filters.etario ?? [],
modalidad: filters.modalidad ?? [],
}
}
export const HORAS_ORDER = ['Menos de 1h','Entre 1 y 3h','Entre 3 y 6h','Más de 6h','Varía mucho']
export const AUSENCIAS_ORDER = ['Nunca','Pocas veces (13 veces al año)','Varias veces (410 veces al año)','Con frecuencia (más de 10 veces al año)']
export const ETARIO_ORDER = ['Menos de 25 años','25 a 34 años','35 a 44 años','45 a 54 años','55 años o más','Prefiero no decir']
export function sortByOrder<T extends Record<string, unknown>>(
arr: T[],
key: keyof T,
order: string[]
): T[] {
return [...arr].sort((a, b) => {
const ia = order.indexOf(a[key] as string)
const ib = order.indexOf(b[key] as string)
return (ia === -1 ? 999 : ia) - (ib === -1 ? 999 : ib)
})
}