Skip to content

Commit

Permalink
feat: add custom variables
Browse files Browse the repository at this point in the history
  • Loading branch information
muhsin-k committed Dec 7, 2023
1 parent c413ba9 commit 9034696
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/canned.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Conversation, Sender, Variables } from './types/conversation';
import {
Conversation,
Sender,
Variables,
CustomAttributes,
} from './types/conversation';
const MESSAGE_VARIABLES_REGEX = /{{(.*?)}}/g;

const skipCodeBlocks = (str: string) => str.replace(/```(?:.|\n)+?```/g, '');
Expand Down Expand Up @@ -29,9 +34,11 @@ export const getMessageVariables = ({
const {
meta: { assignee, sender },
id,
custom_attributes: conversationCustomAttributes = {},
} = conversation;
const { custom_attributes: contactCustomAttributes } = sender || {};

return {
const standardVariables = {
'contact.name': capitalizeName(sender?.name || ''),
'contact.first_name': getFirstName({ user: sender }),
'contact.last_name': getLastName({ user: sender }),
Expand All @@ -44,6 +51,27 @@ export const getMessageVariables = ({
'agent.last_name': getLastName({ user: assignee }),
'agent.email': assignee?.email ?? '',
};
const conversationCustomAttributeVariables = Object.entries(
conversationCustomAttributes as CustomAttributes
).reduce((acc: CustomAttributes, [key, value]) => {
acc[`conversation.custom_attribute.${key}`] = value;
return acc;
}, {});

const contactCustomAttributeVariables = Object.entries(
contactCustomAttributes as CustomAttributes
).reduce((acc: CustomAttributes, [key, value]) => {
acc[`contact.custom_attribute.${key}`] = value;
return acc;
}, {});

const variables = {
...standardVariables,
...conversationCustomAttributeVariables,
...contactCustomAttributeVariables,
};

return variables;
};

export const replaceVariablesInMessage = ({
Expand Down
6 changes: 6 additions & 0 deletions src/types/conversation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Conversation {
meta: Meta;
id: number;
custom_attributes: CustomAttributes;
}

export interface Meta {
Expand All @@ -13,6 +14,7 @@ export interface Sender {
email?: string;
name?: string;
phone_number?: string;
custom_attributes?: CustomAttributes;
}

export interface Assignee {
Expand All @@ -25,3 +27,7 @@ export interface Assignee {
export interface Variables {
[key: string]: string | number;
}

export interface CustomAttributes {
[key: string]: any;
}
8 changes: 8 additions & 0 deletions test/canned.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@ describe('#getMessageVariables', () => {
name: 'john Doe',
email: '[email protected]',
phone_number: '1234567890',
custom_attributes: { priority: 'high' },
},
},
id: 1,
custom_attributes: {
car_model: 'Tesla Model S',
car_year: '2022',
},
};
expect(getMessageVariables({ conversation })).toEqual({
'contact.name': 'John Doe',
Expand All @@ -114,6 +119,9 @@ describe('#getMessageVariables', () => {
'agent.first_name': 'Samuel',
'agent.last_name': 'Smith',
'agent.email': '[email protected]',
'contact.custom_attribute.priority': 'high',
'conversation.custom_attribute.car_model': 'Tesla Model S',
'conversation.custom_attribute.car_year': '2022',
});
});
});
Expand Down

0 comments on commit 9034696

Please sign in to comment.