Files
seguidorDeLotes/nuxt4/server/api/lotes/[id]/trazabilidad.get.ts
josedario87 ee3dffa38e
Some checks failed
build-and-deploy / build-and-deploy (push) Failing after 1m47s
Implementar sistema completo de trazabilidad de lotes
- Agregar PostgreSQL 16 con esquema completo
- Crear API endpoints para lotes y operaciones
- Implementar UI con Nuxt UI (tablas, formularios, trazabilidad)
- Agregar datos de ejemplo del flujo completo
- Documentar sistema en PLAN_TRAZABILIDAD.md
2025-11-21 18:39:04 -06:00

55 lines
1.3 KiB
TypeScript

import { getTrazabilidad, getEstadisticasLote } from '~/server/utils/queries'
/**
* GET /api/lotes/:id/trazabilidad
* Obtiene el historial completo de trazabilidad de un lote
*
* Retorna todos los lotes ancestros hasta llegar a los ingresos iniciales,
* organizado por profundidad (0 = lote actual, n = ancestros más antiguos)
*/
export default defineEventHandler(async (event) => {
try {
const id = getRouterParam(event, 'id')
if (!id) {
throw createError({
statusCode: 400,
statusMessage: 'ID de lote requerido',
})
}
// Obtener trazabilidad completa
const trazabilidad = await getTrazabilidad(id)
if (trazabilidad.length === 0) {
throw createError({
statusCode: 404,
statusMessage: 'Lote no encontrado',
})
}
// Obtener estadísticas
const estadisticas = await getEstadisticasLote(id)
return {
success: true,
data: {
historial: trazabilidad,
estadisticas,
},
}
} catch (error: any) {
console.error('Error obteniendo trazabilidad:', error)
if (error.statusCode) {
throw error
}
throw createError({
statusCode: 500,
statusMessage: 'Error obteniendo trazabilidad',
data: { message: error.message },
})
}
})