Actualizar server.js
This commit is contained in:
@@ -930,7 +930,7 @@ app.get("/services/active", authMiddleware, async (req, res) => {
|
|||||||
} catch (e) { res.status(500).json({ ok: false }); }
|
} catch (e) { res.status(500).json({ ok: false }); }
|
||||||
});
|
});
|
||||||
|
|
||||||
// AÑADIDO: Ruta para fijar la cita o el estado operativo (CORREGIDA Y BLINDADA)
|
// AÑADIDO: Ruta para fijar la cita o el estado operativo (REGLA ESTRICTA)
|
||||||
app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => {
|
app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
@@ -939,17 +939,30 @@ app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => {
|
|||||||
const current = await pool.query('SELECT raw_data FROM scraped_services WHERE id = $1 AND owner_id = $2', [id, req.user.accountId]);
|
const current = await pool.query('SELECT raw_data FROM scraped_services WHERE id = $1 AND owner_id = $2', [id, req.user.accountId]);
|
||||||
if (current.rowCount === 0) return res.status(404).json({ ok: false, error: 'No encontrado' });
|
if (current.rowCount === 0) return res.status(404).json({ ok: false, error: 'No encontrado' });
|
||||||
|
|
||||||
const updatedRawData = { ...current.rows[0].raw_data, ...extra, "scheduled_date": date || "", "scheduled_time": time || "", "status_operativo": status_operativo };
|
const oldDate = current.rows[0].raw_data.scheduled_date || "";
|
||||||
|
const oldTime = current.rows[0].raw_data.scheduled_time || "";
|
||||||
|
const newDate = date || "";
|
||||||
|
const newTime = time || "";
|
||||||
|
|
||||||
|
const updatedRawData = { ...current.rows[0].raw_data, ...extra, "scheduled_date": newDate, "scheduled_time": newTime, "status_operativo": status_operativo };
|
||||||
|
|
||||||
await pool.query('UPDATE scraped_services SET raw_data = $1 WHERE id = $2 AND owner_id = $3', [JSON.stringify(updatedRawData), id, req.user.accountId]);
|
await pool.query('UPDATE scraped_services SET raw_data = $1 WHERE id = $2 AND owner_id = $3', [JSON.stringify(updatedRawData), id, req.user.accountId]);
|
||||||
|
|
||||||
// --- 🕵️♂️ EL MOTOR ESCUCHA EL CAMBIO DE ESTADO ---
|
|
||||||
const statusQ = await pool.query("SELECT name FROM service_statuses WHERE id=$1", [status_operativo]);
|
const statusQ = await pool.query("SELECT name FROM service_statuses WHERE id=$1", [status_operativo]);
|
||||||
const stName = statusQ.rows[0]?.name.toLowerCase() || "";
|
const stName = statusQ.rows[0]?.name.toLowerCase() || "";
|
||||||
|
|
||||||
if (stName.includes('citado') && date) {
|
// REGLA ESTRICTA: ¿Es estado Citado y tiene fecha?
|
||||||
|
if (stName.includes('citado') && newDate !== "") {
|
||||||
|
if (oldDate === "") {
|
||||||
|
// No tenía fecha antes -> Dispara "Cita Creada"
|
||||||
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_date');
|
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_date');
|
||||||
} else if (stName.includes('camino')) {
|
} else if (oldDate !== newDate || oldTime !== newTime) {
|
||||||
|
// Ya tenía fecha y se ha cambiado -> Dispara "Modificación"
|
||||||
|
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_update');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Otras reglas
|
||||||
|
else if (stName.includes('camino')) {
|
||||||
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_onway');
|
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_onway');
|
||||||
} else if (stName.includes('finalizado') || stName.includes('terminado')) {
|
} else if (stName.includes('finalizado') || stName.includes('terminado')) {
|
||||||
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_survey');
|
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_survey');
|
||||||
Reference in New Issue
Block a user