-
Notifications
You must be signed in to change notification settings - Fork 5
/
canned.ts
116 lines (104 loc) · 3.06 KB
/
canned.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
Conversation,
Sender,
Variables,
CustomAttributes,
Contact,
} from './types/conversation';
const MESSAGE_VARIABLES_REGEX = /{{(.*?)}}/g;
const skipCodeBlocks = (str: string) => str.replace(/```(?:.|\n)+?```/g, '');
export const capitalizeName = (name: string | null) => {
return (name || '').replace(/\b(\w)/g, s => s.toUpperCase());
};
export const getFirstName = ({ user }: { user: Sender }) => {
const firstName = user?.name ? user.name.split(' ').shift() : '';
return capitalizeName(firstName as string);
};
export const getLastName = ({ user }: { user: Sender }) => {
if (user && user.name) {
const lastName =
user.name.split(' ').length > 1 ? user.name.split(' ').pop() : '';
return capitalizeName(lastName as string);
}
return '';
};
export const getMessageVariables = ({
conversation,
contact,
}: {
conversation: Conversation;
contact?: Contact;
}) => {
const {
meta: { assignee, sender },
id,
custom_attributes: conversationCustomAttributes = {},
} = conversation;
const { custom_attributes: contactCustomAttributes } = contact || {};
const standardVariables = {
'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 ?? '',
};
const conversationCustomAttributeVariables = Object.entries(
conversationCustomAttributes ?? {}
).reduce((acc: CustomAttributes, [key, value]) => {
acc[`conversation.custom_attribute.${key}`] = value;
return acc;
}, {});
const contactCustomAttributeVariables = Object.entries(
contactCustomAttributes ?? {}
).reduce((acc: CustomAttributes, [key, value]) => {
acc[`contact.custom_attribute.${key}`] = value;
return acc;
}, {});
const variables = {
...standardVariables,
...conversationCustomAttributeVariables,
...contactCustomAttributeVariables,
};
return variables;
};
export const replaceVariablesInMessage = ({
message,
variables,
}: {
message: string;
variables: Variables;
}) => {
// @ts-ignore
return message?.replace(MESSAGE_VARIABLES_REGEX, (_, replace) => {
return variables[replace.trim()]
? variables[replace.trim().toLowerCase()]
: '';
});
};
export const getUndefinedVariablesInMessage = ({
message,
variables,
}: {
message: string;
variables: 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] === undefined;
});
};