Some checks failed
build-and-deploy / build-and-deploy (push) Failing after 1m47s
- 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
97 lines
2.4 KiB
TypeScript
97 lines
2.4 KiB
TypeScript
import { createOperacion } from '~/server/utils/queries'
|
|
|
|
/**
|
|
* POST /api/operaciones
|
|
* Crea una nueva operación con sus lotes de entrada y salida
|
|
*
|
|
* Body:
|
|
* {
|
|
* tipo: string, // ingreso, despulpado, oreado, etc.
|
|
* fecha?: string, // ISO date (opcional, default: ahora)
|
|
* lugar_id?: number,
|
|
* meta?: object,
|
|
* inputs: [ // Lotes de entrada
|
|
* { lote_id: string, cantidad_kg?: number }
|
|
* ],
|
|
* outputs: [ // Lotes de salida (se crearán)
|
|
* { codigo?: string, tipo: string, cantidad_kg?: number, meta?: object }
|
|
* ]
|
|
* }
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
const body = await readBody(event)
|
|
|
|
// Validaciones básicas
|
|
if (!body.tipo) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'El campo "tipo" es requerido',
|
|
})
|
|
}
|
|
|
|
if (!body.inputs || !Array.isArray(body.inputs)) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'El campo "inputs" es requerido y debe ser un array',
|
|
})
|
|
}
|
|
|
|
if (!body.outputs || !Array.isArray(body.outputs)) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'El campo "outputs" es requerido y debe ser un array',
|
|
})
|
|
}
|
|
|
|
// Validar que cada input tenga lote_id
|
|
for (const input of body.inputs) {
|
|
if (!input.lote_id) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Cada input debe tener un "lote_id"',
|
|
})
|
|
}
|
|
}
|
|
|
|
// Validar que cada output tenga tipo
|
|
for (const output of body.outputs) {
|
|
if (!output.tipo) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Cada output debe tener un "tipo"',
|
|
})
|
|
}
|
|
}
|
|
|
|
const result = await createOperacion({
|
|
tipo: body.tipo,
|
|
fecha: body.fecha ? new Date(body.fecha) : undefined,
|
|
lugar_id: body.lugar_id,
|
|
meta: body.meta,
|
|
inputs: body.inputs,
|
|
outputs: body.outputs,
|
|
})
|
|
|
|
return {
|
|
success: true,
|
|
data: {
|
|
operacion: result.operacion,
|
|
lotes_creados: result.lotes_creados,
|
|
},
|
|
}
|
|
} catch (error: any) {
|
|
console.error('Error creando operación:', error)
|
|
|
|
if (error.statusCode) {
|
|
throw error
|
|
}
|
|
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Error creando operación',
|
|
data: { message: error.message },
|
|
})
|
|
}
|
|
})
|