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:
Muhsin Keloth
2024-03-13 16:40:21 +05:30
committed by GitHub
parent 561fafa198
commit 804a42c271
2 changed files with 16 additions and 0 deletions

View File

@@ -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

View File

@@ -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