fix: "wa_source_id" function return value (#5451)
- Fix contact inbox builder returning invalid WhatsApp source id - Add specs to cover source id validations Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -31,7 +31,7 @@ class ContactInboxBuilder
|
||||
return unless @contact.phone_number
|
||||
|
||||
# whatsapp doesn't want the + in e164 format
|
||||
"#{@contact.phone_number}.delete('+')"
|
||||
@contact.phone_number.delete('+').to_s
|
||||
end
|
||||
|
||||
def twilio_source_id
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
class ContactInbox < ApplicationRecord
|
||||
include Pubsubable
|
||||
include RegexHelper
|
||||
validates :inbox_id, presence: true
|
||||
validates :contact_id, presence: true
|
||||
validates :source_id, presence: true
|
||||
@@ -51,10 +52,10 @@ class ContactInbox < ApplicationRecord
|
||||
|
||||
def validate_twilio_source_id
|
||||
# https://www.twilio.com/docs/glossary/what-e164#regex-matching-for-e164
|
||||
if inbox.channel.medium == 'sms' && !/\+[1-9]\d{1,14}\z/.match?(source_id)
|
||||
errors.add(:source_id, 'invalid source id for twilio sms inbox. valid Regex /\+[1-9]\d{1,14}\z/')
|
||||
elsif inbox.channel.medium == 'whatsapp' && !/whatsapp:\+[1-9]\d{1,14}\z/.match?(source_id)
|
||||
errors.add(:source_id, 'invalid source id for twilio whatsapp inbox. valid Regex /whatsapp:\+[1-9]\d{1,14}\z/')
|
||||
if inbox.channel.medium == 'sms' && !TWILIO_CHANNEL_SMS_REGEX.match?(source_id)
|
||||
errors.add(:source_id, "invalid source id for twilio sms inbox. valid Regex #{TWILIO_CHANNEL_SMS_REGEX}")
|
||||
elsif inbox.channel.medium == 'whatsapp' && !TWILIO_CHANNEL_WHATSAPP_REGEX.match?(source_id)
|
||||
errors.add(:source_id, "invalid source id for twilio whatsapp inbox. valid Regex #{TWILIO_CHANNEL_WHATSAPP_REGEX}")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -62,8 +63,15 @@ class ContactInbox < ApplicationRecord
|
||||
errors.add(:source_id, "invalid source id for Email inbox. valid Regex #{Devise.email_regexp}") unless Devise.email_regexp.match?(source_id)
|
||||
end
|
||||
|
||||
def validate_whatsapp_source_id
|
||||
return if WHATSAPP_CHANNEL_REGEX.match?(source_id)
|
||||
|
||||
errors.add(:source_id, "invalid source id for whatsapp inbox. valid Regex #{WHATSAPP_CHANNEL_REGEX}")
|
||||
end
|
||||
|
||||
def valid_source_id_format?
|
||||
validate_twilio_source_id if inbox.channel_type == 'Channel::TwilioSms'
|
||||
validate_email_source_id if inbox.channel_type == 'Channel::Email'
|
||||
validate_whatsapp_source_id if inbox.channel_type == 'Channel::Whatsapp'
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user