mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 19:48:08 +00:00
feat: Update the location and country_code when creating or updating the contact (#9106)
* feat: Update the location and country_code when creating or updating the contact. * chore: improve comments
This commit is contained in:
@@ -61,6 +61,7 @@ class Contact < ApplicationRecord
|
||||
after_create_commit :dispatch_create_event, :ip_lookup
|
||||
after_update_commit :dispatch_update_event
|
||||
after_destroy_commit :dispatch_destroy_event
|
||||
before_save :update_contact_location_and_country_code
|
||||
|
||||
enum contact_type: { visitor: 0, lead: 1, customer: 2 }
|
||||
|
||||
@@ -206,6 +207,13 @@ class Contact < ApplicationRecord
|
||||
self.custom_attributes = {} if custom_attributes.blank?
|
||||
end
|
||||
|
||||
def update_contact_location_and_country_code
|
||||
# TODO: Ensure that location and country_code are updated from additional_attributes.
|
||||
# We will remove this once all contacts are updated and both the location and country_code fields are standardized throughout the app.
|
||||
self.location = additional_attributes['city']
|
||||
self.country_code = additional_attributes['country']
|
||||
end
|
||||
|
||||
def dispatch_create_event
|
||||
Rails.configuration.dispatcher.dispatch(CONTACT_CREATED, Time.zone.now, contact: self)
|
||||
end
|
||||
|
||||
@@ -75,4 +75,12 @@ RSpec.describe Contact do
|
||||
expect(contact.email).to eq 'test@test.com'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when city and country code passed in additional attributes' do
|
||||
it 'updates location and country code' do
|
||||
contact = create(:contact, additional_attributes: { city: 'New York', country: 'US' })
|
||||
expect(contact.location).to eq 'New York'
|
||||
expect(contact.country_code).to eq 'US'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user