From 052f43f0d990ed000e715f15215104fe670cf4c6 Mon Sep 17 00:00:00 2001 From: Muhsin <12408980+muhsin-k@users.noreply.github.com> Date: Fri, 12 Jun 2026 10:44:16 +0400 Subject: [PATCH] fix(whatsapp): guard Mexico normalizer on length and clarify WAID wording Co-Authored-By: Claude Opus 4.8 (1M context) --- .../mexico_phone_normalizer.rb | 8 ++++++- .../mexico_phone_normalizer_spec.rb | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 spec/services/whatsapp/phone_normalizers/mexico_phone_normalizer_spec.rb diff --git a/app/services/whatsapp/phone_normalizers/mexico_phone_normalizer.rb b/app/services/whatsapp/phone_normalizers/mexico_phone_normalizer.rb index 9af5ff385..00a2df1aa 100644 --- a/app/services/whatsapp/phone_normalizers/mexico_phone_normalizer.rb +++ b/app/services/whatsapp/phone_normalizers/mexico_phone_normalizer.rb @@ -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 diff --git a/spec/services/whatsapp/phone_normalizers/mexico_phone_normalizer_spec.rb b/spec/services/whatsapp/phone_normalizers/mexico_phone_normalizer_spec.rb new file mode 100644 index 000000000..ad5216afb --- /dev/null +++ b/spec/services/whatsapp/phone_normalizers/mexico_phone_normalizer_spec.rb @@ -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