From e7bd920c8b638e2c813ef49284ef0bdfa772c684 Mon Sep 17 00:00:00 2001 From: Khoa Nguyen Date: Thu, 27 Feb 2025 15:58:41 +0700 Subject: [PATCH 1/4] feat: add inbox support to message variables handling --- src/canned.ts | 6 ++++++ src/types/conversation.ts | 6 ++++++ test/canned.test.ts | 27 ++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/canned.ts b/src/canned.ts index 0507267..de6640d 100644 --- a/src/canned.ts +++ b/src/canned.ts @@ -4,6 +4,7 @@ import { Variables, CustomAttributes, Contact, + Inbox, } from './types/conversation'; const MESSAGE_VARIABLES_REGEX = /{{(.*?)}}/g; @@ -41,9 +42,11 @@ export const getLastName = ({ user }: { user: Sender }) => { export const getMessageVariables = ({ conversation, contact, + inbox, }: { conversation: Conversation; contact?: Contact; + inbox?: Inbox; }) => { const { meta: { assignee, sender }, @@ -60,6 +63,9 @@ export const getMessageVariables = ({ 'contact.phone': sender?.phone_number, 'contact.id': sender?.id, 'conversation.id': id, + 'conversation.code': conversation.code, + 'inbox.id': inbox?.id, + 'inbox.name': inbox?.name, 'agent.name': capitalizeName(assignee?.name || ''), 'agent.first_name': getFirstName({ user: assignee }), 'agent.last_name': getLastName({ user: assignee }), diff --git a/src/types/conversation.ts b/src/types/conversation.ts index 75a5da9..50fdeee 100644 --- a/src/types/conversation.ts +++ b/src/types/conversation.ts @@ -1,6 +1,7 @@ export interface Conversation { meta: Meta; id: number; + code: string; custom_attributes: CustomAttributes; first_reply_created_at: number; waiting_since: number; @@ -28,6 +29,11 @@ export interface Contact { custom_attributes?: CustomAttributes; } +export interface Inbox { + id: number; + name: string; +} + export interface Assignee { id: number; email?: string; diff --git a/test/canned.test.ts b/test/canned.test.ts index 2cbdd56..c63032e 100644 --- a/test/canned.test.ts +++ b/test/canned.test.ts @@ -14,6 +14,9 @@ const variables = { 'contact.email': 'john.p@example.com', 'contact.phone': '1234567890', 'conversation.id': 1, + 'conversation.code': 'CW123', + 'inbox.id': 1, + 'inbox.name': 'Inbox 1', 'agent.first_name': 'Samuel', 'agent.last_name': 'Smith', 'agent.email': 'samuel@gmail.com', @@ -52,6 +55,20 @@ describe('#replaceVariablesInMessage', () => { ); }); + it('returns the message with conversation code', () => { + const message = 'Your TicketID is #{{conversation.code}}'; + expect(replaceVariablesInMessage({ message, variables })).toBe( + 'Your TicketID is #CW123' + ); + }); + + it('returns the message with inbox name', () => { + const message = 'Welcome to {{inbox.name}}'; + expect(replaceVariablesInMessage({ message, variables })).toBe( + 'Welcome to Inbox 1' + ); + } + it('returns the message if the variable is not present in variables', () => { const message = 'Please dm me at {{contact.twitter}}'; expect(replaceVariablesInMessage({ message, variables })).toBe( @@ -102,6 +119,7 @@ describe('#getMessageVariables', () => { }, }, id: 1, + code: 'CW123', custom_attributes: { car_model: 'Tesla Model S', car_year: '2022', @@ -117,8 +135,12 @@ describe('#getMessageVariables', () => { phone_number: '1234567890', custom_attributes: { priority: 'high' }, }; + const inbox = { + id: 1, + name: 'Inbox 1', + } - expect(getMessageVariables({ conversation, contact })).toEqual({ + expect(getMessageVariables({ conversation, contact, inbox })).toEqual({ 'contact.name': 'John Doe', 'contact.first_name': 'John', 'contact.id': 3, @@ -126,6 +148,9 @@ describe('#getMessageVariables', () => { 'contact.email': 'john.doe@gmail.com', 'contact.phone': '1234567890', 'conversation.id': 1, + 'conversation.code': 'CW123', + 'inbox.id': 1, + 'inbox.name': 'Inbox 1', 'agent.name': 'Samuel Smith', 'agent.first_name': 'Samuel', 'agent.last_name': 'Smith', From 62c0a972af069c8b3282c75ac8b65ce4f1685155 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 15 Jul 2025 14:46:17 +0530 Subject: [PATCH 2/4] chore: fix specs --- test/canned.test.ts | 4 ++-- test/sla.spec.ts | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/canned.test.ts b/test/canned.test.ts index c63032e..f048497 100644 --- a/test/canned.test.ts +++ b/test/canned.test.ts @@ -67,7 +67,7 @@ describe('#replaceVariablesInMessage', () => { expect(replaceVariablesInMessage({ message, variables })).toBe( 'Welcome to Inbox 1' ); - } + }); it('returns the message if the variable is not present in variables', () => { const message = 'Please dm me at {{contact.twitter}}'; @@ -138,7 +138,7 @@ describe('#getMessageVariables', () => { const inbox = { id: 1, name: 'Inbox 1', - } + }; expect(getMessageVariables({ conversation, contact, inbox })).toEqual({ 'contact.name': 'John Doe', diff --git a/test/sla.spec.ts b/test/sla.spec.ts index f8aece5..a5b78f6 100644 --- a/test/sla.spec.ts +++ b/test/sla.spec.ts @@ -34,6 +34,7 @@ describe('SLAHelper', () => { }, }, id: 1, + code: 'CW123', custom_attributes: {}, first_reply_created_at: 0, waiting_since: 0, @@ -74,6 +75,7 @@ describe('SLAHelper', () => { }, }, id: 1, + code: 'CW123', custom_attributes: {}, first_reply_created_at: 0, waiting_since: 0, @@ -110,6 +112,7 @@ describe('SLAHelper', () => { }, }, id: 1, + code: 'CW124', custom_attributes: {}, first_reply_created_at: 1704066200, waiting_since: 1704065940, @@ -145,6 +148,7 @@ describe('SLAHelper', () => { }, }, id: 1, + code: 'CW125', custom_attributes: {}, first_reply_created_at: 1704066200, waiting_since: 1704066060, @@ -180,6 +184,7 @@ describe('SLAHelper', () => { }, }, id: 1, + code: 'CW126', custom_attributes: {}, first_reply_created_at: 1704066200, waiting_since: 0, @@ -215,6 +220,7 @@ describe('SLAHelper', () => { }, }, id: 1, + code: 'CW127', custom_attributes: {}, first_reply_created_at: 1704066200, waiting_since: 0, From 00fab04120beb01036bc24729305a3e1ce0e4dda Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 15 Jul 2025 16:14:18 +0530 Subject: [PATCH 3/4] chore: fix the specs --- src/canned.ts | 1 - test/canned.test.ts | 8 -------- 2 files changed, 9 deletions(-) diff --git a/src/canned.ts b/src/canned.ts index de6640d..34b3cff 100644 --- a/src/canned.ts +++ b/src/canned.ts @@ -63,7 +63,6 @@ export const getMessageVariables = ({ 'contact.phone': sender?.phone_number, 'contact.id': sender?.id, 'conversation.id': id, - 'conversation.code': conversation.code, 'inbox.id': inbox?.id, 'inbox.name': inbox?.name, 'agent.name': capitalizeName(assignee?.name || ''), diff --git a/test/canned.test.ts b/test/canned.test.ts index f048497..64c745f 100644 --- a/test/canned.test.ts +++ b/test/canned.test.ts @@ -55,13 +55,6 @@ describe('#replaceVariablesInMessage', () => { ); }); - it('returns the message with conversation code', () => { - const message = 'Your TicketID is #{{conversation.code}}'; - expect(replaceVariablesInMessage({ message, variables })).toBe( - 'Your TicketID is #CW123' - ); - }); - it('returns the message with inbox name', () => { const message = 'Welcome to {{inbox.name}}'; expect(replaceVariablesInMessage({ message, variables })).toBe( @@ -148,7 +141,6 @@ describe('#getMessageVariables', () => { 'contact.email': 'john.doe@gmail.com', 'contact.phone': '1234567890', 'conversation.id': 1, - 'conversation.code': 'CW123', 'inbox.id': 1, 'inbox.name': 'Inbox 1', 'agent.name': 'Samuel Smith', From 71417ddf4a6a0f6700b27f201fb8c88d76edd95d Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 15 Jul 2025 16:18:36 +0530 Subject: [PATCH 4/4] chore: bump up versions --- README.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de4c085..a50c615 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,4 @@ Feel free to send us feedback on [Twitter](https://twitter.com/chatwootapp) or [ If there's anything you'd like to chat about, please feel free to join our [Discord](https://discord.gg/cJXdrwS) chat! -_Chatwoot_ © 2017-2021, Chatwoot Inc - Released under the MIT License. +_Chatwoot_ © 2017-2025, Chatwoot Inc - Released under the MIT License. diff --git a/package.json b/package.json index 6e23a28..2089379 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@chatwoot/utils", - "version": "0.0.47", + "version": "0.0.48", "description": "Chatwoot utils", "private": false, "license": "MIT",