From 5b7bed96408a4096d663b17dd19ec841915490e5 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Mon, 27 Mar 2023 18:49:48 +0530 Subject: [PATCH] chore: Auto capitalize the name field while sending the canned response/variables (#6758) * capitalize name before sending the message * Fix specs * Code cleanups --- app/drops/contact_drop.rb | 6 +++++- app/drops/user_drop.rb | 4 ++++ app/javascript/dashboard/helper/messageHelper.js | 9 +++++++-- .../dashboard/helper/specs/messageHelper.spec.js | 16 ++++++++++++++++ spec/drops/contact_drop_spec.rb | 5 +++++ spec/drops/user_drop_spec.rb | 5 +++++ spec/models/concerns/liquidable_shared.rb | 8 ++++---- 7 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/drops/contact_drop.rb b/app/drops/contact_drop.rb index 7d5a85f0e..4560ccc23 100644 --- a/app/drops/contact_drop.rb +++ b/app/drops/contact_drop.rb @@ -1,4 +1,8 @@ class ContactDrop < BaseDrop + def name + @obj.try(:name).try(:capitalize) + end + def email @obj.try(:email) end @@ -8,7 +12,7 @@ class ContactDrop < BaseDrop end 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 def last_name diff --git a/app/drops/user_drop.rb b/app/drops/user_drop.rb index 0ef91a724..daca7fdda 100644 --- a/app/drops/user_drop.rb +++ b/app/drops/user_drop.rb @@ -1,4 +1,8 @@ class UserDrop < BaseDrop + def name + @obj.try(:name).try(:capitalize) + end + def available_name @obj.try(:available_name) end diff --git a/app/javascript/dashboard/helper/messageHelper.js b/app/javascript/dashboard/helper/messageHelper.js index 773dd086d..3e831d5ad 100644 --- a/app/javascript/dashboard/helper/messageHelper.js +++ b/app/javascript/dashboard/helper/messageHelper.js @@ -7,10 +7,15 @@ export const replaceVariablesInMessage = ({ message, variables }) => { }); }; +export const capitalizeName = string => { + return string.charAt(0).toUpperCase() + string.slice(1); +}; + const skipCodeBlocks = str => str.replace(/```(?:.|\n)+?```/g, ''); export const getFirstName = ({ user }) => { - return user?.name ? user.name.split(' ').shift() : ''; + const firstName = user?.name ? user.name.split(' ').shift() : ''; + return capitalizeName(firstName); }; export const getLastName = ({ user }) => { @@ -27,7 +32,7 @@ export const getMessageVariables = ({ conversation }) => { } = conversation; return { - 'contact.name': sender?.name, + 'contact.name': capitalizeName(sender?.name), 'contact.first_name': getFirstName({ user: sender }), 'contact.last_name': getLastName({ user: sender }), 'contact.email': sender?.email, diff --git a/app/javascript/dashboard/helper/specs/messageHelper.spec.js b/app/javascript/dashboard/helper/specs/messageHelper.spec.js index 7eac6b55e..114206b7f 100644 --- a/app/javascript/dashboard/helper/specs/messageHelper.spec.js +++ b/app/javascript/dashboard/helper/specs/messageHelper.spec.js @@ -4,6 +4,7 @@ import { getLastName, getMessageVariables, getUndefinedVariablesInMessage, + capitalizeName, } from '../messageHelper'; const variables = { @@ -136,3 +137,18 @@ describe('#getUndefinedVariablesInMessage', () => { ); }); }); + +describe('#capitalizeName', () => { + it('Capitalize name if name is passed', () => { + const string = 'hello world'; + expect(capitalizeName(string)).toBe('Hello world'); + }); + it('returns empty string if the string is empty', () => { + const string = ''; + expect(capitalizeName(string)).toBe(''); + }); + it('Capitalize first name if full name is passed', () => { + const string = 'john Doe'; + expect(capitalizeName(string)).toBe('John Doe'); + }); +}); diff --git a/spec/drops/contact_drop_spec.rb b/spec/drops/contact_drop_spec.rb index c87eb4e2a..d4c81bda8 100644 --- a/spec/drops/contact_drop_spec.rb +++ b/spec/drops/contact_drop_spec.rb @@ -10,6 +10,11 @@ describe ::ContactDrop do contact.update!(name: 'John Doe') expect(subject.first_name).to eq 'John' end + + it('return the capitalized name') do + contact.update!(name: 'john doe') + expect(subject.name).to eq 'John doe' + end end context 'when last name' do diff --git a/spec/drops/user_drop_spec.rb b/spec/drops/user_drop_spec.rb index 1af9f3014..7e136ebc4 100644 --- a/spec/drops/user_drop_spec.rb +++ b/spec/drops/user_drop_spec.rb @@ -12,6 +12,11 @@ describe ::UserDrop do end end + it('return the capitalized name') do + user.update!(name: 'peter') + expect(subject.name).to eq 'Peter' + end + context 'when last name' do it 'returns the last name' do user.update!(name: 'John Doe') diff --git a/spec/models/concerns/liquidable_shared.rb b/spec/models/concerns/liquidable_shared.rb index e488e6e4e..7648bd15c 100644 --- a/spec/models/concerns/liquidable_shared.rb +++ b/spec/models/concerns/liquidable_shared.rb @@ -21,7 +21,7 @@ shared_examples_for 'liqudable' do it 'set replaces liquid variables in message' do message.content = 'hey {{contact.name}} how are you?' message.save! - expect(message.content).to eq 'hey john how are you?' + expect(message.content).to eq 'hey John how are you?' end it 'process liquid operators like default value' do @@ -45,19 +45,19 @@ shared_examples_for 'liqudable' do it 'will not process liquid tags in multiple code blocks' do message.content = 'hey {{contact.name}} how are you? ```code: {{contact.name}}``` ``` code: {{contact.name}} ``` test `{{contact.name}}`' message.save! - expect(message.content).to eq 'hey john how are you? ```code: {{contact.name}}``` ``` code: {{contact.name}} ``` test `{{contact.name}}`' + expect(message.content).to eq 'hey John how are you? ```code: {{contact.name}}``` ``` code: {{contact.name}} ``` test `{{contact.name}}`' end it 'will not process liquid tags in single ticks' do message.content = 'hey {{contact.name}} how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} ` test' message.save! - expect(message.content).to eq 'hey john how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} ` test' + expect(message.content).to eq 'hey John how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} ` test' end it 'will not throw error for broken quotes' do message.content = 'hey {{contact.name}} how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} test' message.save! - expect(message.content).to eq 'hey john how are you? ` code: {{contact.name}} ` ` code: john test' + expect(message.content).to eq 'hey John how are you? ` code: {{contact.name}} ` ` code: John test' end end end