From 7ef8afa9b37c5f00b4c92352700f7eb289a3b7cd Mon Sep 17 00:00:00 2001 From: Richards0n Date: Mon, 11 Nov 2024 16:57:04 -0300 Subject: [PATCH] Refactor phone number formatting in ChatwootService to improve parsing logic --- .../chatwoot/services/chatwoot.service.ts | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index adcc9c3c3..9b0c60bf7 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -1970,11 +1970,16 @@ export class ChatwootService { if (body.key.remoteJid.includes('@g.us')) { const participantName = body.pushName; - const rawPhoneNumber = body.key.remoteJid.split('@')[0]; - const formattedPhoneNumber = `+${rawPhoneNumber.slice(0, 2)} (${rawPhoneNumber.slice( - 2, - 4, - )}) ${rawPhoneNumber.slice(4, 8)}-${rawPhoneNumber.slice(8)}`; + const rawPhoneNumber = body.key.participant.split('@')[0]; + const phoneMatch = rawPhoneNumber.match(/^(\d{2})(\d{2})(\d{4})(\d{4})$/); + + let formattedPhoneNumber: string; + + if (phoneMatch) { + formattedPhoneNumber = `+${phoneMatch[1]} (${phoneMatch[2]}) ${phoneMatch[3]}-${phoneMatch[4]}`; + } else { + formattedPhoneNumber = `+${rawPhoneNumber}`; + } let content: string; @@ -2104,11 +2109,16 @@ export class ChatwootService { if (body.key.remoteJid.includes('@g.us')) { const participantName = body.pushName; - const rawPhoneNumber = body.key.remoteJid.split('@')[0]; - const formattedPhoneNumber = `+${rawPhoneNumber.slice(0, 2)} (${rawPhoneNumber.slice( - 2, - 4, - )}) ${rawPhoneNumber.slice(4, 8)}-${rawPhoneNumber.slice(8)}`; + const rawPhoneNumber = body.key.participant.split('@')[0]; + const phoneMatch = rawPhoneNumber.match(/^(\d{2})(\d{2})(\d{4})(\d{4})$/); + + let formattedPhoneNumber: string; + + if (phoneMatch) { + formattedPhoneNumber = `+${phoneMatch[1]} (${phoneMatch[2]}) ${phoneMatch[3]}-${phoneMatch[4]}`; + } else { + formattedPhoneNumber = `+${rawPhoneNumber}`; + } let content: string;