mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-31 19:17:48 +00:00
chore: Prevent null in Contact JSONB attributes (#3730)
This commit is contained in:
@@ -11,6 +11,7 @@ Metrics/ClassLength:
|
|||||||
Max: 125
|
Max: 125
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'app/models/conversation.rb'
|
- 'app/models/conversation.rb'
|
||||||
|
- 'app/models/contact.rb'
|
||||||
- 'app/mailers/conversation_reply_mailer.rb'
|
- 'app/mailers/conversation_reply_mailer.rb'
|
||||||
- 'app/models/message.rb'
|
- 'app/models/message.rb'
|
||||||
- 'app/builders/messages/facebook/message_builder.rb'
|
- 'app/builders/messages/facebook/message_builder.rb'
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class Contact < ApplicationRecord
|
|||||||
has_many :messages, as: :sender, dependent: :destroy_async
|
has_many :messages, as: :sender, dependent: :destroy_async
|
||||||
has_many :notes, dependent: :destroy_async
|
has_many :notes, dependent: :destroy_async
|
||||||
|
|
||||||
before_validation :prepare_email_attribute
|
before_validation :prepare_contact_attributes
|
||||||
after_create_commit :dispatch_create_event, :ip_lookup
|
after_create_commit :dispatch_create_event, :ip_lookup
|
||||||
after_update_commit :dispatch_update_event
|
after_update_commit :dispatch_update_event
|
||||||
after_destroy_commit :dispatch_destroy_event
|
after_destroy_commit :dispatch_destroy_event
|
||||||
@@ -146,10 +146,19 @@ class Contact < ApplicationRecord
|
|||||||
ContactIpLookupJob.perform_later(self)
|
ContactIpLookupJob.perform_later(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def prepare_contact_attributes
|
||||||
|
prepare_email_attribute
|
||||||
|
prepare_jsonb_attributes
|
||||||
|
end
|
||||||
|
|
||||||
def prepare_email_attribute
|
def prepare_email_attribute
|
||||||
# So that the db unique constraint won't throw error when email is ''
|
# So that the db unique constraint won't throw error when email is ''
|
||||||
self.email = nil if email.blank?
|
self.email = email.present? ? email.downcase : nil
|
||||||
email.downcase! if email.present?
|
end
|
||||||
|
|
||||||
|
def prepare_jsonb_attributes
|
||||||
|
self.additional_attributes = {} if additional_attributes.blank?
|
||||||
|
self.custom_attributes = {} if custom_attributes.blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
def dispatch_create_event
|
def dispatch_create_event
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
class UpdateNilContactAttributesToEmptyHash < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
# rubocop:disable Rails/SkipsModelValidations
|
||||||
|
Contact.where(custom_attributes: nil).update_all(custom_attributes: {})
|
||||||
|
Contact.where(additional_attributes: nil).update_all(additional_attributes: {})
|
||||||
|
# rubocop:enable Rails/SkipsModelValidations
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2021_12_21_125545) do
|
ActiveRecord::Schema.define(version: 2022_01_11_200105) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pg_stat_statements"
|
enable_extension "pg_stat_statements"
|
||||||
@@ -461,7 +461,7 @@ ActiveRecord::Schema.define(version: 2021_12_21_125545) do
|
|||||||
t.string "timezone", default: "UTC"
|
t.string "timezone", default: "UTC"
|
||||||
t.boolean "enable_email_collect", default: true
|
t.boolean "enable_email_collect", default: true
|
||||||
t.boolean "csat_survey_enabled", default: false
|
t.boolean "csat_survey_enabled", default: false
|
||||||
t.boolean 'allow_messages_after_resolved', default: true
|
t.boolean "allow_messages_after_resolved", default: true
|
||||||
t.index ["account_id"], name: "index_inboxes_on_account_id"
|
t.index ["account_id"], name: "index_inboxes_on_account_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -11,4 +11,36 @@ RSpec.describe Contact do
|
|||||||
it { is_expected.to belong_to(:account) }
|
it { is_expected.to belong_to(:account) }
|
||||||
it { is_expected.to have_many(:conversations).dependent(:destroy_async) }
|
it { is_expected.to have_many(:conversations).dependent(:destroy_async) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'pepare contact attributes before validation' do
|
||||||
|
it 'sets email to lowercase' do
|
||||||
|
contact = create(:contact, email: 'Test@test.com')
|
||||||
|
expect(contact.email).to eq('test@test.com')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets email to nil when empty string' do
|
||||||
|
contact = create(:contact, email: '')
|
||||||
|
expect(contact.email).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets custom_attributes to {} when nil' do
|
||||||
|
contact = create(:contact, custom_attributes: nil)
|
||||||
|
expect(contact.custom_attributes).to eq({})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets custom_attributes to {} when empty string' do
|
||||||
|
contact = create(:contact, custom_attributes: '')
|
||||||
|
expect(contact.custom_attributes).to eq({})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets additional_attributes to {} when nil' do
|
||||||
|
contact = create(:contact, additional_attributes: nil)
|
||||||
|
expect(contact.additional_attributes).to eq({})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets additional_attributes to {} when empty string' do
|
||||||
|
contact = create(:contact, additional_attributes: '')
|
||||||
|
expect(contact.additional_attributes).to eq({})
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user