Compare commits

...
8 changed files with 34 additions and 10 deletions
@@ -55,7 +55,8 @@ class ContactInboxWithContactBuilder
email: contact_attributes[:email],
identifier: contact_attributes[:identifier],
additional_attributes: contact_attributes[:additional_attributes],
custom_attributes: contact_attributes[:custom_attributes]
custom_attributes: contact_attributes[:custom_attributes],
contact_type: contact_attributes[:type]
)
end
+1 -1
View File
@@ -101,7 +101,7 @@ class Channel::WebWidget < ApplicationRecord
def create_contact_inbox(additional_attributes = {})
::ContactInboxWithContactBuilder.new({
inbox: inbox,
contact_attributes: { additional_attributes: additional_attributes }
contact_attributes: { additional_attributes: additional_attributes, type: 'visitor' }
}).perform
end
end
+6 -2
View File
@@ -7,7 +7,7 @@
# id :integer not null, primary key
# additional_attributes :jsonb
# blocked :boolean default(FALSE), not null
# contact_type :integer default("visitor")
# contact_type :integer default("lead")
# country_code :string default("")
# custom_attributes :jsonb
# email :string
@@ -57,7 +57,7 @@ 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_validation :prepare_contact_attributes, :set_default_contact_type
after_create_commit :dispatch_create_event, :ip_lookup
after_update_commit :dispatch_update_event
after_destroy_commit :dispatch_destroy_event
@@ -197,6 +197,10 @@ class Contact < ApplicationRecord
prepare_jsonb_attributes
end
def set_default_contact_type
self.contact_type ||= :lead
end
def prepare_email_attribute
# So that the db unique constraint won't throw error when email is ''
self.email = email.present? ? email.downcase : nil
@@ -0,0 +1,5 @@
class ChangeDefaultContactTypeInContacts < ActiveRecord::Migration[7.0]
def change
change_column_default :contacts, :contact_type, from: 0, to: 1
end
end
+2 -2
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2024_03_06_201954) do
ActiveRecord::Schema[7.0].define(version: 2024_03_18_111131) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
@@ -418,7 +418,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_03_06_201954) do
t.string "identifier"
t.jsonb "custom_attributes", default: {}
t.datetime "last_activity_at", precision: nil
t.integer "contact_type", default: 0
t.integer "contact_type", default: 1
t.string "middle_name", default: ""
t.string "last_name", default: ""
t.string "location", default: ""
@@ -96,4 +96,18 @@ describe ContactInboxWithContactBuilder do
expect(contact_inbox.contact.id).to be(contact.id)
end
end
it 'creates contact type as visitor by default' do
contact_inbox = described_class.new(
source_id: '123456',
inbox: inbox,
contact_attributes: {
name: 'Contact',
phone_number: '',
type: 'visitor'
}
).perform
expect(contact_inbox.contact.contact_type).to eq('visitor')
end
end
+3 -3
View File
@@ -28,7 +28,7 @@ RSpec.describe Contact do
it 'sets email to nil when empty string' do
contact = create(:contact, email: '')
expect(contact.email).to be_nil
expect(contact.contact_type).to eq('visitor')
expect(contact.contact_type).to eq('lead')
end
it 'sets custom_attributes to {} when nil' do
@@ -87,9 +87,9 @@ RSpec.describe Contact do
end
context 'when a contact is created' do
it 'has contact type "visitor" by default' do
it 'has contact type "lead" by default' do
contact = create(:contact)
expect(contact.contact_type).to eq 'visitor'
expect(contact.contact_type).to eq 'lead'
end
it 'has contact type "lead" when email is present' do
@@ -9,7 +9,7 @@ RSpec.describe Contacts::SyncAttributes do
context 'when contact has neither email/phone number nor social details' do
it 'does not change contact type' do
described_class.new(contact).perform
expect(contact.reload.contact_type).to eq('visitor')
expect(contact.reload.contact_type).to eq('lead')
end
end