/** * GET /api/vinculaciones/por-lote/:loteId * * Obtiene todas las vinculaciones de un lote especĂ­fico. */ import { getVinculacionesByLote } from '../../../utils/queries' export default defineEventHandler(async (event) => { try { const loteId = getRouterParam(event, 'loteId') if (!loteId) { throw createError({ statusCode: 400, statusMessage: 'loteId requerido', }) } const vinculaciones = await getVinculacionesByLote(loteId) // Agrupar por tipo de registro const agrupados = { ingresos: vinculaciones.filter(v => v.tipo_registro === 'ingreso'), carretas: vinculaciones.filter(v => v.tipo_registro === 'carreta'), salidas: vinculaciones.filter(v => v.tipo_registro === 'salida'), rechazos: vinculaciones.filter(v => v.tipo_registro === 'rechazo'), } return { success: true, data: vinculaciones, agrupados, meta: { lote_id: loteId, total: vinculaciones.length, por_tipo: { ingresos: agrupados.ingresos.length, carretas: agrupados.carretas.length, salidas: agrupados.salidas.length, rechazos: agrupados.rechazos.length, }, }, } } catch (error: any) { console.error('[API] Error obteniendo vinculaciones del lote:', error) throw createError({ statusCode: 500, statusMessage: error.message || 'Error obteniendo vinculaciones del lote', }) } })