diff --git a/app/controllers/api/v1/widget/base_controller.rb b/app/controllers/api/v1/widget/base_controller.rb index 9ec702e6c..229cf27e9 100644 --- a/app/controllers/api/v1/widget/base_controller.rb +++ b/app/controllers/api/v1/widget/base_controller.rb @@ -50,7 +50,9 @@ class Api::V1::Widget::BaseController < ApplicationController end def contact_name - params[:contact][:name] || contact_email.split('@')[0] if contact_email.present? + return if @contact.email.present? || @contact.phone_number.present? || @contact.identifier.present? + + permitted_params.dig(:contact, :name) || (contact_email.split('@')[0] if contact_email.present?) end def contact_phone_number diff --git a/spec/controllers/api/v1/widget/conversations_controller_spec.rb b/spec/controllers/api/v1/widget/conversations_controller_spec.rb index cf386a65e..4cd594a12 100644 --- a/spec/controllers/api/v1/widget/conversations_controller_spec.rb +++ b/spec/controllers/api/v1/widget/conversations_controller_spec.rb @@ -74,8 +74,30 @@ RSpec.describe '/api/v1/widget/conversations/toggle_typing', type: :request do expect(json_response['messages'][0]['content']).to eq 'This is a test message' end + it 'create a conversation with a name and without an email' do + post '/api/v1/widget/conversations', + headers: { 'X-Auth-Token' => token }, + params: { + website_token: web_widget.website_token, + contact: { + name: 'alphy' + }, + message: { + content: 'This is a test message' + } + }, + as: :json + + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + expect(json_response['id']).not_to be_nil + expect(json_response['contact']['email']).to be_nil + expect(json_response['contact']['name']).to eq 'alphy' + expect(json_response['messages'][0]['content']).to eq 'This is a test message' + end + it 'does not update the name if the contact already exist' do - existing_contact = create(:contact, account: account) + existing_contact = create(:contact, account: account, email: 'contact-email@chatwoot.com') post '/api/v1/widget/conversations', headers: { 'X-Auth-Token' => token },