Actualizar server.js

This commit is contained in:
2026-03-14 16:12:00 +00:00
parent ac47ad135d
commit 3ff7788667

View File

@@ -1731,7 +1731,8 @@ app.post("/providers/automate/:id", authMiddleware, async (req, res) => {
app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
const { id } = req.params;
let { automation_status, status, name, phone, address, cp, description, guild_id, assigned_to, assigned_to_name, internal_notes, client_notes, is_urgent, ...extra } = req.body;
// Extraemos todos los datos que vienen del frontend
let { automation_status, status, name, phone, address, cp, description, guild_id, assigned_to, assigned_to_name, internal_notes, client_notes, is_urgent, scheduled_date, scheduled_time, status_operativo, ...extra } = req.body;
try {
if (automation_status) {
@@ -1745,18 +1746,17 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
let rawActual = current.rows[0].raw_data || {};
let oldStatus = String(rawActual.status_operativo || "");
let newStatus = String(extra.status_operativo !== undefined ? extra.status_operativo : oldStatus);
let newStatus = String(status_operativo !== undefined ? status_operativo : oldStatus);
// 🕒 AÑADIDO: Control exhaustivo de Fechas Y Horas
const oldDate = String(rawActual.scheduled_date || "");
const newDate = String(extra.scheduled_date !== undefined ? extra.scheduled_date : oldDate);
const newDate = String(scheduled_date !== undefined ? scheduled_date : oldDate);
const oldTime = String(rawActual.scheduled_time || "");
const newTime = String(extra.scheduled_time !== undefined ? extra.scheduled_time : oldTime);
const newTime = String(scheduled_time !== undefined ? scheduled_time : oldTime);
const statusChanged = (newStatus !== oldStatus && newStatus !== "" && newStatus !== "null");
const dateChanged = (newDate !== oldDate && newDate !== "" && newDate !== "null");
const timeChanged = (newTime !== oldTime && newTime !== "" && newTime !== "null"); // <-- NUEVO
const timeChanged = (newTime !== oldTime && newTime !== "" && newTime !== "null");
const oldWorkerId = current.rows[0].assigned_to || rawActual.assigned_to;
let finalAssignedTo = assigned_to !== undefined ? (assigned_to === "" ? null : assigned_to) : oldWorkerId;
@@ -1769,20 +1769,29 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
console.log(`🤖 [DEBUG ADMIN-PANEL] Exp: ${id} | Estado: '${stName}' | statusChanged: ${statusChanged} | dateChanged: ${dateChanged} | timeChanged: ${timeChanged}`);
// ACTUALIZAR BASE DE DATOS PRIMERO
// 🚨 AQUÍ ESTABA EL BUG: AHORA RE-INYECTAMOS TODOS LOS CAMPOS AL JSON 🚨
const updatedRawData = {
...rawActual, ...extra,
"Nombre Cliente": name || rawActual["Nombre Cliente"],
"Teléfono": phone || rawActual["Teléfono"],
"Dirección": address || rawActual["Dirección"],
"Nombre Cliente": name !== undefined ? name : rawActual["Nombre Cliente"],
"Teléfono": phone !== undefined ? phone : rawActual["Teléfono"],
"Dirección": address !== undefined ? address : rawActual["Dirección"],
"Código Postal": cp !== undefined ? cp : (rawActual["Código Postal"] || rawActual["C.P."]),
"Descripción": description !== undefined ? description : rawActual["Descripción"],
"guild_id": guild_id !== undefined ? guild_id : rawActual.guild_id,
"internal_notes": internal_notes !== undefined ? internal_notes : rawActual.internal_notes,
"client_notes": client_notes !== undefined ? client_notes : rawActual.client_notes,
"assigned_to_name": assigned_to_name !== undefined ? assigned_to_name : rawActual.assigned_to_name,
"scheduled_date": newDate,
"scheduled_time": newTime,
"status_operativo": newStatus === "null" ? null : newStatus
};
// Rescatamos si es urgente (es una columna física en tu base de datos)
const isUrgentFinal = is_urgent !== undefined ? is_urgent : current.rows[0].is_urgent;
await pool.query(
`UPDATE scraped_services SET raw_data = $1, assigned_to = $2 WHERE id = $3 AND owner_id = $4`,
[JSON.stringify(updatedRawData), finalAssignedTo, id, req.user.accountId]
`UPDATE scraped_services SET raw_data = $1, assigned_to = $2, is_urgent = $3 WHERE id = $4 AND owner_id = $5`,
[JSON.stringify(updatedRawData), finalAssignedTo, isUrgentFinal, id, req.user.accountId]
);
// BANDERAS INTELIGENTES