From f9d66e2b42a997891d7573331ef693386c6fefeb Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Mon, 18 Mar 2024 18:59:22 +0530 Subject: [PATCH] feat: Set contact type as lead for all the non-live chat contacts --- app/builders/contact_inbox_with_contact_builder.rb | 3 ++- app/models/channel/web_widget.rb | 2 +- app/models/contact.rb | 8 ++++++-- app/services/contacts/sync_attributes.rb | 2 +- ...1131_change_default_contact_type_in_contacts.rb | 5 +++++ db/schema.rb | 4 ++-- .../contact_inbox_with_contact_builder_spec.rb | 14 ++++++++++++++ 7 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20240318111131_change_default_contact_type_in_contacts.rb diff --git a/app/builders/contact_inbox_with_contact_builder.rb b/app/builders/contact_inbox_with_contact_builder.rb index 6e913eda8..8b2785075 100644 --- a/app/builders/contact_inbox_with_contact_builder.rb +++ b/app/builders/contact_inbox_with_contact_builder.rb @@ -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 diff --git a/app/models/channel/web_widget.rb b/app/models/channel/web_widget.rb index 2efe74881..883fb0969 100644 --- a/app/models/channel/web_widget.rb +++ b/app/models/channel/web_widget.rb @@ -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 diff --git a/app/models/contact.rb b/app/models/contact.rb index a60e9f4d2..e48a6165b 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -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 diff --git a/app/services/contacts/sync_attributes.rb b/app/services/contacts/sync_attributes.rb index bc10def66..1e6b7d2c8 100644 --- a/app/services/contacts/sync_attributes.rb +++ b/app/services/contacts/sync_attributes.rb @@ -7,7 +7,7 @@ class Contacts::SyncAttributes def perform update_contact_location_and_country_code - set_contact_type + # set_contact_type end private diff --git a/db/migrate/20240318111131_change_default_contact_type_in_contacts.rb b/db/migrate/20240318111131_change_default_contact_type_in_contacts.rb new file mode 100644 index 000000000..2deec00d0 --- /dev/null +++ b/db/migrate/20240318111131_change_default_contact_type_in_contacts.rb @@ -0,0 +1,5 @@ +class ChangeDefaultContactTypeInContacts < ActiveRecord::Migration[7.0] + def change + change_column_default :contacts, :contact_type, from: 0, to: 1 + end +end diff --git a/db/schema.rb b/db/schema.rb index d0499cb7b..c0bbc6b9a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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: "" diff --git a/spec/builders/contact_inbox_with_contact_builder_spec.rb b/spec/builders/contact_inbox_with_contact_builder_spec.rb index 8efeae9a5..c774dfd40 100644 --- a/spec/builders/contact_inbox_with_contact_builder_spec.rb +++ b/spec/builders/contact_inbox_with_contact_builder_spec.rb @@ -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