Skip to content

Commit

Permalink
Refactor phone number formatting in ChatwootService to improve parsin…
Browse files Browse the repository at this point in the history
…g logic
  • Loading branch information
Richards0nd committed Nov 11, 2024
1 parent c5fd81d commit 7ef8afa
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 7ef8afa

Please sign in to comment.