primer cambio

This commit is contained in:
beseira13
2026-01-19 12:12:38 -03:00
parent 5f59dba52d
commit 44990f015a
4759 changed files with 588702 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
import defineLogHookProcesos from '../../database/modelos/M_webhoock.js';
// Asegurarnos de que el modelo esté definido una sola vez
let modeloDefined = false;
async function webhoock(req, res, dbcentral) {
try {
const body = req.body;
// Validación rápida inicial
if (!body || typeof body !== 'object') {
return res.status(400).json({ error: "El cuerpo del webhook es inválido" });
}
// Validación de contenido
const keys = Object.keys(body);
if (keys.length === 0) {
return res.status(400).json({ error: "El cuerpo del webhook está vacío" });
}
// Definir el modelo solo si no está definido
if (!modeloDefined) {
await defineLogHookProcesos(dbcentral);
modeloDefined = true;
}
// Validar que IdMnt existe antes de procesar
if (!body.IdMnt) {
return res.status(400).json({
error: "Campo requerido faltante",
message: "El campo IdMnt es obligatorio para procesar el webhook"
});
}
// Verificar si el registro ya existe
const existingRecord = await dbcentral.models.log_hook_procesos.findOne({
where: {
OrderId: body.IdMnt.toString()
}
});
if (existingRecord) {
return res.status(409).json({
error: "Registro duplicado",
message: "Ya existe un registro con este OrderId",
id: existingRecord.id
});
}
// Crear el registro directamente sin sanitización redundante
const result = await dbcentral.models.log_hook_procesos.create({
json: JSON.stringify(body),
OrderId: body.IdMnt.toString(),
NroPaquete: body.NroPaquete || null,
estatus: body.estatus || null,
tipo: body.TipoMnt ? body.TipoMnt.toString() : null
}, {
logging: false, // Desactivar logging SQL
returning: ['id'] // Solo retornar el ID
});
// Respuesta rápida
res.status(201).json({
success: true,
message: 'Webhook almacenado correctamente',
id: result.id
});
} catch (error) {
// Log simplificado del error
console.error("❌ Error al procesar webhook:", error.message);
res.status(500).json({
error: "Error al procesar el webhook",
message: process.env.NODE_ENV === 'development' ? error.message : 'Error interno del servidor'
});
}
}
export { webhoock };