Actualizar server.js
This commit is contained in:
@@ -2113,6 +2113,9 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||
const oldWorkerId = current.rows[0].assigned_to || rawActual.assigned_to;
|
||||
let finalAssignedTo = assigned_to !== undefined ? (assigned_to === "" ? null : assigned_to) : oldWorkerId;
|
||||
|
||||
// 🚨 NUEVO: Detectar si le acabamos de asignar un técnico (de la nada a alguien, o de un técnico a otro distinto)
|
||||
const workerNewlyAssigned = finalAssignedTo && (String(finalAssignedTo) !== String(oldWorkerId));
|
||||
|
||||
let stName = "";
|
||||
if (newStatus && newStatus !== "null") {
|
||||
const statusQ = await pool.query("SELECT name FROM service_statuses WHERE id=$1", [newStatus]);
|
||||
@@ -2163,7 +2166,7 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||
const isNoLocalizado = stName.includes('no localizado') || stName.includes('buzon') || stName.includes('contesta');
|
||||
|
||||
// --- DISPARADORES WHATSAPP ---
|
||||
if (statusChanged && isAsignado && finalAssignedTo) {
|
||||
if ((statusChanged && isAsignado && finalAssignedTo) || workerNewlyAssigned) {
|
||||
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned').catch(console.error);
|
||||
}
|
||||
// 🔔 Se dispara si cambia el estado a citado, si cambia el día o si cambia la hora
|
||||
@@ -2177,7 +2180,7 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||
const providerName = checkProvider.rows[0]?.provider;
|
||||
|
||||
if (providerName === 'homeserve') {
|
||||
if (statusChanged && isAsignado && finalAssignedTo) {
|
||||
if ((statusChanged && isAsignado && finalAssignedTo) || workerNewlyAssigned) {
|
||||
console.log("✅ [ADMIN] Disparando robot HS: ASIGNACIÓN");
|
||||
triggerHomeServeRobot(req.user.accountId, id, 'assign').catch(console.error);
|
||||
}
|
||||
@@ -2191,7 +2194,7 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||
}
|
||||
}
|
||||
else if (providerName === 'multiasistencia') {
|
||||
if (statusChanged && isAsignado && finalAssignedTo) {
|
||||
if ((statusChanged && isAsignado && finalAssignedTo) || workerNewlyAssigned) {
|
||||
console.log("✅ [ADMIN] Disparando robot MULTI: ASIGNACIÓN");
|
||||
triggerMultiRobot(req.user.accountId, id, 'assign').catch(console.error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user