mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-30 18:47:51 +00:00
67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
const MESSAGE_VARIABLES_REGEX = /{{(.*?)}}/g;
|
|
export const replaceVariablesInMessage = ({ message, variables }) => {
|
|
return message.replace(MESSAGE_VARIABLES_REGEX, (match, replace) => {
|
|
return variables[replace.trim()]
|
|
? variables[replace.trim().toLowerCase()]
|
|
: '';
|
|
});
|
|
};
|
|
|
|
export const capitalizeName = name => {
|
|
return (name || '').replace(/\b(\w)/g, s => s.toUpperCase());
|
|
};
|
|
|
|
const skipCodeBlocks = str => str.replace(/```(?:.|\n)+?```/g, '');
|
|
|
|
export const getFirstName = ({ user }) => {
|
|
const firstName = user?.name ? user.name.split(' ').shift() : '';
|
|
return capitalizeName(firstName);
|
|
};
|
|
|
|
export const getLastName = ({ user }) => {
|
|
if (user && user.name) {
|
|
const lastName =
|
|
user.name.split(' ').length > 1 ? user.name.split(' ').pop() : '';
|
|
return capitalizeName(lastName);
|
|
}
|
|
return '';
|
|
};
|
|
|
|
export const getMessageVariables = ({ conversation }) => {
|
|
const {
|
|
meta: { assignee = {}, sender = {} },
|
|
id,
|
|
} = conversation;
|
|
|
|
return {
|
|
'contact.name': capitalizeName(sender?.name),
|
|
'contact.first_name': getFirstName({ user: sender }),
|
|
'contact.last_name': getLastName({ user: sender }),
|
|
'contact.email': sender?.email,
|
|
'contact.phone': sender?.phone_number,
|
|
'contact.id': sender?.id,
|
|
'conversation.id': id,
|
|
'agent.name': capitalizeName(assignee?.name || ''),
|
|
'agent.first_name': getFirstName({ user: assignee }),
|
|
'agent.last_name': getLastName({ user: assignee }),
|
|
'agent.email': assignee?.email ?? '',
|
|
};
|
|
};
|
|
|
|
export const getUndefinedVariablesInMessage = ({ message, variables }) => {
|
|
const messageWithOutCodeBlocks = skipCodeBlocks(message);
|
|
const matches = messageWithOutCodeBlocks.match(MESSAGE_VARIABLES_REGEX);
|
|
if (!matches) return [];
|
|
|
|
return matches
|
|
.map(match => {
|
|
return match
|
|
.replace('{{', '')
|
|
.replace('}}', '')
|
|
.trim();
|
|
})
|
|
.filter(variable => {
|
|
return !variables[variable];
|
|
});
|
|
};
|