From 7434b60f71a2eaeca40df7e6fcd5c8b95c8e5e81 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Wed, 25 May 2022 12:38:23 +0530 Subject: [PATCH] fix: Revert changes to fix name updation from pre-chat form (#4741) --- .../api/v1/widget/base_controller.rb | 6 ++++ .../api/v1/widget/conversations_controller.rb | 1 - .../widget/conversations_controller_spec.rb | 30 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/widget/base_controller.rb b/app/controllers/api/v1/widget/base_controller.rb index 646d70acf..fc4d33fc3 100644 --- a/app/controllers/api/v1/widget/base_controller.rb +++ b/app/controllers/api/v1/widget/base_controller.rb @@ -54,6 +54,7 @@ class Api::V1::Widget::BaseController < ApplicationController ).perform else @contact.update!(email: email) + update_contact_name end end @@ -67,9 +68,14 @@ class Api::V1::Widget::BaseController < ApplicationController ).perform else @contact.update!(phone_number: phone_number) + update_contact_name end end + def update_contact_name + @contact.update!(name: contact_name) if contact_name.present? + end + def contact_email permitted_params.dig(:contact, :email)&.downcase end diff --git a/app/controllers/api/v1/widget/conversations_controller.rb b/app/controllers/api/v1/widget/conversations_controller.rb index cfe107ac6..8d054229c 100644 --- a/app/controllers/api/v1/widget/conversations_controller.rb +++ b/app/controllers/api/v1/widget/conversations_controller.rb @@ -16,7 +16,6 @@ class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController def process_update_contact update_contact(contact_email) if @contact.email.blank? && contact_email.present? update_contact_phone_number(contact_phone_number) if @contact.phone_number.blank? && contact_phone_number.present? - @contact.update!(name: contact_name) if contact_name.present? end def update_last_seen diff --git a/spec/controllers/api/v1/widget/conversations_controller_spec.rb b/spec/controllers/api/v1/widget/conversations_controller_spec.rb index 88f41dea0..307862374 100644 --- a/spec/controllers/api/v1/widget/conversations_controller_spec.rb +++ b/spec/controllers/api/v1/widget/conversations_controller_spec.rb @@ -69,6 +69,36 @@ RSpec.describe '/api/v1/widget/conversations/toggle_typing', type: :request do expect(json_response['id']).not_to eq nil expect(json_response['contact']['email']).to eq 'contact-email@chatwoot.com' expect(json_response['contact']['phone_number']).to eq '+919745313456' + expect(json_response['contact']['name']).to eq 'contact-name' + expect(json_response['custom_attributes']['order_id']).to eq '12345' + 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) + + post '/api/v1/widget/conversations', + headers: { 'X-Auth-Token' => token }, + params: { + website_token: web_widget.website_token, + contact: { + name: 'contact-name', + email: existing_contact.email, + phone_number: '+919745313456' + }, + message: { + content: 'This is a test message' + }, + custom_attributes: { order_id: '12345' } + }, + as: :json + + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + expect(json_response['id']).not_to eq nil + expect(json_response['contact']['email']).to eq existing_contact.email + expect(json_response['contact']['name']).not_to eq 'contact-name' + expect(json_response['contact']['phone_number']).to eq '+919745313456' expect(json_response['custom_attributes']['order_id']).to eq '12345' expect(json_response['messages'][0]['content']).to eq 'This is a test message' end