fix: Add validation to the name attribute in user (#10805)
With this change, the form will start displaying a required field. While validation is already enforced in APIs and other areas, the super_admin console—being autogenerated—will throw an error since this requirement isn’t explicitly defined in the model. <img width="670" alt="Screenshot 2025-01-30 at 2 12 43 PM" src="https://github.com/user-attachments/assets/e0ab3ace-3649-4ef2-bc94-8d4d80453dd1" /> Fixes https://github.com/chatwoot/chatwoot/issues/10754 --------- Co-authored-by: Shivam Mishra <scm.mymail@gmail.com> Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com> Co-authored-by: Sony Mathew <ynos1234@gmail.com> Co-authored-by: Sony Mathew <2040199+sony-mathew@users.noreply.github.com> Co-authored-by: Sony Mathew <sony@chatwoot.com>
This commit is contained in:
co-authored by
Shivam Mishra
Muhsin Keloth
Sony Mathew
Sony Mathew
Sony Mathew
parent
0920a01e66
commit
2266eb493b
@@ -29,8 +29,9 @@ class AgentBuilder
|
||||
user = User.from_email(email)
|
||||
return user if user
|
||||
|
||||
@name = email.split('@').first if @name.blank?
|
||||
temp_password = "1!aA#{SecureRandom.alphanumeric(12)}"
|
||||
User.create!(email: email, name: name, password: temp_password, password_confirmation: temp_password)
|
||||
User.create!(email: email, name: @name, password: temp_password, password_confirmation: temp_password)
|
||||
end
|
||||
|
||||
# Checks if the user needs confirmation.
|
||||
|
||||
@@ -75,6 +75,7 @@ class User < ApplicationRecord
|
||||
# work because :validatable in devise overrides this.
|
||||
# validates_uniqueness_of :email, scope: :account_id
|
||||
|
||||
validates :name, presence: true
|
||||
validates :email, presence: true
|
||||
|
||||
serialize :otp_backup_codes, type: Array
|
||||
|
||||
@@ -56,7 +56,7 @@ RSpec.describe AgentBuilder, type: :model do
|
||||
|
||||
it 'creates a user with default values' do
|
||||
user = agent_builder.perform
|
||||
expect(user.name).to eq('')
|
||||
expect(user.name).to eq(email.split('@').first)
|
||||
expect(AccountUser.find_by(user: user).role).to eq('agent')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -188,12 +188,13 @@ RSpec.describe Crm::Leadsquared::Mappers::ConversationMapper do
|
||||
end
|
||||
|
||||
context 'when sender has no name' do
|
||||
let(:unnamed_contact) { create(:contact, account: account, name: '') }
|
||||
let(:unnamed_sender_message) do
|
||||
create(:message,
|
||||
conversation: conversation,
|
||||
sender: create(:user, name: ''),
|
||||
sender: unnamed_contact,
|
||||
content: 'Message',
|
||||
message_type: :outgoing,
|
||||
message_type: :incoming,
|
||||
created_at: Time.zone.parse('2024-01-01 10:05'))
|
||||
end
|
||||
|
||||
@@ -201,7 +202,7 @@ RSpec.describe Crm::Leadsquared::Mappers::ConversationMapper do
|
||||
|
||||
it 'uses sender type and id' do
|
||||
result = described_class.map_transcript_activity(hook, conversation)
|
||||
expect(result).to include("User #{unnamed_sender_message.sender_id}")
|
||||
expect(result).to include("Contact #{unnamed_sender_message.sender_id}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user