diff --git a/app/models/contact.rb b/app/models/contact.rb index 1e9ab8dc6..1f3d2fb90 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -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 diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 7029c89fe..ff186466a 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -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