Skip to content

Commit 3038df1

Browse files
feat: Add inbox support to message variables handling (#47)
* feat: add inbox support to message variables handling * chore: fix specs * chore: fix the specs * chore: bump up versions --------- Co-authored-by: Muhsin Keloth <[email protected]>
1 parent a6a1ce1 commit 3038df1

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ Feel free to send us feedback on [Twitter](https://twitter.com/chatwootapp) or [
2525

2626
If there's anything you'd like to chat about, please feel free to join our [Discord](https://discord.gg/cJXdrwS) chat!
2727

28-
_Chatwoot_ &copy; 2017-2021, Chatwoot Inc - Released under the MIT License.
28+
_Chatwoot_ &copy; 2017-2025, Chatwoot Inc - Released under the MIT License.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chatwoot/utils",
3-
"version": "0.0.47",
3+
"version": "0.0.48",
44
"description": "Chatwoot utils",
55
"private": false,
66
"license": "MIT",

src/canned.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Variables,
55
CustomAttributes,
66
Contact,
7+
Inbox,
78
} from './types/conversation';
89
const MESSAGE_VARIABLES_REGEX = /{{(.*?)}}/g;
910

@@ -41,9 +42,11 @@ export const getLastName = ({ user }: { user: Sender }) => {
4142
export const getMessageVariables = ({
4243
conversation,
4344
contact,
45+
inbox,
4446
}: {
4547
conversation: Conversation;
4648
contact?: Contact;
49+
inbox?: Inbox;
4750
}) => {
4851
const {
4952
meta: { assignee, sender },
@@ -60,6 +63,8 @@ export const getMessageVariables = ({
6063
'contact.phone': sender?.phone_number,
6164
'contact.id': sender?.id,
6265
'conversation.id': id,
66+
'inbox.id': inbox?.id,
67+
'inbox.name': inbox?.name,
6368
'agent.name': capitalizeName(assignee?.name || ''),
6469
'agent.first_name': getFirstName({ user: assignee }),
6570
'agent.last_name': getLastName({ user: assignee }),

src/types/conversation.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface Conversation {
22
meta: Meta;
33
id: number;
4+
code: string;
45
custom_attributes: CustomAttributes;
56
first_reply_created_at: number;
67
waiting_since: number;
@@ -28,6 +29,11 @@ export interface Contact {
2829
custom_attributes?: CustomAttributes;
2930
}
3031

32+
export interface Inbox {
33+
id: number;
34+
name: string;
35+
}
36+
3137
export interface Assignee {
3238
id: number;
3339
email?: string;

test/canned.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const variables = {
1414
'contact.email': '[email protected]',
1515
'contact.phone': '1234567890',
1616
'conversation.id': 1,
17+
'conversation.code': 'CW123',
18+
'inbox.id': 1,
19+
'inbox.name': 'Inbox 1',
1720
'agent.first_name': 'Samuel',
1821
'agent.last_name': 'Smith',
1922
'agent.email': '[email protected]',
@@ -52,6 +55,13 @@ describe('#replaceVariablesInMessage', () => {
5255
);
5356
});
5457

58+
it('returns the message with inbox name', () => {
59+
const message = 'Welcome to {{inbox.name}}';
60+
expect(replaceVariablesInMessage({ message, variables })).toBe(
61+
'Welcome to Inbox 1'
62+
);
63+
});
64+
5565
it('returns the message if the variable is not present in variables', () => {
5666
const message = 'Please dm me at {{contact.twitter}}';
5767
expect(replaceVariablesInMessage({ message, variables })).toBe(
@@ -102,6 +112,7 @@ describe('#getMessageVariables', () => {
102112
},
103113
},
104114
id: 1,
115+
code: 'CW123',
105116
custom_attributes: {
106117
car_model: 'Tesla Model S',
107118
car_year: '2022',
@@ -117,15 +128,21 @@ describe('#getMessageVariables', () => {
117128
phone_number: '1234567890',
118129
custom_attributes: { priority: 'high' },
119130
};
131+
const inbox = {
132+
id: 1,
133+
name: 'Inbox 1',
134+
};
120135

121-
expect(getMessageVariables({ conversation, contact })).toEqual({
136+
expect(getMessageVariables({ conversation, contact, inbox })).toEqual({
122137
'contact.name': 'John Doe',
123138
'contact.first_name': 'John',
124139
'contact.id': 3,
125140
'contact.last_name': 'Doe',
126141
'contact.email': '[email protected]',
127142
'contact.phone': '1234567890',
128143
'conversation.id': 1,
144+
'inbox.id': 1,
145+
'inbox.name': 'Inbox 1',
129146
'agent.name': 'Samuel Smith',
130147
'agent.first_name': 'Samuel',
131148
'agent.last_name': 'Smith',

test/sla.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('SLAHelper', () => {
3434
},
3535
},
3636
id: 1,
37+
code: 'CW123',
3738
custom_attributes: {},
3839
first_reply_created_at: 0,
3940
waiting_since: 0,
@@ -74,6 +75,7 @@ describe('SLAHelper', () => {
7475
},
7576
},
7677
id: 1,
78+
code: 'CW123',
7779
custom_attributes: {},
7880
first_reply_created_at: 0,
7981
waiting_since: 0,
@@ -110,6 +112,7 @@ describe('SLAHelper', () => {
110112
},
111113
},
112114
id: 1,
115+
code: 'CW124',
113116
custom_attributes: {},
114117
first_reply_created_at: 1704066200,
115118
waiting_since: 1704065940,
@@ -145,6 +148,7 @@ describe('SLAHelper', () => {
145148
},
146149
},
147150
id: 1,
151+
code: 'CW125',
148152
custom_attributes: {},
149153
first_reply_created_at: 1704066200,
150154
waiting_since: 1704066060,
@@ -180,6 +184,7 @@ describe('SLAHelper', () => {
180184
},
181185
},
182186
id: 1,
187+
code: 'CW126',
183188
custom_attributes: {},
184189
first_reply_created_at: 1704066200,
185190
waiting_since: 0,
@@ -215,6 +220,7 @@ describe('SLAHelper', () => {
215220
},
216221
},
217222
id: 1,
223+
code: 'CW127',
218224
custom_attributes: {},
219225
first_reply_created_at: 1704066200,
220226
waiting_since: 0,

0 commit comments

Comments
 (0)