Skip to content

Commit b94af15

Browse files
Merge pull request #122 from freshworks/convo_props_l4
FC-132051 : Agent updates a conversation property cancellation trigger error
2 parents 9e907a7 + 849fca9 commit b94af15

File tree

8 files changed

+181
-17
lines changed

8 files changed

+181
-17
lines changed

.github/workflows/continuous-integration-workflow.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616
steps:
1717
- uses: actions/checkout@v2
1818

19-
- name: Set Node.js 12.22.1
19+
- name: Set Node.js 18.20.4
2020
uses: actions/setup-node@master
2121
with:
22-
node-version: 12.22.1
22+
node-version: 18.20.4
2323

2424
- name: Install dependencies
2525
run: yarn
@@ -36,10 +36,10 @@ jobs:
3636
steps:
3737
- uses: actions/checkout@v2
3838

39-
- name: Set Node.js 12.22.1
39+
- name: Set Node.js 18.20.4
4040
uses: actions/setup-node@master
4141
with:
42-
node-version: 12.22.1
42+
node-version: 18.20.4
4343

4444
- name: Install dependencies
4545
run: yarn

packages/rule-engine/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@freshworks-jaya/rule-engine",
3-
"version": "0.18.7",
3+
"version": "0.18.8-beta-5",
44
"description": "Provides methods to process rules in product events in marketplace app",
55
"repository": "git@github.com:freshdesk/jaya-lib.git",
66
"main": "lib/index.js",

packages/rule-engine/src/recommended/trigger-actions/update-conversation-property.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ import { Event, ProductEventData } from '@freshworks-jaya/marketplace-models';
22
import { TriggerAction } from '../../models/rule';
33

44
export default (productEvent: Event, productEventData: ProductEventData, triggerAction: TriggerAction): boolean => {
5-
const keys = Object.keys(productEventData.changes.model_changes);
6-
const match = keys.find((value) => /^cf_[a-zA-Z0-9_]+/.test(value));
5+
const { actor, changes } = productEventData;
6+
const { model_changes } = changes || {};
7+
const { assigned_agent_id, assigned_group_id, status, priority } = model_changes || {};
8+
9+
const match = model_changes && Object.keys(model_changes).find((key) => /^cf_[a-zA-Z0-9_]+/.test(key));
710

811
return (
912
productEvent === Event.ConversationUpdate &&
10-
productEventData.actor?.actor_source != null &&
11-
productEventData.actor?.actor_source != 'API' &&
12-
!!productEventData.changes.model_changes &&
13-
(!!productEventData.changes.model_changes.assigned_agent_id ||
14-
!!productEventData.changes.model_changes.assigned_group_id ||
15-
!!productEventData.changes.model_changes.status ||
16-
!!productEventData.changes.model_changes.priority ||
17-
match !== undefined)
13+
actor?.actor_source != null &&
14+
actor.actor_source !== 'API' &&
15+
!!model_changes &&
16+
!!(assigned_agent_id || assigned_group_id || status || priority || match)
1817
);
19-
};
18+
};

packages/rule-engine/test/ActionExecutor.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('ActionExecutor test', () => {
5555
marketplaceServices: {
5656
requestProxy: {} as unknown as RequestProxy,
5757
},
58+
featureFlags:{},
5859
timezoneOffset: -330,
5960
},
6061
convFieldsMap,

packages/rule-engine/test/RuleProcessor.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ describe('RuleProcessor test', () => {
216216
maxProductEventDelay: 30000,
217217
},
218218
).then((rule) => {
219-
assert.equal(rule.name, matchingRules[0].name);
219+
if (rule) {
220+
assert.equal(rule.name, matchingRules[0].name);
221+
} else {
222+
assert.fail('Expected rule to be non-null');
223+
}
220224
});
221225
});
222226
});

packages/rule-engine/test/TimerRuleEngine.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ describe('TimerRuleEngine test', () => {
221221
marketplaceServices: {
222222
requestProxy: {} as unknown as RequestProxy,
223223
},
224+
featureFlags:{},
224225
timezoneOffset: -330,
225226
},
226227
);
@@ -258,6 +259,7 @@ describe('TimerRuleEngine test', () => {
258259
marketplaceServices: {
259260
requestProxy: {} as unknown as RequestProxy,
260261
},
262+
featureFlags:{},
261263
timezoneOffset: -330,
262264
},
263265
);
@@ -295,6 +297,7 @@ describe('TimerRuleEngine test', () => {
295297
marketplaceServices: {
296298
requestProxy: {} as unknown as RequestProxy,
297299
},
300+
featureFlags:{},
298301
timezoneOffset: -330,
299302
},
300303
);
@@ -332,6 +335,7 @@ describe('TimerRuleEngine test', () => {
332335
marketplaceServices: {
333336
requestProxy: {} as unknown as RequestProxy,
334337
},
338+
featureFlags:{},
335339
timezoneOffset: -330,
336340
},
337341
);
@@ -369,6 +373,7 @@ describe('TimerRuleEngine test', () => {
369373
marketplaceServices: {
370374
requestProxy: {} as unknown as RequestProxy,
371375
},
376+
featureFlags:{},
372377
timezoneOffset: -330,
373378
},
374379
);
@@ -407,6 +412,7 @@ describe('TimerRuleEngine test', () => {
407412
marketplaceServices: {
408413
requestProxy: {} as unknown as RequestProxy,
409414
},
415+
featureFlags:{},
410416
timezoneOffset: -330,
411417
},
412418
);

packages/rule-engine/test/Utils.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('Utils test', () => {
3030
marketplaceServices: {
3131
requestProxy: {} as unknown as RequestProxy,
3232
},
33+
featureFlags:{},
3334
timezoneOffset: -330,
3435
};
3536

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { expect } from 'chai';
2+
import { Event, ProductEventData, ActorType, ResponseDueType, ConversationStatus, MessageSource,MessageType, ConversationSource, ChangedStatus } from '@freshworks-jaya/marketplace-models';
3+
import updateConversationProperty from '../src/recommended/trigger-actions/update-conversation-property';
4+
import { TriggerAction } from '../src/models/rule';
5+
6+
const productEventPayload: unknown = {
7+
actor: {
8+
last_name: 'Doe',
9+
first_name: 'John',
10+
email: 'some-agent-email',
11+
type: 'agent',
12+
avatar: {
13+
url: 'some-avatar-url',
14+
},
15+
id: 'some-agent-id',
16+
phone: 'some-agent-phone',
17+
},
18+
message: {
19+
created_time: '2020-04-03T08:26:55.782Z',
20+
conversation_id: 'some-conv-id',
21+
response_due_type: 'NO_RESPONSE_DUE',
22+
user_id: 'some-user-id',
23+
channel_id: 'some-channel-id',
24+
reopened_time: '2020-04-05T16:58:52.806Z',
25+
app_id: 'some-app-id',
26+
status: 'new',
27+
messages: [
28+
{
29+
created_time: '2020-04-06T05:01:40.601Z',
30+
conversation_id: 'some-conv-id',
31+
id: 'some-message-id',
32+
user_id: 'some-user-id',
33+
message_source: 'web',
34+
message_type: 'normal',
35+
message_parts: [
36+
{
37+
text: {
38+
content: 'hi',
39+
},
40+
},
41+
],
42+
app_id: 'some-app-id',
43+
},
44+
],
45+
},
46+
associations: {
47+
channel: {
48+
public: true,
49+
name: 'Inbox',
50+
welcome_message: {
51+
message_parts: [
52+
{
53+
text: {
54+
content: 'Hello there!',
55+
},
56+
},
57+
],
58+
message_type: 'normal',
59+
message_source: 'system',
60+
},
61+
updated_time: '2020-04-03T08:05:43.028Z',
62+
id: 'some-channel-id',
63+
tags: [],
64+
icon: {},
65+
locale: '',
66+
enabled: true,
67+
},
68+
user: {
69+
last_name: 'some-user-last-name',
70+
properties: [
71+
{
72+
name: 'fc_user_timezone',
73+
value: 'Asia/Calcutta',
74+
},
75+
],
76+
first_name: 'some-user-first-name',
77+
created_time: '2020-04-03T08:26:55.409Z',
78+
avatar: {},
79+
id: 'some-user-id',
80+
},
81+
}
82+
};
83+
84+
describe('updateConversationProperty', () => {
85+
it('should return true for valid conversation update with model changes', () => {
86+
const productEvent = Event.ConversationUpdate;
87+
const productEventData = productEventPayload as ProductEventData;
88+
89+
productEventData.actor.actor_source = 'USER';
90+
productEventData.changes = {
91+
model_changes : {
92+
"assigned_agent_id": ['agent_123', 'agent_456'],
93+
}
94+
} as any;
95+
const triggerAction: TriggerAction = { type: 'action' } as any;
96+
97+
const result = updateConversationProperty(productEvent, productEventData, triggerAction);
98+
expect(result).to.be.true;
99+
});
100+
101+
it('should return false if actor source is API', () => {
102+
const productEvent = Event.ConversationUpdate;
103+
const productEventData = productEventPayload as ProductEventData;
104+
105+
productEventData.actor.actor_source = 'API'
106+
const triggerAction: TriggerAction = { type: 'action' } as any;
107+
108+
const result = updateConversationProperty(productEvent, productEventData, triggerAction);
109+
expect(result).to.be.false;
110+
});
111+
112+
it('should return false if model changes are empty', () => {
113+
const productEvent = Event.ConversationUpdate;
114+
const productEventData = productEventPayload as ProductEventData;
115+
116+
productEventData.actor.actor_source = 'USER';
117+
productEventData.changes = {model_changes : {} } as any;
118+
const triggerAction: TriggerAction = { type: 'action' } as any;
119+
120+
121+
const result = updateConversationProperty(productEvent, productEventData, triggerAction);
122+
expect(result).to.be.false;
123+
});
124+
125+
it('should return true if custom field matches the pattern', () => {
126+
const productEvent = Event.ConversationUpdate;
127+
const productEventData = productEventPayload as ProductEventData;
128+
129+
productEventData.actor.actor_source = 'USER';
130+
productEventData.changes = {
131+
model_changes :{
132+
"cf_custom_filed": ['agent_123', 'agent_456'],
133+
}
134+
} as any;
135+
const triggerAction: TriggerAction = { type: 'action' } as any;
136+
137+
138+
const result = updateConversationProperty(productEvent, productEventData, triggerAction);
139+
expect(result).to.be.true;
140+
});
141+
142+
it('should return false for non-conversation update events', () => {
143+
const productEvent = Event.MessageCreate;
144+
const productEventData = productEventPayload as ProductEventData;
145+
146+
productEventData.actor.actor_source = 'AGENT';
147+
const triggerAction: TriggerAction = { type: 'action' } as any;
148+
149+
150+
const result = updateConversationProperty(productEvent, productEventData, triggerAction);
151+
expect(result).to.be.false;
152+
});
153+
});

0 commit comments

Comments
 (0)