@periskope/types 0.6.151 → 0.6.153

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/rules.types.ts ADDED
@@ -0,0 +1,1509 @@
1
+ import { Merge, OverrideProperties } from 'type-fest';
2
+ import { Tables } from './supabase.types';
3
+ import {
4
+ ChatRuleInfoType,
5
+ MessageRuleInfoType,
6
+ ReactionRuleInfoType,
7
+ SenderRuleInfoType,
8
+ TicketRuleInfoType,
9
+ } from './types';
10
+
11
+ export type AppendTypes<T extends object, O extends string> = {
12
+ [K in keyof T & string as `${K}${O}${keyof T[K] & string}`]: T[K][keyof T[K]];
13
+ };
14
+
15
+ export type TicketVariablesType = TicketRuleInfoType['ticket'];
16
+
17
+ export type MessageVariablesType = MessageRuleInfoType['message'];
18
+
19
+ export type SenderVariablesType = SenderRuleInfoType;
20
+
21
+ export type ReactionVariablesType = ReactionRuleInfoType['reaction'];
22
+
23
+ export type ChatVariablesType = ChatRuleInfoType;
24
+
25
+ export type MessageRulesInfoType = Merge<
26
+ AppendTypes<
27
+ {
28
+ message: MessageVariablesType;
29
+ sender: SenderVariablesType;
30
+ chat: ChatVariablesType;
31
+ },
32
+ '.'
33
+ >,
34
+ {
35
+ rule: Tables<'tbl_automation_rules'>[];
36
+ id: 'message.message_id';
37
+ type: `message.${'created' | 'updated' | 'flagged'}`;
38
+ org_id: string;
39
+ }
40
+ >;
41
+
42
+ export type TicketRulesInfoType = Merge<
43
+ AppendTypes<
44
+ {
45
+ ticket: TicketVariablesType;
46
+ message: MessageVariablesType;
47
+ sender: SenderVariablesType;
48
+ chat: ChatVariablesType;
49
+ },
50
+ '.'
51
+ >,
52
+ {
53
+ id: 'ticket.ticket_id';
54
+ type: `ticket.${'created' | 'updated'}`;
55
+ org_id: string;
56
+ rule: Tables<'tbl_automation_rules'>[];
57
+ }
58
+ >;
59
+
60
+ export type ReactionRulesInfoType = Merge<
61
+ AppendTypes<
62
+ {
63
+ reaction: ReactionVariablesType;
64
+ message: MessageVariablesType;
65
+ sender: SenderVariablesType;
66
+ chat: ChatVariablesType;
67
+ },
68
+ '.'
69
+ >,
70
+ {
71
+ rule: Tables<'tbl_automation_rules'>[];
72
+ id: 'reaction.reaction_id';
73
+ type: `reaction.added`;
74
+ org_id: string;
75
+ }
76
+ >;
77
+
78
+ export type ChatRulesInfoType = Merge<
79
+ AppendTypes<
80
+ {
81
+ chat: ChatVariablesType;
82
+ },
83
+ '.'
84
+ >,
85
+ {
86
+ rule: Tables<'tbl_automation_rules'>[];
87
+ id: 'chat.chat_id';
88
+ type: 'chat.created' | 'chat.label.updated';
89
+ org_id: string;
90
+ }
91
+ >;
92
+
93
+ export type RuleInfoType =
94
+ | MessageRulesInfoType
95
+ | ChatRulesInfoType
96
+ | TicketRulesInfoType
97
+ | ReactionRulesInfoType;
98
+
99
+ export interface Filter {
100
+ id: string;
101
+ condition:
102
+ | 'CONTAINS'
103
+ | 'NCONTAINS'
104
+ | 'EQ'
105
+ | 'NEQ'
106
+ | 'LT'
107
+ | 'LTE'
108
+ | 'GT'
109
+ | 'GTE'
110
+ | 'KNOWN'
111
+ | 'NKNOWN'
112
+ | 'IS'
113
+ | 'NIS'
114
+ | 'ON'; // Add other condition types as needed
115
+ variable: string;
116
+ value: string | string[];
117
+ variable_type?: 'string' | 'number' | 'boolean' | 'day-time' | 'date' | 'day'; // Optional, like 'Date'
118
+ }
119
+
120
+ export const isFilter = (filter: any): filter is Filter => {
121
+ return (
122
+ typeof filter === 'object' &&
123
+ 'id' in filter &&
124
+ 'condition' in filter &&
125
+ 'variable' in filter &&
126
+ 'value' in filter
127
+ );
128
+ };
129
+
130
+ export const FilterNameMap: Record<
131
+ Filter['condition'],
132
+ {
133
+ name: string;
134
+ input: boolean;
135
+ }
136
+ > = {
137
+ EQ: {
138
+ name: 'equals to',
139
+ input: true,
140
+ },
141
+ NEQ: {
142
+ name: 'not equals to',
143
+ input: true,
144
+ },
145
+ CONTAINS: {
146
+ name: 'contains',
147
+ input: true,
148
+ },
149
+ NCONTAINS: {
150
+ name: 'does not contain',
151
+ input: true,
152
+ },
153
+ LT: {
154
+ name: 'less than',
155
+ input: true,
156
+ },
157
+ LTE: {
158
+ name: 'less than or equals to',
159
+ input: true,
160
+ },
161
+ GT: {
162
+ name: 'greater than',
163
+ input: true,
164
+ },
165
+ GTE: {
166
+ name: 'greater than or equals to',
167
+ input: true,
168
+ },
169
+ KNOWN: {
170
+ name: 'is known',
171
+ input: false,
172
+ },
173
+ NKNOWN: {
174
+ name: 'is not known',
175
+ input: false,
176
+ },
177
+ IS: {
178
+ name: 'is',
179
+ input: true,
180
+ },
181
+ NIS: {
182
+ name: 'is not',
183
+ input: true,
184
+ },
185
+ ON: {
186
+ name: 'on',
187
+ input: true,
188
+ },
189
+ };
190
+
191
+ export type VariableNameValueType = {
192
+ text: string;
193
+ type:
194
+ | 'string'
195
+ | 'time'
196
+ | 'boolean'
197
+ | 'dropdown'
198
+ | 'day-time'
199
+ | 'number'
200
+ | 'date';
201
+ variable_type: 'string' | 'number' | 'boolean' | 'day-time' | 'date';
202
+ value?:
203
+ | string
204
+ | {
205
+ id: string;
206
+ value: string | number | boolean;
207
+ label: string;
208
+ }[]
209
+ | null;
210
+ filters:
211
+ | Partial<
212
+ Record<
213
+ Filter['condition'],
214
+ {
215
+ info?: string;
216
+ }
217
+ >
218
+ >
219
+ | Filter['condition'][];
220
+ hidden?: boolean;
221
+ placeholder?: string;
222
+ info?: string;
223
+ };
224
+
225
+ export const MessageVariableNameMap: Record<
226
+ keyof AppendTypes<{ message: MessageVariablesType }, '.'>,
227
+ VariableNameValueType
228
+ > = {
229
+ 'message.body': {
230
+ text: 'Message Body',
231
+ type: 'string',
232
+ filters: ['CONTAINS', 'NCONTAINS', 'EQ', 'NEQ', 'KNOWN', 'NKNOWN'],
233
+ placeholder: 'e.g. Test message',
234
+ variable_type: 'string',
235
+ },
236
+ 'message.sender_phone': {
237
+ text: 'Message Sender Phone',
238
+ type: 'string',
239
+ filters: ['EQ', 'NEQ'],
240
+ placeholder: 'e.g. 919876543210',
241
+ variable_type: 'string',
242
+ },
243
+ 'message.timestamp': {
244
+ text: 'Message receive timestamp',
245
+ type: 'day-time',
246
+ filters: {
247
+ LT: {
248
+ info: 'When message is received before mentioned time (UTC)',
249
+ },
250
+ LTE: {
251
+ info: 'When message is received before mentioned time (UTC)',
252
+ },
253
+ GT: {
254
+ info: 'When message is received after mentioned time (UTC)',
255
+ },
256
+ GTE: {
257
+ info: 'When message is received on or after mentioned time (UTC)',
258
+ },
259
+ ON: {
260
+ info: 'When message is received on mentioned days',
261
+ },
262
+ },
263
+ variable_type: 'day-time',
264
+ info: 'The timestamp when the message was received',
265
+ },
266
+ 'message.flag_status': {
267
+ text: 'Message Flag Status',
268
+ filters: ['IS'],
269
+ type: 'boolean',
270
+ variable_type: 'boolean',
271
+ },
272
+ 'message.has_quoted_msg': {
273
+ text: 'Quoted Message',
274
+ filters: ['IS'],
275
+ type: 'boolean',
276
+ variable_type: 'boolean',
277
+ },
278
+ 'message.media': {
279
+ text: 'Message Media',
280
+ type: 'boolean',
281
+ filters: ['KNOWN', 'NKNOWN'],
282
+ variable_type: 'boolean',
283
+ },
284
+ 'message.performed_by': {
285
+ text: 'Message Performed By',
286
+ type: 'dropdown',
287
+ value: 'org.members',
288
+ filters: ['EQ', 'NEQ'],
289
+ variable_type: 'string',
290
+ },
291
+ 'message.chat_id': {
292
+ text: 'Chat ID',
293
+ type: 'string',
294
+ filters: ['EQ', 'NEQ'],
295
+ hidden: true,
296
+ variable_type: 'string',
297
+ },
298
+ 'message.message_id': {
299
+ text: 'Message ID',
300
+ type: 'string',
301
+ filters: ['EQ', 'NEQ'],
302
+ hidden: true,
303
+ variable_type: 'string',
304
+ },
305
+ 'message.org_phone': {
306
+ text: 'Org Phone',
307
+ type: 'string',
308
+ filters: ['EQ', 'NEQ'],
309
+ hidden: true,
310
+ variable_type: 'string',
311
+ },
312
+ 'message.org_id': {
313
+ text: 'Org ID',
314
+ type: 'string',
315
+ filters: ['EQ', 'NEQ'],
316
+ hidden: true,
317
+ variable_type: 'string',
318
+ },
319
+ 'message.message_type': {
320
+ text: 'Message Type',
321
+ type: 'dropdown',
322
+ value: [
323
+ { id: 'text', value: 'chat', label: 'Text' },
324
+ { id: 'image', value: 'image', label: 'Image' },
325
+ { id: 'video', value: 'video', label: 'Video' },
326
+ { id: 'audio', value: 'audio', label: 'Audio' },
327
+ { id: 'audio', value: 'ptt', label: 'PTT (Audio voice message)' },
328
+ { id: 'document', value: 'document', label: 'Document' },
329
+ ],
330
+ filters: ['EQ', 'NEQ'],
331
+ variable_type: 'string',
332
+ },
333
+ 'message.author': {
334
+ text: 'Message Author',
335
+ type: 'string',
336
+ filters: ['EQ', 'NEQ'],
337
+ hidden: true,
338
+ variable_type: 'string',
339
+ },
340
+ };
341
+
342
+ export const SenderVariableNameMap: Record<
343
+ keyof AppendTypes<{ sender: SenderVariablesType }, '.'>,
344
+ VariableNameValueType
345
+ > = {
346
+ 'sender.is_business': {
347
+ text: 'Sender is business',
348
+ type: 'boolean',
349
+ filters: ['IS'],
350
+ variable_type: 'boolean',
351
+ },
352
+ 'sender.is_enterprise': {
353
+ text: 'Sender is enterprise',
354
+ type: 'boolean',
355
+ filters: ['IS'],
356
+ variable_type: 'boolean',
357
+ },
358
+ 'sender.is_internal': {
359
+ text: 'Sender is internal',
360
+ type: 'boolean',
361
+ filters: ['IS'],
362
+ variable_type: 'boolean',
363
+ },
364
+ 'sender.contact_name': {
365
+ text: 'Sender Name',
366
+ type: 'string',
367
+ filters: ['EQ', 'NEQ', 'KNOWN', 'NKNOWN'],
368
+ placeholder: 'e.g. John Doe',
369
+ variable_type: 'string',
370
+ },
371
+ 'sender.contact_id': {
372
+ text: 'Sender Contact Phone',
373
+ type: 'string',
374
+ filters: ['EQ', 'NEQ'],
375
+ placeholder: 'e.g. 919876543210',
376
+ variable_type: 'string',
377
+ },
378
+ 'sender.labels': {
379
+ text: 'Sender Labels',
380
+ type: 'dropdown',
381
+ value: 'org.labels',
382
+ filters: ['CONTAINS', 'NCONTAINS'],
383
+ variable_type: 'string',
384
+ },
385
+ 'sender.org_id': {
386
+ text: 'Org ID',
387
+ type: 'string',
388
+ filters: ['EQ', 'NEQ'],
389
+ hidden: true,
390
+ variable_type: 'string',
391
+ },
392
+ 'sender.is_super_admin': {
393
+ text: 'Sender is super admin',
394
+ type: 'dropdown',
395
+ filters: ['EQ', 'NEQ'],
396
+ value: [
397
+ {
398
+ id: 'true',
399
+ value: 'true',
400
+ label: 'True',
401
+ },
402
+ {
403
+ id: 'false',
404
+ value: 'false',
405
+ label: 'False',
406
+ },
407
+ ],
408
+ variable_type: 'boolean',
409
+ },
410
+ 'sender.is_admin': {
411
+ text: 'Sender is admin',
412
+ type: 'dropdown',
413
+ filters: ['EQ', 'NEQ'],
414
+ value: [
415
+ {
416
+ id: 'true',
417
+ value: 'true',
418
+ label: 'True',
419
+ },
420
+ {
421
+ id: 'false',
422
+ value: 'false',
423
+ label: 'False',
424
+ },
425
+ ],
426
+ variable_type: 'boolean',
427
+ },
428
+ };
429
+
430
+ export const ChatVariableNameMap: Record<
431
+ keyof AppendTypes<{ chat: ChatVariablesType }, '.'>,
432
+ VariableNameValueType
433
+ > = {
434
+ 'chat.assigned_to': {
435
+ text: 'Chat Assignee',
436
+ type: 'dropdown',
437
+ value: 'org.members',
438
+ filters: ['EQ', 'NEQ', 'KNOWN', 'NKNOWN'],
439
+ variable_type: 'string',
440
+ },
441
+ 'chat.chat_name': {
442
+ text: 'Chat Name',
443
+ type: 'string',
444
+ filters: ['EQ', 'NEQ', 'CONTAINS', 'NCONTAINS'],
445
+ placeholder: 'e.g. Support Chat',
446
+ variable_type: 'string',
447
+ },
448
+ 'chat.org_phone': {
449
+ text: 'Chat Org Phone',
450
+ type: 'dropdown',
451
+ value: 'org.org_phones',
452
+ filters: ['EQ', 'NEQ'],
453
+ variable_type: 'string',
454
+ },
455
+ 'chat.chat_type': {
456
+ text: 'Chat Type',
457
+ type: 'dropdown',
458
+ value: [
459
+ { id: 'user', value: 'user', label: 'User' },
460
+ { id: 'group', value: 'group', label: 'Group' },
461
+ ],
462
+ filters: ['EQ', 'NEQ'],
463
+ variable_type: 'string',
464
+ },
465
+ 'chat.members': {
466
+ text: 'Chat Members',
467
+ type: 'string',
468
+ filters: ['CONTAINS', 'NCONTAINS'],
469
+ placeholder: 'e.g. 919876543210',
470
+ variable_type: 'string',
471
+ },
472
+ 'chat.labels': {
473
+ text: 'Chat Labels',
474
+ type: 'dropdown',
475
+ value: 'org.labels',
476
+ filters: ['CONTAINS', 'NCONTAINS'],
477
+ variable_type: 'string',
478
+ },
479
+ 'chat.chat_id': {
480
+ text: 'Chat ID',
481
+ type: 'string',
482
+ filters: ['EQ', 'NEQ'],
483
+ placeholder: 'e.g. 12027747916749@c.us',
484
+ variable_type: 'string',
485
+ },
486
+ 'chat.chat_org_phones': {
487
+ text: 'Chat Org Phones',
488
+ type: 'dropdown',
489
+ filters: ['CONTAINS', 'NCONTAINS'],
490
+ hidden: true,
491
+ variable_type: 'string',
492
+ },
493
+ 'chat.org_id': {
494
+ text: 'Org ID',
495
+ type: 'string',
496
+ filters: [],
497
+ hidden: true,
498
+ variable_type: 'string',
499
+ },
500
+ 'chat.messages_admins_only': {
501
+ text: 'Chat Messages Admins Only',
502
+ type: 'boolean',
503
+ filters: ['EQ', 'NEQ'],
504
+ value: [
505
+ {
506
+ id: 'true',
507
+ value: 'true',
508
+ label: 'True',
509
+ },
510
+ {
511
+ id: 'false',
512
+ value: 'false',
513
+ label: 'False',
514
+ },
515
+ ],
516
+ variable_type: 'boolean',
517
+ },
518
+ 'chat.is_muted': {
519
+ text: 'Chat is muted',
520
+ type: 'boolean',
521
+ filters: ['EQ', 'NEQ'],
522
+ value: [
523
+ {
524
+ id: 'true',
525
+ value: 'true',
526
+ label: 'True',
527
+ },
528
+ {
529
+ id: 'false',
530
+ value: 'false',
531
+ label: 'False',
532
+ },
533
+ ],
534
+ variable_type: 'boolean',
535
+ },
536
+ 'chat.info_admins_only': {
537
+ text: 'Chat Info Admins Only',
538
+ type: 'boolean',
539
+ filters: ['EQ', 'NEQ'],
540
+ value: [
541
+ {
542
+ id: 'true',
543
+ value: 'true',
544
+ label: 'True',
545
+ },
546
+ {
547
+ id: 'false',
548
+ value: 'false',
549
+ label: 'False',
550
+ },
551
+ ],
552
+ variable_type: 'boolean',
553
+ },
554
+ 'chat.has_flagged_messages': {
555
+ text: 'Chat has flagged messages',
556
+ type: 'boolean',
557
+ filters: ['EQ', 'NEQ'],
558
+ value: [
559
+ {
560
+ id: 'true',
561
+ value: 'true',
562
+ label: 'True',
563
+ },
564
+ {
565
+ id: 'false',
566
+ value: 'false',
567
+ label: 'False',
568
+ },
569
+ ],
570
+ variable_type: 'boolean',
571
+ },
572
+ 'chat.group_description': {
573
+ text: 'Group Description',
574
+ type: 'string',
575
+ filters: ['CONTAINS', 'NCONTAINS', 'EQ', 'NEQ'],
576
+ placeholder: 'e.g. Group description',
577
+ variable_type: 'string',
578
+ },
579
+ 'chat.custom_properties': {
580
+ text: 'Chat Custom Properties',
581
+ type: 'dropdown',
582
+ value: 'org.custom_properties',
583
+ filters: ['EQ', 'NEQ'],
584
+ hidden: true,
585
+ variable_type: 'string',
586
+ },
587
+ 'chat.created_at': {
588
+ text: 'Chat Created At',
589
+ type: 'day-time',
590
+ filters: ['LT', 'LTE', 'GT', 'GTE', 'ON'],
591
+ variable_type: 'day-time',
592
+ },
593
+ };
594
+
595
+ export const TicketVariableNameMap: Record<
596
+ keyof AppendTypes<{ ticket: TicketVariablesType }, '.'>,
597
+ VariableNameValueType
598
+ > = {
599
+ 'ticket.subject': {
600
+ text: 'Ticket Subject',
601
+ type: 'string',
602
+ filters: ['CONTAINS', 'NCONTAINS', 'EQ', 'NEQ'],
603
+ placeholder: 'e.g. Test ticket',
604
+ variable_type: 'string',
605
+ },
606
+ 'ticket.status': {
607
+ text: 'Ticket Status',
608
+ type: 'dropdown',
609
+ value: [
610
+ { id: 'open', value: 'open', label: 'Open' },
611
+ { id: 'closed', value: 'closed', label: 'Closed' },
612
+ { id: 'inprogress', value: 'inprogress', label: 'In progress' },
613
+ ],
614
+ filters: ['EQ', 'NEQ'],
615
+ variable_type: 'string',
616
+ },
617
+ 'ticket.priority': {
618
+ text: 'Ticket Priority',
619
+ type: 'dropdown',
620
+ value: [
621
+ { id: '0', value: '0', label: 'No Prioriy' },
622
+ { id: '1', value: '1', label: 'Low' },
623
+ { id: '2', value: '2', label: 'Medium' },
624
+ { id: '3', value: '3', label: 'High' },
625
+ { id: '4', value: '4', label: 'Urgent' },
626
+ ],
627
+ filters: ['EQ', 'NEQ'],
628
+ variable_type: 'string',
629
+ },
630
+ 'ticket.assignee': {
631
+ text: 'Ticket Assignee',
632
+ type: 'dropdown',
633
+ value: 'org.members',
634
+ filters: ['EQ', 'NEQ', 'KNOWN', 'NKNOWN'],
635
+ variable_type: 'string',
636
+ },
637
+ 'ticket.labels': {
638
+ text: 'Ticket Labels',
639
+ type: 'dropdown',
640
+ value: 'org.labels',
641
+ filters: ['CONTAINS', 'NCONTAINS'],
642
+ variable_type: 'string',
643
+ },
644
+ 'ticket.is_deleted': {
645
+ text: 'Ticket is deleted',
646
+ type: 'boolean',
647
+ value: [
648
+ {
649
+ id: 'true',
650
+ value: 'true',
651
+ label: 'True',
652
+ },
653
+ {
654
+ id: 'false',
655
+ value: 'false',
656
+ label: 'False',
657
+ },
658
+ ],
659
+ filters: ['IS'],
660
+ variable_type: 'boolean',
661
+ },
662
+ 'ticket.raised_by': {
663
+ text: 'Ticket Raised By',
664
+ type: 'dropdown',
665
+ value: 'org.members',
666
+ filters: ['EQ', 'NEQ'],
667
+ variable_type: 'string',
668
+ },
669
+ 'ticket.due_date': {
670
+ text: 'Ticket Due Date',
671
+ type: 'date',
672
+ filters: ['KNOWN', 'NKNOWN'],
673
+ variable_type: 'date',
674
+ hidden: true,
675
+ },
676
+ 'ticket.ticket_id': {
677
+ text: 'Ticket ID',
678
+ type: 'string',
679
+ filters: ['EQ', 'NEQ'],
680
+ hidden: true,
681
+ variable_type: 'string',
682
+ },
683
+ 'ticket.org_id': {
684
+ text: 'Org ID',
685
+ type: 'string',
686
+ filters: ['EQ', 'NEQ'],
687
+ hidden: true,
688
+ variable_type: 'string',
689
+ },
690
+ 'ticket.custom_properties': {
691
+ text: 'Ticket Custom Properties',
692
+ type: 'dropdown',
693
+ value: 'org.custom_properties',
694
+ filters: ['EQ', 'NEQ'],
695
+ hidden: true,
696
+ variable_type: 'string',
697
+ },
698
+ 'ticket.created_at': {
699
+ text: 'Ticket Created At',
700
+ type: 'day-time',
701
+ filters: ['LT', 'LTE', 'GT', 'GTE', 'ON'],
702
+ variable_type: 'day-time',
703
+ },
704
+ 'ticket.chat_id': {
705
+ text: 'Chat ID',
706
+ type: 'string',
707
+ filters: ['EQ', 'NEQ'],
708
+ hidden: true,
709
+ variable_type: 'string',
710
+ },
711
+ };
712
+
713
+ export const ReactionVariableNameMap: Record<
714
+ keyof AppendTypes<{ reaction: ReactionVariablesType }, '.'>,
715
+ VariableNameValueType
716
+ > = {
717
+ 'reaction.reaction': {
718
+ text: 'Reaction',
719
+ type: 'string',
720
+ filters: ['EQ', 'NEQ'],
721
+ placeholder: 'e.g. 👍',
722
+ variable_type: 'string',
723
+ },
724
+ 'reaction.sender_id': {
725
+ text: 'Reaction Sender Phone',
726
+ type: 'string',
727
+ filters: ['EQ', 'NEQ'],
728
+ placeholder: 'e.g. 919876543210',
729
+ variable_type: 'string',
730
+ },
731
+ 'reaction.message_id': {
732
+ text: 'Reaction Message ID',
733
+ type: 'string',
734
+ filters: ['EQ', 'NEQ'],
735
+ hidden: true,
736
+ variable_type: 'string',
737
+ },
738
+ 'reaction.chat_id': {
739
+ text: 'Chat ID',
740
+ type: 'string',
741
+ filters: ['EQ', 'NEQ'],
742
+ hidden: true,
743
+ variable_type: 'string',
744
+ },
745
+ 'reaction.reaction_id': {
746
+ text: 'Reaction ID',
747
+ type: 'string',
748
+ filters: ['EQ', 'NEQ'],
749
+ hidden: true,
750
+ variable_type: 'string',
751
+ },
752
+ };
753
+
754
+ export enum FilterConditionMap {
755
+ 'CONTAINS' = 'contains',
756
+ 'NCONTAINS' = 'does not contain',
757
+ 'EQ' = '=',
758
+ 'NEQ' = '<>',
759
+ 'LT' = '<',
760
+ 'LTE' = '<=',
761
+ 'GT' = '>',
762
+ 'GTE' = '>=',
763
+ 'KNOWN' = 'is known',
764
+ 'NKNOWN' = 'is not known',
765
+ 'IS' = '=',
766
+ 'NIS' = '<>',
767
+ 'ON' = 'on',
768
+ }
769
+
770
+ interface FilterGroup {
771
+ filters: Filter[];
772
+ condition: 'allOf' | 'anyOf';
773
+ }
774
+
775
+ export interface Conditions {
776
+ filters: FilterGroup[];
777
+ condition: 'allOf' | 'anyOf';
778
+ variables: string[];
779
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
780
+ org_phones: string[];
781
+ }
782
+
783
+ export type SendMessageAction = {
784
+ id: string;
785
+ type: 'send_message';
786
+ metadata: {
787
+ message: string;
788
+ media?: {
789
+ url: string;
790
+ type: 'image' | 'video' | 'audio' | 'document';
791
+ name: string;
792
+ };
793
+ maintain_flag_status: 'true' | 'false';
794
+ };
795
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
796
+ };
797
+
798
+ export type NotifyHttpAction = {
799
+ id: string;
800
+ type: 'notify_http';
801
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
802
+ metadata: {
803
+ url: string;
804
+ };
805
+ };
806
+
807
+ export type CreateTicketAction = {
808
+ id: string;
809
+ type: 'create_ticket';
810
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
811
+ metadata: {
812
+ assignee?: string;
813
+ priority?: '0' | '1' | '2' | '3' | '4';
814
+ label?: string;
815
+ };
816
+ };
817
+
818
+ export type FlagMessageAction = {
819
+ id: string;
820
+ type: 'flag_message';
821
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
822
+ metadata: {
823
+ flag: 'true' | 'false';
824
+ };
825
+ };
826
+
827
+ export type AssignTicketAction = {
828
+ id: string;
829
+ type: 'assign_ticket';
830
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
831
+ metadata: {
832
+ assignee: string;
833
+ round_robin: 'true' | 'false';
834
+ };
835
+ };
836
+
837
+ export type CloseTicketAction = {
838
+ id: string;
839
+ type: 'close_ticket';
840
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
841
+ metadata: {
842
+ closing_note: string;
843
+ };
844
+ };
845
+
846
+ export type AddChatLabelAction = {
847
+ id: string;
848
+ type: 'add_chat_label';
849
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
850
+ metadata: {
851
+ label: string;
852
+ };
853
+ };
854
+
855
+ export type AddTicketLabelAction = {
856
+ id: string;
857
+ type: 'add_ticket_label';
858
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
859
+ metadata: {
860
+ label: string;
861
+ };
862
+ };
863
+
864
+ export type AssignChatAction = {
865
+ id: string;
866
+ type: 'assign_chat';
867
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
868
+ metadata: {
869
+ assignee: string;
870
+ round_robin: 'true' | 'false';
871
+ };
872
+ };
873
+
874
+ export type ForwardMessageAction = {
875
+ id: string;
876
+ type: 'forward_message';
877
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
878
+ metadata: {
879
+ chat_id: string;
880
+ prefix: string;
881
+ };
882
+ };
883
+
884
+ export type SendEmailAction = {
885
+ id: string;
886
+ type: 'send_email';
887
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
888
+ metadata: {
889
+ email: string;
890
+ subject: string;
891
+ body: string;
892
+ };
893
+ };
894
+
895
+ export type ReplyToMessageAction = {
896
+ id: string;
897
+ type: 'reply_to_message';
898
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
899
+ metadata: {
900
+ message: string;
901
+ media?: {
902
+ url: string;
903
+ type: 'image' | 'video' | 'audio' | 'document';
904
+ name: string;
905
+ };
906
+ maintain_flag_status: boolean;
907
+ };
908
+ };
909
+
910
+ export type UpdateCustomPropertyAction = {
911
+ id: string;
912
+ type: 'update_custom_property';
913
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
914
+ metadata: {
915
+ property_id: string;
916
+ value: string;
917
+ };
918
+ };
919
+
920
+ export type Action =
921
+ | SendMessageAction
922
+ | NotifyHttpAction
923
+ | CreateTicketAction
924
+ | FlagMessageAction
925
+ | AssignTicketAction
926
+ | CloseTicketAction
927
+ | AddChatLabelAction
928
+ | AddTicketLabelAction
929
+ | AssignChatAction
930
+ | ForwardMessageAction
931
+ | SendEmailAction
932
+ | ReplyToMessageAction
933
+ | UpdateCustomPropertyAction;
934
+
935
+ export const isSendMessageAction = (
936
+ action: Action
937
+ ): action is SendMessageAction => {
938
+ return action.type === 'send_message';
939
+ };
940
+
941
+ export type Rule = OverrideProperties<
942
+ Tables<'tbl_automation_rules'>,
943
+ {
944
+ actions: Action[];
945
+ conditions: Conditions;
946
+ trigger:
947
+ | 'message.created'
948
+ | 'message.updated'
949
+ | 'chat.created'
950
+ | 'ticket.updated'
951
+ | 'ticket.created'
952
+ | 'reaction.added'
953
+ | 'chat.label.updated'
954
+ | 'message.flagged';
955
+ }
956
+ >;
957
+
958
+ export const RuleNameMap: Record<
959
+ Rule['trigger'],
960
+ {
961
+ title: string;
962
+ description: string;
963
+ base_conditions: {
964
+ org_phone: boolean;
965
+ };
966
+ }
967
+ > = {
968
+ 'message.created': {
969
+ title: 'New Message Received',
970
+ description: 'When a new message is received from an external contact',
971
+ base_conditions: {
972
+ org_phone: true,
973
+ },
974
+ },
975
+ 'chat.created': {
976
+ title: 'New Chat Created',
977
+ description: 'When a new chat is created',
978
+ base_conditions: {
979
+ org_phone: true,
980
+ },
981
+ },
982
+ 'ticket.created': {
983
+ title: 'New Ticket Created',
984
+ description: 'When a new ticket is created',
985
+ base_conditions: {
986
+ org_phone: false,
987
+ },
988
+ },
989
+ 'reaction.added': {
990
+ title: 'New Reaction Added',
991
+ description: 'When a reaction is added on a message',
992
+ base_conditions: {
993
+ org_phone: true,
994
+ },
995
+ },
996
+ 'message.updated': {
997
+ title: 'Message Updated',
998
+ description: 'When a message is updated',
999
+ base_conditions: {
1000
+ org_phone: true,
1001
+ },
1002
+ },
1003
+ 'ticket.updated': {
1004
+ title: 'Ticket Updated',
1005
+ description: 'When a ticket is updated',
1006
+ base_conditions: {
1007
+ org_phone: false,
1008
+ },
1009
+ },
1010
+ 'chat.label.updated': {
1011
+ title: 'Chat Label Added/Removed',
1012
+ description: 'When labels on chats are updated',
1013
+ base_conditions: {
1014
+ org_phone: false,
1015
+ },
1016
+ },
1017
+ 'message.flagged': {
1018
+ title: 'Message Flagged',
1019
+ description: 'When a message is flagged',
1020
+ base_conditions: {
1021
+ org_phone: true,
1022
+ },
1023
+ },
1024
+ };
1025
+
1026
+ export const ActionNameMap: Record<
1027
+ Action['type'],
1028
+ {
1029
+ title: string;
1030
+ description: string;
1031
+ inputs: Record<
1032
+ string,
1033
+ {
1034
+ id: string;
1035
+ type:
1036
+ | 'text'
1037
+ | 'dropdown'
1038
+ | 'editor'
1039
+ | 'date'
1040
+ | 'file'
1041
+ | 'textarea'
1042
+ | 'dynamic';
1043
+ value:
1044
+ | 'ticket.labels'
1045
+ | 'org.members'
1046
+ | 'chat.labels'
1047
+ | 'org.chats'
1048
+ | {
1049
+ id: string;
1050
+ value: string;
1051
+ label: string;
1052
+ }[]
1053
+ | null
1054
+ | 'org.custom_properties'
1055
+ | 'custom_property_values';
1056
+ label: string;
1057
+ placeholder: string;
1058
+ info?: string;
1059
+ required: boolean;
1060
+ }
1061
+ >;
1062
+ validTriggers: Rule['trigger'][];
1063
+ }
1064
+ > = {
1065
+ send_message: {
1066
+ title: 'Send Message',
1067
+ description: 'Send a message',
1068
+ inputs: {
1069
+ message: {
1070
+ id: 'message',
1071
+ type: 'editor',
1072
+ label: 'Message',
1073
+ placeholder: 'Enter message',
1074
+ value: null,
1075
+ required: true,
1076
+ },
1077
+ media: {
1078
+ id: 'media',
1079
+ type: 'file',
1080
+ label: 'Media',
1081
+ placeholder: 'Upload media',
1082
+ value: null,
1083
+ required: false,
1084
+ },
1085
+ maintain_flag_status: {
1086
+ id: 'maintain_flag_status',
1087
+ type: 'dropdown',
1088
+ label: 'Maintain Flag Status',
1089
+ placeholder: 'Select...',
1090
+ value: [
1091
+ { id: 'true', value: 'true', label: 'True' },
1092
+ { id: 'false', value: 'false', label: 'False' },
1093
+ ],
1094
+ required: true,
1095
+ },
1096
+ },
1097
+ validTriggers: [
1098
+ 'message.created',
1099
+ 'message.updated',
1100
+ 'chat.created',
1101
+ 'ticket.updated',
1102
+ 'ticket.created',
1103
+ 'reaction.added',
1104
+ 'message.flagged',
1105
+ 'chat.label.updated',
1106
+ ],
1107
+ },
1108
+ reply_to_message: {
1109
+ title: 'Reply to message',
1110
+ description: 'Send a message with the quoted message',
1111
+ inputs: {
1112
+ message: {
1113
+ id: 'message',
1114
+ type: 'editor',
1115
+ label: 'Message',
1116
+ placeholder: 'Enter message',
1117
+ value: null,
1118
+ required: true,
1119
+ },
1120
+ media: {
1121
+ id: 'media',
1122
+ type: 'file',
1123
+ label: 'Media',
1124
+ placeholder: 'Upload media',
1125
+ value: null,
1126
+ required: false,
1127
+ },
1128
+ maintain_flag_status: {
1129
+ id: 'maintain_flag_status',
1130
+ type: 'dropdown',
1131
+ label: 'Maintain Flag Status',
1132
+ placeholder: 'Select...',
1133
+ value: [
1134
+ { id: 'true', value: 'true', label: 'True' },
1135
+ { id: 'false', value: 'false', label: 'False' },
1136
+ ],
1137
+ required: true,
1138
+ },
1139
+ },
1140
+ validTriggers: [
1141
+ 'message.created',
1142
+ 'message.updated',
1143
+ 'reaction.added',
1144
+ 'message.flagged',
1145
+ ],
1146
+ },
1147
+ notify_http: {
1148
+ title: 'Notify HTTP',
1149
+ description: 'Notify an HTTP endpoint',
1150
+ inputs: {
1151
+ url: {
1152
+ id: 'url',
1153
+ type: 'text',
1154
+ label: 'URL',
1155
+ placeholder: 'Enter URL',
1156
+ value: null,
1157
+ required: true,
1158
+ },
1159
+ },
1160
+ validTriggers: [
1161
+ 'message.created',
1162
+ 'message.updated',
1163
+ 'chat.created',
1164
+ 'ticket.updated',
1165
+ 'ticket.created',
1166
+ 'reaction.added',
1167
+ 'message.flagged',
1168
+ 'chat.label.updated',
1169
+ ],
1170
+ },
1171
+ create_ticket: {
1172
+ title: 'Create Ticket',
1173
+ description: 'Create a ticket',
1174
+ inputs: {
1175
+ assignee: {
1176
+ id: 'assignee',
1177
+ type: 'dropdown',
1178
+ value: 'org.members',
1179
+ label: 'Assignee',
1180
+ placeholder: 'Select assignee',
1181
+ required: false,
1182
+ },
1183
+ priority: {
1184
+ id: 'priority',
1185
+ type: 'dropdown',
1186
+ value: [
1187
+ { id: '0', value: '0', label: 'No Prioriy' },
1188
+ { id: '1', value: '1', label: 'Low' },
1189
+ { id: '2', value: '2', label: 'Medium' },
1190
+ { id: '3', value: '3', label: 'High' },
1191
+ { id: '4', value: '4', label: 'Urgent' },
1192
+ ],
1193
+ label: 'Priority',
1194
+ placeholder: 'Select priority',
1195
+ required: false,
1196
+ },
1197
+ label: {
1198
+ id: 'label',
1199
+ type: 'dropdown',
1200
+ value: 'ticket.labels',
1201
+ label: 'Label',
1202
+ placeholder: 'Select label',
1203
+ required: false,
1204
+ },
1205
+ },
1206
+ validTriggers: [
1207
+ 'message.created',
1208
+ 'message.updated',
1209
+ 'reaction.added',
1210
+ 'message.flagged',
1211
+ ],
1212
+ },
1213
+ flag_message: {
1214
+ title: 'Update flag status',
1215
+ description: 'Flag/Unflag a message',
1216
+ inputs: {
1217
+ flag: {
1218
+ id: 'flag',
1219
+ type: 'dropdown',
1220
+ value: [
1221
+ { id: 'flag', value: 'true', label: 'TRUE' },
1222
+ { id: 'unflag', value: 'false', label: 'FALSE' },
1223
+ ],
1224
+ label: 'Flag status',
1225
+ placeholder: 'Select flag',
1226
+ required: true,
1227
+ },
1228
+ },
1229
+ validTriggers: ['message.created', 'message.updated', 'reaction.added'],
1230
+ },
1231
+ assign_ticket: {
1232
+ title: 'Assign Ticket',
1233
+ description: 'Assign a ticket',
1234
+ inputs: {
1235
+ round_robin: {
1236
+ id: 'round_robin',
1237
+ type: 'dropdown',
1238
+ value: [
1239
+ { id: 'true', value: 'true', label: 'True' },
1240
+ { id: 'false', value: 'false', label: 'False' },
1241
+ ],
1242
+ label: 'Enable Round Robin',
1243
+ placeholder: 'Select...',
1244
+ info: 'If enabled, the ticket will be assigned to the next available agent in the list',
1245
+ required: false,
1246
+ },
1247
+ assignee: {
1248
+ id: 'assignee',
1249
+ type: 'dynamic',
1250
+ value: 'org.members',
1251
+ label: 'Assignee',
1252
+ placeholder: 'Select assignee',
1253
+ info: "If round robin is enabled, the ticket will be assigned to the next available agent in the list else it'll be assigned to the selected agent",
1254
+ required: true,
1255
+ },
1256
+ },
1257
+ validTriggers: ['ticket.updated', 'ticket.created'],
1258
+ },
1259
+ close_ticket: {
1260
+ title: 'Close Ticket',
1261
+ description: 'Close a ticket',
1262
+ inputs: {
1263
+ closing_note: {
1264
+ id: 'closing_note',
1265
+ type: 'textarea',
1266
+ label: 'Closing Note',
1267
+ placeholder: 'Enter closing note',
1268
+ value: null,
1269
+ required: false,
1270
+ },
1271
+ },
1272
+ validTriggers: ['ticket.updated', 'ticket.created'],
1273
+ },
1274
+ add_chat_label: {
1275
+ title: 'Add Chat Label',
1276
+ description: 'Add a label to referred chat',
1277
+ inputs: {
1278
+ label: {
1279
+ id: 'label',
1280
+ type: 'dropdown',
1281
+ value: 'chat.labels',
1282
+ label: 'Label',
1283
+ placeholder: 'Select label',
1284
+ required: true,
1285
+ },
1286
+ },
1287
+ validTriggers: [
1288
+ 'message.created',
1289
+ 'message.updated',
1290
+ 'chat.created',
1291
+ 'ticket.updated',
1292
+ 'ticket.created',
1293
+ 'reaction.added',
1294
+ 'chat.label.updated',
1295
+ ],
1296
+ },
1297
+ add_ticket_label: {
1298
+ title: 'Add Ticket Label',
1299
+ description: 'Add a label to referred ticket',
1300
+ inputs: {
1301
+ label: {
1302
+ id: 'label',
1303
+ type: 'dropdown',
1304
+ value: 'ticket.labels',
1305
+ label: 'Label',
1306
+ placeholder: 'Select label',
1307
+ required: true,
1308
+ },
1309
+ },
1310
+ validTriggers: ['ticket.updated', 'ticket.created'],
1311
+ },
1312
+ update_custom_property: {
1313
+ description: 'Update a chat custom property',
1314
+ title: 'Update Chat Custom Property',
1315
+ validTriggers: [
1316
+ 'message.created',
1317
+ 'message.updated',
1318
+ 'chat.created',
1319
+ 'ticket.updated',
1320
+ 'ticket.created',
1321
+ 'reaction.added',
1322
+ 'message.flagged',
1323
+ ],
1324
+ inputs: {
1325
+ property_id: {
1326
+ id: 'property_id',
1327
+ type: 'dropdown',
1328
+ value: 'org.custom_properties',
1329
+ label: 'Property',
1330
+ placeholder: 'Select property',
1331
+ required: true,
1332
+ },
1333
+ value: {
1334
+ id: 'property_id',
1335
+ type: 'dynamic',
1336
+ label: 'Value',
1337
+ placeholder: 'Enter value',
1338
+ value: 'custom_property_values',
1339
+ required: true,
1340
+ },
1341
+ },
1342
+ },
1343
+ assign_chat: {
1344
+ title: 'Assign Chat',
1345
+ description: 'Assign a chat',
1346
+ inputs: {
1347
+ round_robin: {
1348
+ id: 'round_robin',
1349
+ type: 'dropdown',
1350
+ value: [
1351
+ { id: 'true', value: 'true', label: 'True' },
1352
+ { id: 'false', value: 'false', label: 'False' },
1353
+ ],
1354
+ label: 'Enable Round Robin',
1355
+ placeholder: 'Select...',
1356
+ info: 'If enabled, the chat will be assigned to the next available agent in the list',
1357
+ required: false,
1358
+ },
1359
+ assignee: {
1360
+ id: 'assignee',
1361
+ type: 'dynamic',
1362
+ value: 'org.members',
1363
+ label: 'Assignee',
1364
+ placeholder: 'Select assignee',
1365
+ required: true,
1366
+ },
1367
+ },
1368
+ validTriggers: [
1369
+ 'message.created',
1370
+ 'message.updated',
1371
+ 'chat.created',
1372
+ 'ticket.updated',
1373
+ 'ticket.created',
1374
+ 'reaction.added',
1375
+ 'chat.label.updated',
1376
+ ],
1377
+ },
1378
+ forward_message: {
1379
+ title: 'Forward Message',
1380
+ description: 'Forward a message',
1381
+ inputs: {
1382
+ chat_id: {
1383
+ id: 'chat_id',
1384
+ type: 'dropdown',
1385
+ value: 'org.chats',
1386
+ label: 'Chat',
1387
+ placeholder: 'Select chat',
1388
+ required: true,
1389
+ },
1390
+ },
1391
+ validTriggers: [
1392
+ 'message.created',
1393
+ 'message.updated',
1394
+ 'ticket.updated',
1395
+ 'ticket.created',
1396
+ 'reaction.added',
1397
+ 'message.flagged',
1398
+ ],
1399
+ },
1400
+ send_email: {
1401
+ title: 'Send Email',
1402
+ description: 'Send an email',
1403
+ inputs: {
1404
+ email: {
1405
+ id: 'email',
1406
+ type: 'text',
1407
+ label: 'Email',
1408
+ placeholder: 'Enter email',
1409
+ value: null,
1410
+ required: true,
1411
+ },
1412
+ subject: {
1413
+ id: 'subject',
1414
+ type: 'text',
1415
+ label: 'Subject',
1416
+ placeholder: 'Enter subject',
1417
+ value: null,
1418
+ required: true,
1419
+ },
1420
+ body: {
1421
+ id: 'body',
1422
+ type: 'editor',
1423
+ label: 'Body',
1424
+ placeholder: 'Enter body',
1425
+ value: null,
1426
+ required: true,
1427
+ },
1428
+ },
1429
+ validTriggers: [
1430
+ 'message.created',
1431
+ 'message.updated',
1432
+ 'chat.created',
1433
+ 'ticket.updated',
1434
+ 'ticket.created',
1435
+ 'reaction.added',
1436
+ 'message.flagged',
1437
+ 'chat.label.updated',
1438
+ ],
1439
+ },
1440
+ };
1441
+
1442
+ export const editorVariables: Array<{
1443
+ label: string;
1444
+ value: string;
1445
+ validTriggers: Rule['trigger'][];
1446
+ }> = [
1447
+ {
1448
+ label: 'Message Body',
1449
+ value: 'message.body',
1450
+ validTriggers: [
1451
+ 'message.created',
1452
+ 'message.updated',
1453
+ 'reaction.added',
1454
+ 'message.flagged',
1455
+ ],
1456
+ },
1457
+ {
1458
+ label: 'Sender Name',
1459
+ value: 'sender.contact_name',
1460
+ validTriggers: [
1461
+ 'message.created',
1462
+ 'message.updated',
1463
+ 'reaction.added',
1464
+ 'message.flagged',
1465
+ ],
1466
+ },
1467
+ {
1468
+ label: 'Chat Name',
1469
+ value: 'chat.chat_name',
1470
+ validTriggers: [
1471
+ 'message.created',
1472
+ 'message.updated',
1473
+ 'reaction.added',
1474
+ 'message.flagged',
1475
+ 'chat.created',
1476
+ 'chat.label.updated',
1477
+ ],
1478
+ },
1479
+ {
1480
+ label: 'Ticket ID',
1481
+ value: 'ticket.ticket_id',
1482
+ validTriggers: ['ticket.updated', 'ticket.created'],
1483
+ },
1484
+ ];
1485
+
1486
+ export const variablesExclusionList: Record<
1487
+ Rule['trigger'],
1488
+ Array<
1489
+ | keyof AppendTypes<{ message: MessageVariablesType }, '.'>
1490
+ | keyof AppendTypes<{ sender: SenderVariablesType }, '.'>
1491
+ | keyof AppendTypes<{ chat: ChatVariablesType }, '.'>
1492
+ | keyof AppendTypes<{ ticket: TicketVariablesType }, '.'>
1493
+ | keyof AppendTypes<{ reaction: ReactionVariablesType }, '.'>
1494
+ >
1495
+ > = {
1496
+ 'ticket.created': ['ticket.is_deleted'],
1497
+ 'chat.created': ['chat.chat_id'],
1498
+ 'message.created': [
1499
+ 'message.author',
1500
+ 'message.performed_by',
1501
+ 'message.flag_status',
1502
+ 'sender.is_internal',
1503
+ ],
1504
+ 'ticket.updated': [],
1505
+ 'reaction.added': [],
1506
+ 'message.updated': [],
1507
+ 'message.flagged': [],
1508
+ 'chat.label.updated': [],
1509
+ };