fix(whatsapp): guard Mexico normalizer on length and clarify WAID wording

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Muhsin
2026-06-12 10:44:16 +04:00
co-authored by Claude Opus 4.8
parent 03e662d5b9
commit 052f43f0d9
2 changed files with 30 additions and 1 deletions
@@ -1,10 +1,16 @@
# Handles Mexico phone number normalization
#
# WhatsApp may include a "1" after Mexico's country code (521), while contacts
# can already be stored without it (52). Normalize to the stored E.164 format.
# can already be stored without it (52). Normalize to the stored WAID (digits-only)
# format; provider formatting (whatsapp:+...) happens in the normalization service.
# The mobile "1" only appears on the 13-digit form (52 + 1 + 10 digits), so guard
# on length to avoid stripping a legitimate "1" from a shorter national number.
class Whatsapp::PhoneNormalizers::MexicoPhoneNormalizer < Whatsapp::PhoneNormalizers::BasePhoneNormalizer
COUNTRY_CODE_WITH_MOBILE_LENGTH = 13
def normalize(waid)
return waid unless handles_country?(waid)
return waid unless waid.length == COUNTRY_CODE_WITH_MOBILE_LENGTH
waid.sub(/^521/, '52')
end
@@ -0,0 +1,23 @@
require 'rails_helper'
describe Whatsapp::PhoneNormalizers::MexicoPhoneNormalizer do
subject(:normalizer) { described_class.new }
describe '#normalize' do
it 'strips the mobile "1" from the 13-digit form (521 + 10 digits)' do
expect(normalizer.normalize('5215512345678')).to eq('525512345678')
end
it 'leaves the already-normalized 12-digit form untouched' do
expect(normalizer.normalize('525512345678')).to eq('525512345678')
end
it 'does not strip a legitimate leading "1" from a 12-digit national number' do
expect(normalizer.normalize('521512345678')).to eq('521512345678')
end
it 'returns non-Mexico numbers unchanged' do
expect(normalizer.normalize('5511999998888')).to eq('5511999998888')
end
end
end