From adad0a06098a186ae05692d319ee1eed63f97110 Mon Sep 17 00:00:00 2001 From: borja Date: Mon, 29 Sep 2025 11:35:11 +0200 Subject: [PATCH] refactor: normalizar IDs de WhatsApp eliminando dominio y separador Co-authored-by: aider (openrouter/openai/gpt-5) --- src/utils/whatsapp.ts | 61 +++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/utils/whatsapp.ts b/src/utils/whatsapp.ts index 8cbcb28..3422955 100644 --- a/src/utils/whatsapp.ts +++ b/src/utils/whatsapp.ts @@ -1,38 +1,43 @@ /** - * Normaliza un ID de WhatsApp. - * - Para usuarios (DMs o @s.whatsapp.net): devuelve solo dígitos. - * - Para grupos (@g.us): devuelve el identificador numérico con guion (ej. 12345-67890). - * - * Ejemplos: + * Normalizes a WhatsApp ID by removing the domain part (@s.whatsapp.net, @g.us) + * and any participant identifier (:12). + * Handles potential variations like participant IDs in group messages. + * + * Examples: * - '1234567890@s.whatsapp.net' -> '1234567890' - * - '+34 600 123 456' -> '34600123456' - * - '1234567890:12@s.whatsapp.net' -> '1234567890' * - '1234567890-1234567890@g.us' -> '1234567890-1234567890' - * - 'status_me@broadcast' -> null + * - '1234567890:12@s.whatsapp.net' -> '1234567890' (handles participant format) + * - 'status_me@broadcast' -> 'status_me' (handles status broadcast) + * + * @param id The raw WhatsApp ID string. Can be null or undefined. + * @returns The normalized ID string, or null if the input is null/undefined or invalid after normalization. */ export function normalizeWhatsAppId(id: string | null | undefined): string | null { - if (!id) return null; - const raw = String(id).trim(); - if (!raw) return null; - - // Partes antes de @ y antes de ':' (participante) - const atIdx = raw.indexOf('@'); - const beforeAt = atIdx >= 0 ? raw.slice(0, atIdx) : raw; - const core = beforeAt.split(':')[0]; - - // Si es un JID de grupo (en el raw termina en @g.us), conservar dígitos y guion - if (raw.endsWith('@g.us')) { - const groupCore = core.replace(/[^0-9-]/g, ''); - // Aceptar formatos tipo "123-456" o "123456" (algunas libs incluyen un único bloque) - if (!groupCore) return null; - if (!/^\d+(?:-\d+)?$/.test(groupCore)) return null; - return groupCore; + if (!id) { + return null; + } + + // Remove domain part (@s.whatsapp.net, @g.us, @broadcast etc.) + let normalized = id.split('@')[0]; + + // Handle potential participant format like '1234567890:12' by taking the part before ':' + normalized = normalized.split(':')[0]; + + // Basic validation: should contain alphanumeric characters, possibly hyphens for group IDs + // Allows simple numbers, group IDs with hyphens, and potentially status_me + if (!/^[a-zA-Z0-9_-]+$/.test(normalized)) { + console.warn(`[normalizeWhatsAppId] Invalid characters found in WhatsApp ID after normalization: ${id} -> ${normalized}`); + // Return null for clearly invalid IDs after normalization + return null; } - // Usuarios: conservar solo dígitos (eliminar +, espacios, etc.) - const digits = core.replace(/\D+/g, ''); - if (!digits) return null; - return digits; + // Prevent empty strings after normalization + if (normalized.length === 0) { + console.warn(`[normalizeWhatsAppId] Empty string resulted from normalization: ${id}`); + return null; + } + + return normalized; } /**