Actualizar server.js
This commit is contained in:
18
server.js
18
server.js
@@ -1822,7 +1822,13 @@ app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => {
|
|||||||
// 2. MAGIA: TAREAS EN SEGUNDO PLANO (El WhatsApp tarda lo que tenga que tardar, sin bloquear)
|
// 2. MAGIA: TAREAS EN SEGUNDO PLANO (El WhatsApp tarda lo que tenga que tardar, sin bloquear)
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
if (stName.includes('asignado')) {
|
// 👇 Detectamos cambios en la fecha y el estado para no hacer disparos falsos
|
||||||
|
const statusChanged = status_operativo !== rawActual.status_operativo;
|
||||||
|
const oldDate = rawActual.scheduled_date || "";
|
||||||
|
const dateChanged = newDate !== "" && newDate !== oldDate;
|
||||||
|
|
||||||
|
if (statusChanged && stName.includes('asignado')) {
|
||||||
|
triggerHomeServeRobot(req.user.accountId, id, 'assign').catch(console.error);
|
||||||
const waEnviadoExito = await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned');
|
const waEnviadoExito = await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned');
|
||||||
if (waEnviadoExito) {
|
if (waEnviadoExito) {
|
||||||
const estadoEsperando = await pool.query("SELECT id FROM service_statuses WHERE owner_id=$1 AND name='Esperando al Cliente' LIMIT 1", [req.user.accountId]);
|
const estadoEsperando = await pool.query("SELECT id FROM service_statuses WHERE owner_id=$1 AND name='Esperando al Cliente' LIMIT 1", [req.user.accountId]);
|
||||||
@@ -1850,12 +1856,16 @@ app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => {
|
|||||||
updatedRawData.assigned_to_name = null;
|
updatedRawData.assigned_to_name = null;
|
||||||
await pool.query('UPDATE scraped_services SET raw_data = $1, assigned_to = null WHERE id = $2 AND owner_id = $3', [JSON.stringify(updatedRawData), id, req.user.accountId]);
|
await pool.query('UPDATE scraped_services SET raw_data = $1, assigned_to = null WHERE id = $2 AND owner_id = $3', [JSON.stringify(updatedRawData), id, req.user.accountId]);
|
||||||
}
|
}
|
||||||
else if (stName.includes('citado') && newDate !== "" && date !== undefined) {
|
|
||||||
|
// 🟢 NUEVO DISPARADOR ROBOT CITA (Se lanza si cambia estado O si cambia fecha)
|
||||||
|
else if ((statusChanged && stName.includes('citado') && newDate !== "") || (dateChanged && stName.includes('citado'))) {
|
||||||
|
|
||||||
triggerHomeServeRobot(req.user.accountId, id, 'date').catch(console.error);
|
triggerHomeServeRobot(req.user.accountId, id, 'date').catch(console.error);
|
||||||
const oldDate = rawActual.scheduled_date || "";
|
|
||||||
if (oldDate === "") await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_date');
|
if (oldDate === "") await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_date');
|
||||||
else if (oldDate !== newDate) await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_update');
|
else if (oldDate !== newDate) await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_update');
|
||||||
} else if (stName.includes('camino')) {
|
}
|
||||||
|
else if (stName.includes('camino')) {
|
||||||
await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_onway');
|
await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_onway');
|
||||||
} else if (stName.includes('finalizado') || stName.includes('terminado')) {
|
} else if (stName.includes('finalizado') || stName.includes('terminado')) {
|
||||||
if (!skip_survey) {
|
if (!skip_survey) {
|
||||||
|
|||||||
Reference in New Issue
Block a user