mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 02:57:57 +00:00 
			
		
		
		
	chore: Auto capitalize the last name field while sending the canned response/variables (#6767)
* Capitalize last name * Add more spec * Fix last name spec issue * More spec fixes * Add more spec fixes * Update user_drop_spec.rb
This commit is contained in:
		| @@ -1,6 +1,6 @@ | |||||||
| class ContactDrop < BaseDrop | class ContactDrop < BaseDrop | ||||||
|   def name |   def name | ||||||
|     @obj.try(:name).try(:capitalize) |     @obj.try(:name).try(:split).try(:map, &:capitalize).try(:join, ' ') | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def email |   def email | ||||||
| @@ -16,6 +16,6 @@ class ContactDrop < BaseDrop | |||||||
|   end |   end | ||||||
|  |  | ||||||
|   def last_name |   def last_name | ||||||
|     @obj.try(:name).try(:split).try(:last) if @obj.try(:name).try(:split).try(:size) > 1 |     @obj.try(:name).try(:split).try(:last).try(:capitalize) if @obj.try(:name).try(:split).try(:size) > 1 | ||||||
|   end |   end | ||||||
| end | end | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| class UserDrop < BaseDrop | class UserDrop < BaseDrop | ||||||
|   def name |   def name | ||||||
|     @obj.try(:name).try(:capitalize) |     @obj.try(:name).try(:split).try(:map, &:capitalize).try(:join, ' ') | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def available_name |   def available_name | ||||||
| @@ -8,10 +8,10 @@ class UserDrop < BaseDrop | |||||||
|   end |   end | ||||||
|  |  | ||||||
|   def first_name |   def first_name | ||||||
|     @obj.try(:name).try(:split).try(:first) |     @obj.try(:name).try(:split).try(:first).try(:capitalize) if @obj.try(:name).try(:split).try(:size) > 1 | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def last_name |   def last_name | ||||||
|     @obj.try(:name).try(:split).try(:last) if @obj.try(:name).try(:split).try(:size) > 1 |     @obj.try(:name).try(:split).try(:last).try(:capitalize) if @obj.try(:name).try(:split).try(:size) > 1 | ||||||
|   end |   end | ||||||
| end | end | ||||||
|   | |||||||
| @@ -7,8 +7,8 @@ export const replaceVariablesInMessage = ({ message, variables }) => { | |||||||
|   }); |   }); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| export const capitalizeName = string => { | export const capitalizeName = name => { | ||||||
|   return string.charAt(0).toUpperCase() + string.slice(1); |   return name.replace(/\b(\w)/g, s => s.toUpperCase()); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| const skipCodeBlocks = str => str.replace(/```(?:.|\n)+?```/g, ''); | const skipCodeBlocks = str => str.replace(/```(?:.|\n)+?```/g, ''); | ||||||
| @@ -20,7 +20,9 @@ export const getFirstName = ({ user }) => { | |||||||
|  |  | ||||||
| export const getLastName = ({ user }) => { | export const getLastName = ({ user }) => { | ||||||
|   if (user && user.name) { |   if (user && user.name) { | ||||||
|     return user.name.split(' ').length > 1 ? user.name.split(' ').pop() : ''; |     const lastName = | ||||||
|  |       user.name.split(' ').length > 1 ? user.name.split(' ').pop() : ''; | ||||||
|  |     return capitalizeName(lastName); | ||||||
|   } |   } | ||||||
|   return ''; |   return ''; | ||||||
| }; | }; | ||||||
| @@ -39,7 +41,7 @@ export const getMessageVariables = ({ conversation }) => { | |||||||
|     'contact.phone': sender?.phone_number, |     'contact.phone': sender?.phone_number, | ||||||
|     'contact.id': sender?.id, |     'contact.id': sender?.id, | ||||||
|     'conversation.id': id, |     'conversation.id': id, | ||||||
|     'agent.name': assignee?.name ? assignee?.name : '', |     'agent.name': capitalizeName(assignee?.name || ''), | ||||||
|     'agent.first_name': getFirstName({ user: assignee }), |     'agent.first_name': getFirstName({ user: assignee }), | ||||||
|     'agent.last_name': getLastName({ user: assignee }), |     'agent.last_name': getLastName({ user: assignee }), | ||||||
|     'agent.email': assignee?.email ?? '', |     'agent.email': assignee?.email ?? '', | ||||||
|   | |||||||
| @@ -88,11 +88,11 @@ describe('#getMessageVariables', () => { | |||||||
|     const conversation = { |     const conversation = { | ||||||
|       meta: { |       meta: { | ||||||
|         assignee: { |         assignee: { | ||||||
|           name: 'Samuel Smith', |           name: 'samuel Smith', | ||||||
|           email: 'samuel@example.com', |           email: 'samuel@example.com', | ||||||
|         }, |         }, | ||||||
|         sender: { |         sender: { | ||||||
|           name: 'John Doe', |           name: 'john Doe', | ||||||
|           email: 'john.doe@gmail.com', |           email: 'john.doe@gmail.com', | ||||||
|           phone_number: '1234567890', |           phone_number: '1234567890', | ||||||
|         }, |         }, | ||||||
| @@ -139,16 +139,28 @@ describe('#getUndefinedVariablesInMessage', () => { | |||||||
| }); | }); | ||||||
|  |  | ||||||
| describe('#capitalizeName', () => { | describe('#capitalizeName', () => { | ||||||
|   it('Capitalize name if name is passed', () => { |   it('capitalize name if name is passed', () => { | ||||||
|     const string = 'hello world'; |     const string = 'john peter'; | ||||||
|     expect(capitalizeName(string)).toBe('Hello world'); |     expect(capitalizeName(string)).toBe('John Peter'); | ||||||
|  |   }); | ||||||
|  |   it('capitalize first name if full name is passed', () => { | ||||||
|  |     const string = 'john Doe'; | ||||||
|  |     expect(capitalizeName(string)).toBe('John Doe'); | ||||||
|   }); |   }); | ||||||
|   it('returns empty string if the string is empty', () => { |   it('returns empty string if the string is empty', () => { | ||||||
|     const string = ''; |     const string = ''; | ||||||
|     expect(capitalizeName(string)).toBe(''); |     expect(capitalizeName(string)).toBe(''); | ||||||
|   }); |   }); | ||||||
|   it('Capitalize first name if full name is passed', () => { |   it('capitalize last name if last name is passed', () => { | ||||||
|     const string = 'john Doe'; |     const string = 'john doe'; | ||||||
|     expect(capitalizeName(string)).toBe('John Doe'); |     expect(capitalizeName(string)).toBe('John Doe'); | ||||||
|   }); |   }); | ||||||
|  |   it('capitalize first name if first name is passed', () => { | ||||||
|  |     const string = 'john'; | ||||||
|  |     expect(capitalizeName(string)).toBe('John'); | ||||||
|  |   }); | ||||||
|  |   it('capitalize last name if last name is passed', () => { | ||||||
|  |     const string = 'doe'; | ||||||
|  |     expect(capitalizeName(string)).toBe('Doe'); | ||||||
|  |   }); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -13,7 +13,12 @@ describe ::ContactDrop do | |||||||
|  |  | ||||||
|     it('return the capitalized name') do |     it('return the capitalized name') do | ||||||
|       contact.update!(name: 'john doe') |       contact.update!(name: 'john doe') | ||||||
|       expect(subject.name).to eq 'John doe' |       expect(subject.name).to eq 'John Doe' | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     it('return the capitalized first name') do | ||||||
|  |       contact.update!(name: 'john doe') | ||||||
|  |       expect(subject.last_name).to eq 'Doe' | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
| @@ -27,5 +32,10 @@ describe ::ContactDrop do | |||||||
|       contact.update!(name: 'John') |       contact.update!(name: 'John') | ||||||
|       expect(subject.last_name).to be_nil |       expect(subject.last_name).to be_nil | ||||||
|     end |     end | ||||||
|  |  | ||||||
|  |     it('return the capitalized last name') do | ||||||
|  |       contact.update!(name: 'john doe') | ||||||
|  |       expect(subject.last_name).to eq 'Doe' | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|   | |||||||
| @@ -10,6 +10,11 @@ describe ::UserDrop do | |||||||
|       user.update!(name: 'John Doe') |       user.update!(name: 'John Doe') | ||||||
|       expect(subject.first_name).to eq 'John' |       expect(subject.first_name).to eq 'John' | ||||||
|     end |     end | ||||||
|  |  | ||||||
|  |     it('return the capitalized first name') do | ||||||
|  |       user.update!(name: 'john doe') | ||||||
|  |       expect(subject.first_name).to eq 'John' | ||||||
|  |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   it('return the capitalized name') do |   it('return the capitalized name') do | ||||||
| @@ -27,5 +32,10 @@ describe ::UserDrop do | |||||||
|       user.update!(name: 'John') |       user.update!(name: 'John') | ||||||
|       expect(subject.last_name).to be_nil |       expect(subject.last_name).to be_nil | ||||||
|     end |     end | ||||||
|  |  | ||||||
|  |     it('return the capitalized last name') do | ||||||
|  |       user.update!(name: 'john doe') | ||||||
|  |       expect(subject.last_name).to eq 'Doe' | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Muhsin Keloth
					Muhsin Keloth