refactor: normalizar IDs de WhatsApp eliminando dominio y separador

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
pull/1/head
borja 1 month ago
parent 7308c73a79
commit adad0a0609

@ -1,38 +1,43 @@
/** /**
* Normaliza un ID de WhatsApp. * Normalizes a WhatsApp ID by removing the domain part (@s.whatsapp.net, @g.us)
* - Para usuarios (DMs o @s.whatsapp.net): devuelve solo dígitos. * and any participant identifier (:12).
* - Para grupos (@g.us): devuelve el identificador numérico con guion (ej. 12345-67890). * Handles potential variations like participant IDs in group messages.
* *
* Ejemplos: * Examples:
* - '1234567890@s.whatsapp.net' -> '1234567890' * - '1234567890@s.whatsapp.net' -> '1234567890'
* - '+34 600 123 456' -> '34600123456'
* - '1234567890:12@s.whatsapp.net' -> '1234567890'
* - '1234567890-1234567890@g.us' -> '1234567890-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 { export function normalizeWhatsAppId(id: string | null | undefined): string | null {
if (!id) return null; if (!id) {
const raw = String(id).trim(); return null;
if (!raw) return null; }
// Partes antes de @ y antes de ':' (participante) // Remove domain part (@s.whatsapp.net, @g.us, @broadcast etc.)
const atIdx = raw.indexOf('@'); let normalized = id.split('@')[0];
const beforeAt = atIdx >= 0 ? raw.slice(0, atIdx) : raw;
const core = beforeAt.split(':')[0]; // Handle potential participant format like '1234567890:12' by taking the part before ':'
normalized = normalized.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')) { // Basic validation: should contain alphanumeric characters, possibly hyphens for group IDs
const groupCore = core.replace(/[^0-9-]/g, ''); // Allows simple numbers, group IDs with hyphens, and potentially status_me
// Aceptar formatos tipo "123-456" o "123456" (algunas libs incluyen un único bloque) if (!/^[a-zA-Z0-9_-]+$/.test(normalized)) {
if (!groupCore) return null; console.warn(`[normalizeWhatsAppId] Invalid characters found in WhatsApp ID after normalization: ${id} -> ${normalized}`);
if (!/^\d+(?:-\d+)?$/.test(groupCore)) return null; // Return null for clearly invalid IDs after normalization
return groupCore; return null;
} }
// Usuarios: conservar solo dígitos (eliminar +, espacios, etc.) // Prevent empty strings after normalization
const digits = core.replace(/\D+/g, ''); if (normalized.length === 0) {
if (!digits) return null; console.warn(`[normalizeWhatsAppId] Empty string resulted from normalization: ${id}`);
return digits; return null;
}
return normalized;
} }
/** /**

Loading…
Cancel
Save