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", diff --git a/src/canned.ts b/src/canned.ts index 0507267..34b3cff 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,8 @@ export const getMessageVariables = ({ 'contact.phone': sender?.phone_number, 'contact.id': sender?.id, 'conversation.id': id, + '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..64c745f 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,13 @@ describe('#replaceVariablesInMessage', () => { ); }); + 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 +112,7 @@ describe('#getMessageVariables', () => { }, }, id: 1, + code: 'CW123', custom_attributes: { car_model: 'Tesla Model S', car_year: '2022', @@ -117,8 +128,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 +141,8 @@ describe('#getMessageVariables', () => { 'contact.email': 'john.doe@gmail.com', 'contact.phone': '1234567890', 'conversation.id': 1, + 'inbox.id': 1, + 'inbox.name': 'Inbox 1', 'agent.name': 'Samuel Smith', 'agent.first_name': 'Samuel', 'agent.last_name': 'Smith', 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,