iachat/spec/services/twilio
Muhsin Keloth 26ea87a6cb
fix: Extend phone number normalization to Twilio WhatsApp (#12655)
### Problem
WhatsApp Cloud channels already handle Brazil/Argentina phone number
format mismatches (PRs #12492, #11173), but Twilio WhatsApp channels
were creating duplicate contacts
  when:
  - Template sent to new format: `whatsapp:+5541988887777` (13 digits)
  - User responds from old format: `whatsapp:+554188887777` (12 digits)

### Solution

The solution extends the existing phone number normalization
infrastructure to support both WhatsApp providers while handling their
different payload formats:

  ### Provider Format Differences
  - **WhatsApp Cloud**: `wa_id: "919745786257"` (clean number)
- **Twilio WhatsApp**: `From: "whatsapp:+919745786257"` (prefixed
format)
  
  
 ### Test Coverage

#### Brazil Phone Number Tests
  **Case 1: New Format (13 digits with "9")**
- **Test 1**: No existing contact → Creates new contact with original
format
- **Test 2**: Contact exists in same format → Appends to existing
conversation

  **Case 2: Old Format (12 digits without "9")**
- **Test 3**: Contact exists in old format → Appends to existing
conversation
- **Test 4** *(Critical)*: Contact exists in new format, message in old
format → Finds existing contact, prevents duplicate
- **Test 5**: No contact exists → Creates new contact with incoming
format

#### Argentina Phone Number Tests
  **Case 3: With "9" after country code**
  - **Test 6**: No existing contact → Creates new contact
- **Test 7**: Contact exists in normalized format → Uses existing
contact

  **Case 4: Without "9" after country code**
  - **Test 8**: Contact exists in same format → Appends to existing
  - **Test 9**: No contact exists → Creates new contact

Fixes
https://linear.app/chatwoot/issue/CW-5565/inconsistencies-for-mobile-numbersargentina-brazil-and-mexico-numbers
2025-10-28 18:16:29 +05:30
..
delivery_status_service_spec.rb fix: Change the message status to failed if the Twilio message delivery status is undelivered (#8097) 2023-10-12 15:27:38 +05:30
incoming_message_service_spec.rb fix: Extend phone number normalization to Twilio WhatsApp (#12655) 2025-10-28 18:16:29 +05:30
oneoff_sms_campaign_service_spec.rb fix: Error shouldn't halt the campaign for entire audience (#11980) 2025-08-11 12:03:48 +05:30
send_on_twilio_service_spec.rb chore: Add cache to improve widget performance (#11163) 2025-03-24 16:04:49 -07:00
template_processor_service_spec.rb feat: Added the backend support for twilio content templates (#12272) 2025-08-24 10:05:15 +05:30
template_sync_service_spec.rb feat: Added the backend support for twilio content templates (#12272) 2025-08-24 10:05:15 +05:30
webhook_setup_service_spec.rb feat: Allow Twilio::REST::TwilioError errors to raise and prevent Inbox creation (#7379) 2023-08-16 15:00:02 -07:00