Actualizar server.js
This commit is contained in:
21
server.js
21
server.js
@@ -412,6 +412,21 @@ async function autoUpdateDB() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HELPERS
|
// HELPERS
|
||||||
|
|
||||||
|
// HELPERS
|
||||||
|
function extractValidPhone(rawPhone) {
|
||||||
|
if (!rawPhone) return "";
|
||||||
|
let str = String(rawPhone).toUpperCase();
|
||||||
|
str = str.replace(/(^\+34|^0034)/, ""); // Quita prefijo inicial si lo hay
|
||||||
|
str = str.replace(/[\s\-\.]/g, ""); // Quita espacios, guiones y puntos
|
||||||
|
const match = str.match(/[6789]\d{8}/); // Busca el primer número de 9 cifras que empiece por 6,7,8,9
|
||||||
|
if (match) return match[0];
|
||||||
|
const digits = str.replace(/\D/g, ""); // Si falla, coge los primeros 9 números que pille
|
||||||
|
return digits.length >= 9 ? digits.substring(0, 9) : digits;
|
||||||
|
}
|
||||||
|
function normalizePhone(phone) { let p = String(phone || "").trim().replace(/\s+/g, "").replace(/-/g, ""); if (!p) return ""; if (!p.startsWith("+") && /^[6789]\d{8}/.test(p)) return "+34" + p; return p; }
|
||||||
|
|
||||||
|
|
||||||
function normalizePhone(phone) { let p = String(phone || "").trim().replace(/\s+/g, "").replace(/-/g, ""); if (!p) return ""; if (!p.startsWith("+") && /^[6789]\d{8}/.test(p)) return "+34" + p; return p; }
|
function normalizePhone(phone) { let p = String(phone || "").trim().replace(/\s+/g, "").replace(/-/g, ""); if (!p) return ""; if (!p.startsWith("+") && /^[6789]\d{8}/.test(p)) return "+34" + p; return p; }
|
||||||
function signToken(user) { const accountId = user.owner_id || user.id; return jwt.sign({ sub: user.id, email: user.email, phone: user.phone, role: user.role || 'operario', accountId }, JWT_SECRET, { expiresIn: "30d" }); }
|
function signToken(user) { const accountId = user.owner_id || user.id; return jwt.sign({ sub: user.id, email: user.email, phone: user.phone, role: user.role || 'operario', accountId }, JWT_SECRET, { expiresIn: "30d" }); }
|
||||||
function authMiddleware(req, res, next) { const h = req.headers.authorization || ""; const token = h.startsWith("Bearer ") ? h.slice(7) : ""; if (!token) return res.status(401).json({ ok: false, error: "No token" }); try { req.user = jwt.verify(token, JWT_SECRET); next(); } catch { return res.status(401).json({ ok: false, error: "Token inválido" }); } }
|
function authMiddleware(req, res, next) { const h = req.headers.authorization || ""; const token = h.startsWith("Bearer ") ? h.slice(7) : ""; if (!token) return res.status(401).json({ ok: false, error: "No token" }); try { req.user = jwt.verify(token, JWT_SECRET); next(); } catch { return res.status(401).json({ ok: false, error: "Token inválido" }); } }
|
||||||
@@ -1360,8 +1375,7 @@ async function triggerWhatsAppEvent(ownerId, serviceId, eventType) {
|
|||||||
|
|
||||||
// 1. EXTRAER TELÉFONO DEL CLIENTE LIMPIO
|
// 1. EXTRAER TELÉFONO DEL CLIENTE LIMPIO
|
||||||
let rawPhone = raw["Teléfono"] || raw["TELEFONO"] || raw["TELEFONOS"] || "";
|
let rawPhone = raw["Teléfono"] || raw["TELEFONO"] || raw["TELEFONOS"] || "";
|
||||||
let cleanPhoneToMatch = String(rawPhone).replace(/\D/g, "");
|
let cleanPhoneToMatch = extractValidPhone(rawPhone);
|
||||||
if (cleanPhoneToMatch.length > 9) cleanPhoneToMatch = cleanPhoneToMatch.slice(-9);
|
|
||||||
if (cleanPhoneToMatch.length < 9) return false; // Si no hay teléfono válido, cancelamos
|
if (cleanPhoneToMatch.length < 9) return false; // Si no hay teléfono válido, cancelamos
|
||||||
|
|
||||||
const finalPhoneToSend = "34" + cleanPhoneToMatch;
|
const finalPhoneToSend = "34" + cleanPhoneToMatch;
|
||||||
@@ -1927,8 +1941,7 @@ app.post("/services/not-found/:id", authMiddleware, async (req, res) => {
|
|||||||
|
|
||||||
// Intentamos enviar el WhatsApp usando la plantilla
|
// Intentamos enviar el WhatsApp usando la plantilla
|
||||||
let rawPhone = raw["Teléfono"] || raw["TELEFONOS"] || raw["TELEFONO"] || "";
|
let rawPhone = raw["Teléfono"] || raw["TELEFONOS"] || raw["TELEFONO"] || "";
|
||||||
let cleanPhoneToMatch = String(rawPhone).replace(/\D/g, "");
|
let cleanPhoneToMatch = extractValidPhone(rawPhone);
|
||||||
if (cleanPhoneToMatch.length > 9) cleanPhoneToMatch = cleanPhoneToMatch.slice(-9);
|
|
||||||
|
|
||||||
if (cleanPhoneToMatch.length >= 9) { // Solo intentamos enviar si hay un número válido
|
if (cleanPhoneToMatch.length >= 9) { // Solo intentamos enviar si hay un número válido
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user