Actualizar server.js
This commit is contained in:
33
server.js
33
server.js
@@ -1731,7 +1731,8 @@ app.post("/providers/automate/:id", authMiddleware, async (req, res) => {
|
|||||||
|
|
||||||
app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||||
const { id } = req.params;
|
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 {
|
try {
|
||||||
if (automation_status) {
|
if (automation_status) {
|
||||||
@@ -1745,18 +1746,17 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
|||||||
let rawActual = current.rows[0].raw_data || {};
|
let rawActual = current.rows[0].raw_data || {};
|
||||||
|
|
||||||
let oldStatus = String(rawActual.status_operativo || "");
|
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 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 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 statusChanged = (newStatus !== oldStatus && newStatus !== "" && newStatus !== "null");
|
||||||
const dateChanged = (newDate !== oldDate && newDate !== "" && newDate !== "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;
|
const oldWorkerId = current.rows[0].assigned_to || rawActual.assigned_to;
|
||||||
let finalAssignedTo = assigned_to !== undefined ? (assigned_to === "" ? null : assigned_to) : oldWorkerId;
|
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}`);
|
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 = {
|
const updatedRawData = {
|
||||||
...rawActual, ...extra,
|
...rawActual, ...extra,
|
||||||
"Nombre Cliente": name || rawActual["Nombre Cliente"],
|
"Nombre Cliente": name !== undefined ? name : rawActual["Nombre Cliente"],
|
||||||
"Teléfono": phone || rawActual["Teléfono"],
|
"Teléfono": phone !== undefined ? phone : rawActual["Teléfono"],
|
||||||
"Dirección": address || rawActual["Dirección"],
|
"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_date": newDate,
|
||||||
"scheduled_time": newTime,
|
"scheduled_time": newTime,
|
||||||
"status_operativo": newStatus === "null" ? null : newStatus
|
"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(
|
await pool.query(
|
||||||
`UPDATE scraped_services SET raw_data = $1, assigned_to = $2 WHERE id = $3 AND owner_id = $4`,
|
`UPDATE scraped_services SET raw_data = $1, assigned_to = $2, is_urgent = $3 WHERE id = $4 AND owner_id = $5`,
|
||||||
[JSON.stringify(updatedRawData), finalAssignedTo, id, req.user.accountId]
|
[JSON.stringify(updatedRawData), finalAssignedTo, isUrgentFinal, id, req.user.accountId]
|
||||||
);
|
);
|
||||||
|
|
||||||
// BANDERAS INTELIGENTES
|
// BANDERAS INTELIGENTES
|
||||||
|
|||||||
Reference in New Issue
Block a user