chore: Auto capitalize the name field while sending the canned response/variables (#6758)

* capitalize name before sending the message

* Fix specs

* Code cleanups
This commit is contained in:
Muhsin Keloth
2023-03-27 18:49:48 +05:30
committed by GitHub
parent b3850cb4fa
commit 5b7bed9640
7 changed files with 46 additions and 7 deletions

View File

@@ -1,4 +1,8 @@
class ContactDrop < BaseDrop class ContactDrop < BaseDrop
def name
@obj.try(:name).try(:capitalize)
end
def email def email
@obj.try(:email) @obj.try(:email)
end end
@@ -8,7 +12,7 @@ class ContactDrop < 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

View File

@@ -1,4 +1,8 @@
class UserDrop < BaseDrop class UserDrop < BaseDrop
def name
@obj.try(:name).try(:capitalize)
end
def available_name def available_name
@obj.try(:available_name) @obj.try(:available_name)
end end

View File

@@ -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, ''); const skipCodeBlocks = str => str.replace(/```(?:.|\n)+?```/g, '');
export const getFirstName = ({ user }) => { 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 }) => { export const getLastName = ({ user }) => {
@@ -27,7 +32,7 @@ export const getMessageVariables = ({ conversation }) => {
} = conversation; } = conversation;
return { return {
'contact.name': sender?.name, 'contact.name': capitalizeName(sender?.name),
'contact.first_name': getFirstName({ user: sender }), 'contact.first_name': getFirstName({ user: sender }),
'contact.last_name': getLastName({ user: sender }), 'contact.last_name': getLastName({ user: sender }),
'contact.email': sender?.email, 'contact.email': sender?.email,

View File

@@ -4,6 +4,7 @@ import {
getLastName, getLastName,
getMessageVariables, getMessageVariables,
getUndefinedVariablesInMessage, getUndefinedVariablesInMessage,
capitalizeName,
} from '../messageHelper'; } from '../messageHelper';
const variables = { 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');
});
});

View File

@@ -10,6 +10,11 @@ describe ::ContactDrop do
contact.update!(name: 'John Doe') contact.update!(name: 'John Doe')
expect(subject.first_name).to eq 'John' expect(subject.first_name).to eq 'John'
end end
it('return the capitalized name') do
contact.update!(name: 'john doe')
expect(subject.name).to eq 'John doe'
end
end end
context 'when last name' do context 'when last name' do

View File

@@ -12,6 +12,11 @@ describe ::UserDrop do
end end
end end
it('return the capitalized name') do
user.update!(name: 'peter')
expect(subject.name).to eq 'Peter'
end
context 'when last name' do context 'when last name' do
it 'returns the last name' do it 'returns the last name' do
user.update!(name: 'John Doe') user.update!(name: 'John Doe')

View File

@@ -21,7 +21,7 @@ shared_examples_for 'liqudable' do
it 'set replaces liquid variables in message' do it 'set replaces liquid variables in message' do
message.content = 'hey {{contact.name}} how are you?' message.content = 'hey {{contact.name}} how are you?'
message.save! message.save!
expect(message.content).to eq 'hey john how are you?' expect(message.content).to eq 'hey John how are you?'
end end
it 'process liquid operators like default value' do 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 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.content = 'hey {{contact.name}} how are you? ```code: {{contact.name}}``` ``` code: {{contact.name}} ``` test `{{contact.name}}`'
message.save! 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 end
it 'will not process liquid tags in single ticks' do 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.content = 'hey {{contact.name}} how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} ` test'
message.save! 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 end
it 'will not throw error for broken quotes' do 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.content = 'hey {{contact.name}} how are you? ` code: {{contact.name}} ` ` code: {{contact.name}} test'
message.save! 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 end
end end