fix: Bypass invalid values in contact patch end point (#4519)

This commit is contained in:
Muhsin Keloth
2022-04-25 12:25:38 +05:30
committed by GitHub
parent 1b3011b27b
commit fa51fd1d73
2 changed files with 59 additions and 4 deletions

View File

@@ -31,8 +31,7 @@ class Contact < ApplicationRecord
validates :email, allow_blank: true, uniqueness: { scope: [:account_id], case_sensitive: false }
validates :identifier, allow_blank: true, uniqueness: { scope: [:account_id] }
validates :phone_number,
allow_blank: true, uniqueness: { scope: [:account_id] },
format: { with: /\+[1-9]\d{1,14}\z/, message: 'should be in e164 format' }
allow_blank: true, uniqueness: { scope: [:account_id] }
validates :name, length: { maximum: 255 }
belongs_to :account
@@ -42,8 +41,8 @@ class Contact < ApplicationRecord
has_many :inboxes, through: :contact_inboxes
has_many :messages, as: :sender, dependent: :destroy_async
has_many :notes, dependent: :destroy_async
before_validation :prepare_contact_attributes
before_save :phone_number_format, :email_format
after_create_commit :dispatch_create_event, :ip_lookup
after_update_commit :dispatch_update_event
after_destroy_commit :dispatch_destroy_event
@@ -143,6 +142,18 @@ class Contact < ApplicationRecord
ContactIpLookupJob.perform_later(self)
end
def phone_number_format
return if phone_number.blank?
self.phone_number = changes['phone_number'].first unless phone_number.match?(/\+[1-9]\d{1,14}\z/)
end
def email_format
return if email.blank?
self.email = changes['email'].first unless email.match(Devise.email_regexp)
end
def prepare_contact_attributes
prepare_email_attribute
prepare_jsonb_attributes