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