From bfb9f44b697751f206e4d3ec6e282d9732c2e326 Mon Sep 17 00:00:00 2001 From: borja Date: Sat, 6 Sep 2025 13:55:24 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20a=C3=B1ade=20rutas=20Evolution=20pa?= =?UTF-8?q?ra=20obtener=20nombre=20de=20contacto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (openrouter/openai/gpt-5) --- src/services/contacts.ts | 61 ++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/services/contacts.ts b/src/services/contacts.ts index abfe374..56d64ca 100644 --- a/src/services/contacts.ts +++ b/src/services/contacts.ts @@ -79,18 +79,7 @@ export class ContactsService { } } - private static async maybeFetchFromApi(digitsId: string): Promise { - const baseUrl = process.env.EVOLUTION_API_URL; - const instance = process.env.EVOLUTION_API_INSTANCE; - const apikey = process.env.EVOLUTION_API_KEY; - - if (!baseUrl || !instance || !apikey) return null; - - const url = `${baseUrl}/chat/findContacts/${instance}`; - const body = { - where: { id: `${digitsId}@s.whatsapp.net` } - }; - + private static async fetchNameFrom(url: string, body: any, digitsId: string): Promise { try { const res = await fetch(url, { method: 'POST', @@ -99,10 +88,6 @@ export class ContactsService { }); if (!res.ok) { - if (process.env.NODE_ENV !== 'test') { - const txt = await res.text().catch(() => ''); - console.warn('ContactsService.findContacts non-OK:', res.status, txt?.slice(0, 200)); - } return null; } @@ -119,29 +104,49 @@ export class ContactsService { ? [data] : []; - let name: string | null = null; for (const rec of arrayCandidates) { const recId = rec?.id ?? rec?.jid ?? rec?.user ?? rec?.remoteJid ?? rec?.wid ?? rec?.wid?._serialized; if (!recId) continue; const norm = normalizeWhatsAppId(String(recId)); if (!norm || norm !== digitsId) continue; - name = this.extractName(rec); - if (name) break; - } - - if (name) { - this.cache.set(digitsId, { name, expiresAt: this.now() + this.TTL_MS }); - return name; + const name = this.extractName(rec); + if (name) { + this.cache.set(digitsId, { name, expiresAt: this.now() + this.TTL_MS }); + return name; + } } return null; - } catch (e) { - if (process.env.NODE_ENV !== 'test') { - console.warn('ContactsService.findContacts error:', e); - } + } catch { return null; } } + private static async maybeFetchFromApi(digitsId: string): Promise { + const baseUrl = process.env.EVOLUTION_API_URL; + const instance = process.env.EVOLUTION_API_INSTANCE; + const apikey = process.env.EVOLUTION_API_KEY; + + if (!baseUrl || !instance || !apikey) return null; + + const jid = `${digitsId}@s.whatsapp.net`; + const body = { where: { id: jid } }; + + // Probar múltiples rutas conocidas según versión de Evolution + const candidates = [ + `${baseUrl}/chat/findContacts/${instance}`, + `${baseUrl}/contact/findContacts/${instance}`, + `${baseUrl}/contact/find/${instance}`, + `${baseUrl}/chat/find/${instance}`, + ]; + + for (const url of candidates) { + const name = await this.fetchNameFrom(url, body, digitsId); + if (name) return name; + } + + return null; + } + static async getDisplayName(idOrJid: string | null | undefined): Promise { const normalized = normalizeWhatsAppId(idOrJid || ''); if (!normalized) return null;