diff --git a/app/services/whatsapp/baileys_handlers/helpers.rb b/app/services/whatsapp/baileys_handlers/helpers.rb index 000be4d3a..76db37756 100644 --- a/app/services/whatsapp/baileys_handlers/helpers.rb +++ b/app/services/whatsapp/baileys_handlers/helpers.rb @@ -128,7 +128,7 @@ module Whatsapp::BaileysHandlers::Helpers # rubocop:disable Metrics/ModuleLength def contact_name # NOTE: `verifiedBizName` is only available for business accounts and has a higher priority than `pushName`. name = @raw_message[:verifiedBizName].presence || @raw_message[:pushName] - return name if self_message? || incoming? + return name if name.presence && (self_message? || incoming?) phone_number_from_jid end diff --git a/spec/services/whatsapp/incoming_message_baileys_service_spec.rb b/spec/services/whatsapp/incoming_message_baileys_service_spec.rb index d755b7d94..f24d5f384 100644 --- a/spec/services/whatsapp/incoming_message_baileys_service_spec.rb +++ b/spec/services/whatsapp/incoming_message_baileys_service_spec.rb @@ -344,6 +344,16 @@ describe Whatsapp::IncomingMessageBaileysService do expect(conversation.contact.name).to eq('5511912345678') end + it 'creates contact with phone number as name on incoming message if pushName is not present' do + raw_message[:pushName] = nil + create(:account_user, account: inbox.account) + + described_class.new(inbox: inbox, params: params).perform + + conversation = inbox.conversations.last + expect(conversation.contact.name).to eq('5511912345678') + end + it 'creates a message on an existing conversation' do contact = create(:contact, account: inbox.account, name: 'John Doe') contact_inbox = create(:contact_inbox, inbox: inbox, contact: contact, source_id: '5511912345678')