Actualizar server.js
This commit is contained in:
85
server.js
85
server.js
@@ -2611,91 +2611,6 @@ app.delete("/budgets/:id", authMiddleware, async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Convertir Presupuesto en Servicio Activo (CON SOPORTE RED INTERNA Y TRAZABILIDAD)
|
|
||||||
app.post("/budgets/:id/convert", authMiddleware, async (req, res) => {
|
|
||||||
try {
|
|
||||||
const { date, time, guild_id, assigned_to, use_automation } = req.body;
|
|
||||||
const bq = await pool.query("SELECT * FROM budgets WHERE id=$1 AND owner_id=$2", [req.params.id, req.user.accountId]);
|
|
||||||
if (bq.rowCount === 0) return res.status(404).json({ok: false});
|
|
||||||
const budget = bq.rows[0];
|
|
||||||
|
|
||||||
const rawData = {
|
|
||||||
"Nombre Cliente": budget.client_name,
|
|
||||||
"Teléfono": budget.client_phone,
|
|
||||||
"Dirección": budget.client_address,
|
|
||||||
"Compañía": "Particular",
|
|
||||||
"Descripción": "PRESUPUESTO ACEPTADO.\n" + budget.items.map(i => `${i.qty}x ${i.concept}`).join("\n"),
|
|
||||||
"guild_id": guild_id || null,
|
|
||||||
"assigned_to": assigned_to || null,
|
|
||||||
"scheduled_date": date || "",
|
|
||||||
"scheduled_time": time || ""
|
|
||||||
};
|
|
||||||
|
|
||||||
const insertSvc = await pool.query(
|
|
||||||
"INSERT INTO scraped_services (owner_id, provider, service_ref, status, automation_status, assigned_to, raw_data) VALUES ($1, 'particular', $2, 'pending', 'manual', $3, $4) RETURNING id",
|
|
||||||
[
|
|
||||||
req.user.accountId,
|
|
||||||
`PRE-${budget.id}`,
|
|
||||||
assigned_to || null,
|
|
||||||
JSON.stringify(rawData)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
const newServiceId = insertSvc.rows[0].id;
|
|
||||||
|
|
||||||
await pool.query("UPDATE budgets SET status='converted' WHERE id=$1", [budget.id]);
|
|
||||||
await pool.query(
|
|
||||||
"INSERT INTO service_financials (scraped_id, amount, payment_method) VALUES ($1, $2, 'Pendiente')",
|
|
||||||
[newServiceId, budget.total]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (use_automation && guild_id) {
|
|
||||||
const cpMatch = budget.client_address ? budget.client_address.match(/\b\d{5}\b/) : null;
|
|
||||||
const cp = cpMatch ? cpMatch[0] : "00000";
|
|
||||||
|
|
||||||
const port = process.env.PORT || 3000;
|
|
||||||
const autoUrl = `http://127.0.0.1:${port}/providers/automate/${newServiceId}`;
|
|
||||||
|
|
||||||
fetch(autoUrl, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': req.headers.authorization
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ guild_id, cp, useDelay: false })
|
|
||||||
}).catch(e => console.error("Error lanzando automatización interna:", e));
|
|
||||||
|
|
||||||
if (budget.client_phone) {
|
|
||||||
const msg = `✅ *PRESUPUESTO ACEPTADO*\n\nHola ${budget.client_name}, confirmamos la aceptación del presupuesto por un total de *${budget.total}€*.\n\nEn breve un técnico se pondrá en contacto contigo para agendar la cita. ¡Gracias por confiar en nosotros!`;
|
|
||||||
sendWhatsAppAuto(budget.client_phone, msg, `cliente_${req.user.accountId}`, false).catch(console.error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (budget.client_phone && date && time) {
|
|
||||||
const [y, m, d] = date.split('-');
|
|
||||||
const msg = `✅ *PRESUPUESTO ACEPTADO*\n\nHola ${budget.client_name}, confirmamos la aceptación del presupuesto por un total de *${budget.total}€*.\n\nEl servicio ha sido agendado para el *${d}/${m}/${y} a las ${time}*. ¡Gracias por confiar en nosotros!`;
|
|
||||||
sendWhatsAppAuto(budget.client_phone, msg, `cliente_${req.user.accountId}`, false).catch(console.error);
|
|
||||||
|
|
||||||
if (assigned_to) {
|
|
||||||
const statusQ = await pool.query("SELECT id FROM service_statuses WHERE owner_id=$1 AND name ILIKE '%asignado%' LIMIT 1", [req.user.accountId]);
|
|
||||||
if (statusQ.rowCount > 0) {
|
|
||||||
rawData.status_operativo = statusQ.rows[0].id;
|
|
||||||
await pool.query("UPDATE scraped_services SET raw_data = $1 WHERE id = $2", [JSON.stringify(rawData), newServiceId]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- INICIO TRAZABILIDAD ---
|
|
||||||
if (typeof registrarMovimiento === "function") {
|
|
||||||
await registrarMovimiento(newServiceId, req.user.sub, "Aviso Creado", `Servicio generado a raíz del presupuesto aceptado #PRE-${budget.id}.`);
|
|
||||||
}
|
|
||||||
// --- FIN TRAZABILIDAD ---
|
|
||||||
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch(e) {
|
|
||||||
console.error("Error convirtiendo presupuesto:", e);
|
|
||||||
res.status(500).json({ok: false});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🕒 EL RELOJ DEL SISTEMA (Ejecutar cada minuto)
|
// 🕒 EL RELOJ DEL SISTEMA (Ejecutar cada minuto)
|
||||||
|
|||||||
Reference in New Issue
Block a user