diff --git a/app/controllers/twilio/callback_controller.rb b/app/controllers/twilio/callback_controller.rb index 455828228..d607ba151 100644 --- a/app/controllers/twilio/callback_controller.rb +++ b/app/controllers/twilio/callback_controller.rb @@ -30,7 +30,8 @@ class Twilio::CallbackController < ApplicationController :NumMedia, :Latitude, :Longitude, - :MessageType + :MessageType, + :ProfileName ) end end diff --git a/app/services/twilio/incoming_message_service.rb b/app/services/twilio/incoming_message_service.rb index 29bd05f8c..7a74488e2 100644 --- a/app/services/twilio/incoming_message_service.rb +++ b/app/services/twilio/incoming_message_service.rb @@ -61,6 +61,9 @@ class Twilio::IncomingMessageService @contact_inbox = contact_inbox @contact = contact_inbox.contact + + # Update existing contact name if ProfileName is available and current name is just phone number + update_contact_name_if_needed end def conversation_params @@ -173,4 +176,18 @@ class Twilio::IncomingMessageService coordinates_long: params[:Longitude].to_f ) end + + def update_contact_name_if_needed + return if params[:ProfileName].blank? + return if @contact.name == params[:ProfileName] + + # Only update if current name exactly matches the phone number or formatted phone number + return unless contact_name_matches_phone_number? + + @contact.update!(name: params[:ProfileName]) + end + + def contact_name_matches_phone_number? + @contact.name == phone_number || @contact.name == formatted_phone_number + end end diff --git a/spec/services/twilio/incoming_message_service_spec.rb b/spec/services/twilio/incoming_message_service_spec.rb index 63f721dde..d32ef59bb 100644 --- a/spec/services/twilio/incoming_message_service_spec.rb +++ b/spec/services/twilio/incoming_message_service_spec.rb @@ -327,6 +327,81 @@ describe Twilio::IncomingMessageService do contact = twilio_channel.inbox.contacts.find_by(phone_number: '+1234567890') expect(contact.name).to eq('1234567890') end + + it 'updates existing contact name when current name matches phone number' do + # Create contact with phone number as name + existing_contact = create(:contact, + account: twilio_channel.inbox.account, + name: '+1234567890', + phone_number: '+1234567890') + create(:contact_inbox, + contact: existing_contact, + inbox: twilio_channel.inbox, + source_id: '+1234567890') + + params = { + SmsSid: 'SMxx', + From: '+1234567890', + AccountSid: 'ACxxx', + MessagingServiceSid: twilio_channel.messaging_service_sid, + Body: 'Hello', + ProfileName: 'Jane Smith' + } + + described_class.new(params: params).perform + existing_contact.reload + expect(existing_contact.name).to eq('Jane Smith') + end + + it 'does not update contact name when current name is different from phone number' do + # Create contact with human name + existing_contact = create(:contact, + account: twilio_channel.inbox.account, + name: 'John Doe', + phone_number: '+1234567890') + create(:contact_inbox, + contact: existing_contact, + inbox: twilio_channel.inbox, + source_id: '+1234567890') + + params = { + SmsSid: 'SMxx', + From: '+1234567890', + AccountSid: 'ACxxx', + MessagingServiceSid: twilio_channel.messaging_service_sid, + Body: 'Hello', + ProfileName: 'Jane Smith' + } + + described_class.new(params: params).perform + existing_contact.reload + expect(existing_contact.name).to eq('John Doe') # Should not change + end + + it 'updates contact name when current name matches formatted phone number' do + # Create contact with formatted phone number as name + existing_contact = create(:contact, + account: twilio_channel.inbox.account, + name: '1234567890', + phone_number: '+1234567890') + create(:contact_inbox, + contact: existing_contact, + inbox: twilio_channel.inbox, + source_id: '+1234567890') + + params = { + SmsSid: 'SMxx', + From: '+1234567890', + AccountSid: 'ACxxx', + MessagingServiceSid: twilio_channel.messaging_service_sid, + Body: 'Hello', + ProfileName: 'Alice Johnson' + } + + described_class.new(params: params).perform + existing_contact.reload + expect(existing_contact.name).to eq('Alice Johnson') + end end end end