@periskope/types 0.6.13 → 0.6.14-5.2

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,1303 @@
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
+ ticket: TicketVariablesType;
67
+ chat: ChatVariablesType;
68
+ },
69
+ '.'
70
+ >,
71
+ {
72
+ rule: Tables<'tbl_automation_rules'>[];
73
+ id: 'reaction.reaction_id';
74
+ type: `reaction.${'created' | 'updated'}`;
75
+ org_id: string;
76
+ }
77
+ >;
78
+
79
+ export type ChatRulesInfoType = Merge<
80
+ AppendTypes<
81
+ {
82
+ chat: ChatVariablesType;
83
+ },
84
+ '.'
85
+ >,
86
+ {
87
+ rule: Tables<'tbl_automation_rules'>[];
88
+ id: 'chat.chat_id';
89
+ type: 'chat.created' | 'chat.label.updated';
90
+ org_id: string;
91
+ }
92
+ >;
93
+
94
+ export type RuleInfoType =
95
+ | MessageRulesInfoType
96
+ | ChatRulesInfoType
97
+ | TicketRulesInfoType
98
+ | ReactionRulesInfoType;
99
+
100
+ export interface Filter {
101
+ id: string;
102
+ condition:
103
+ | 'CONTAINS'
104
+ | 'NCONTAINS'
105
+ | 'EQ'
106
+ | 'NEQ'
107
+ | 'LT'
108
+ | 'LTE'
109
+ | 'GT'
110
+ | 'GTE'
111
+ | 'KNOWN'
112
+ | 'NKNOWN'
113
+ | 'IS'
114
+ | 'NIS'; // Add other condition types as needed
115
+ variable: string;
116
+ value: string | string[];
117
+ variable_type?: 'string' | 'date'; // 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
+ CONTAINS: {
138
+ name: 'contains',
139
+ input: true,
140
+ },
141
+ NCONTAINS: {
142
+ name: 'does not contain',
143
+ input: true,
144
+ },
145
+ EQ: {
146
+ name: 'equals to',
147
+ input: true,
148
+ },
149
+ NEQ: {
150
+ name: 'not equals to',
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
+ };
186
+
187
+ export type VariableNameValueType = {
188
+ text: string;
189
+ type: 'string' | 'time' | 'boolean' | 'dropdown' | 'day-time' | 'number';
190
+ value?:
191
+ | string
192
+ | {
193
+ id: string;
194
+ value: string;
195
+ label: string;
196
+ }[]
197
+ | null;
198
+ filters: Filter['condition'][];
199
+ hidden?: boolean;
200
+ placeholder?: string;
201
+ };
202
+
203
+ export const MessageVariableNameMap: Record<
204
+ keyof AppendTypes<{ message: MessageVariablesType }, '.'>,
205
+ VariableNameValueType
206
+ > = {
207
+ 'message.body': {
208
+ text: 'Message Body',
209
+ type: 'string',
210
+ filters: ['CONTAINS', 'NCONTAINS', 'EQ', 'NEQ', 'KNOWN', 'NKNOWN'],
211
+ placeholder: 'e.g. Test message',
212
+ },
213
+ 'message.sender_phone': {
214
+ text: 'Message Sender Phone',
215
+ type: 'string',
216
+ filters: ['EQ', 'NEQ'],
217
+ placeholder: 'e.g. 919876543210@c.us',
218
+ },
219
+ 'message.timestamp': {
220
+ text: 'Message Timestamp',
221
+ type: 'day-time',
222
+ filters: ['LT', 'LTE', 'GT', 'GTE', 'IS'],
223
+ },
224
+ 'message.flag_status': {
225
+ text: 'Message Flag Status',
226
+ filters: ['IS'],
227
+ type: 'boolean',
228
+
229
+ },
230
+ 'message.has_quoted_msg': {
231
+ text: 'Quoted Message',
232
+ filters: ['IS'],
233
+ type: 'boolean',
234
+ },
235
+ 'message.media': {
236
+ text: 'Message Media',
237
+ type: 'boolean',
238
+ filters: ['KNOWN', 'NKNOWN'],
239
+ },
240
+ 'message.performed_by': {
241
+ text: 'Message Performed By',
242
+ type: 'dropdown',
243
+ value: 'org.members',
244
+ filters: ['EQ', 'NEQ'],
245
+ },
246
+ 'message.chat_id': {
247
+ text: 'Chat ID',
248
+ type: 'string',
249
+ filters: ['EQ', 'NEQ'],
250
+ hidden: true,
251
+ },
252
+ 'message.message_id': {
253
+ text: 'Message ID',
254
+ type: 'string',
255
+ filters: ['EQ', 'NEQ'],
256
+ hidden: true,
257
+ },
258
+ 'message.org_phone': {
259
+ text: 'Org Phone',
260
+ type: 'string',
261
+ filters: ['EQ', 'NEQ'],
262
+ hidden: true,
263
+ },
264
+ 'message.org_id': {
265
+ text: 'Org ID',
266
+ type: 'string',
267
+ filters: ['EQ', 'NEQ'],
268
+ hidden: true,
269
+ },
270
+ 'message.message_type': {
271
+ text: 'Message Type',
272
+ type: 'dropdown',
273
+ value: [
274
+ { id: 'text', value: 'text', label: 'Text' },
275
+ { id: 'image', value: 'image', label: 'Image' },
276
+ { id: 'video', value: 'video', label: 'Video' },
277
+ { id: 'audio', value: 'audio', label: 'Audio' },
278
+ { id: 'document', value: 'document', label: 'Document' },
279
+ ],
280
+ filters: ['EQ', 'NEQ'],
281
+ },
282
+ 'message.author': {
283
+ text: 'Message Author',
284
+ type: 'string',
285
+ filters: ['EQ', 'NEQ'],
286
+ hidden: true,
287
+ },
288
+ 'message.ack': {
289
+ text: 'Message acknowledment status',
290
+ type: 'dropdown',
291
+ value: [
292
+ { id: '0', value: '0', label: '0' },
293
+ { id: '1', value: '1', label: '1' },
294
+ { id: '2', value: '2', label: '2' },
295
+ { id: '3', value: '3', label: '3' },
296
+ ],
297
+ filters: ['EQ'],
298
+ },
299
+ };
300
+
301
+ export const SenderVariableNameMap: Record<
302
+ keyof AppendTypes<{ sender: SenderVariablesType }, '.'>,
303
+ VariableNameValueType
304
+ > = {
305
+ 'sender.is_business': {
306
+ text: 'Sender is business',
307
+ type: 'boolean',
308
+ filters: ['IS'],
309
+ },
310
+ 'sender.is_enterprise': {
311
+ text: 'Sender is enterprise',
312
+ type: 'boolean',
313
+ filters: ['IS'],
314
+ },
315
+ 'sender.is_internal': {
316
+ text: 'Sender is internal',
317
+ type: 'boolean',
318
+ filters: ['IS'],
319
+ },
320
+ 'sender.contact_name': {
321
+ text: 'Sender Name',
322
+ type: 'string',
323
+ filters: ['EQ', 'NEQ', 'KNOWN', 'NKNOWN'],
324
+ placeholder: 'e.g. John Doe',
325
+ },
326
+ 'sender.contact_id': {
327
+ text: 'Sender Contact Phone',
328
+ type: 'string',
329
+ filters: ['EQ', 'NEQ'],
330
+ placeholder: 'e.g. 919876543210@c.us'
331
+ },
332
+ 'sender.labels': {
333
+ text: 'Sender Labels',
334
+ type: 'dropdown',
335
+ value: 'org.labels',
336
+ filters: ['EQ', 'NEQ'],
337
+ },
338
+ 'sender.org_id': {
339
+ text: 'Org ID',
340
+ type: 'string',
341
+ filters: ['EQ', 'NEQ'],
342
+ hidden: true,
343
+ },
344
+ 'sender.is_super_admin': {
345
+ text: 'Sender is super admin',
346
+ type: 'dropdown',
347
+ filters: ['EQ', 'NEQ'],
348
+ value: [
349
+ {
350
+ id: 'true',
351
+ value: 'true',
352
+ label: 'True',
353
+ },
354
+ {
355
+ id: 'false',
356
+ value: 'false',
357
+ label: 'False',
358
+ },
359
+ ],
360
+ },
361
+ 'sender.is_admin': {
362
+ text: 'Sender is admin',
363
+ type: 'dropdown',
364
+ filters: ['EQ', 'NEQ'],
365
+ value: [
366
+ {
367
+ id: 'true',
368
+ value: 'true',
369
+ label: 'True',
370
+ },
371
+ {
372
+ id: 'false',
373
+ value: 'false',
374
+ label: 'False',
375
+ },
376
+ ],
377
+ },
378
+ };
379
+
380
+ export const ChatVariableNameMap: Record<
381
+ keyof AppendTypes<{ chat: ChatVariablesType }, '.'>,
382
+ VariableNameValueType
383
+ > = {
384
+ 'chat.assigned_to': {
385
+ text: 'Chat Assignee',
386
+ type: 'dropdown',
387
+ value: 'org.members',
388
+ filters: ['EQ', 'NEQ', 'KNOWN', 'NKNOWN'],
389
+ },
390
+ 'chat.chat_name': {
391
+ text: 'Chat Name',
392
+ type: 'string',
393
+ filters: ['EQ', 'NEQ'],
394
+ placeholder: 'e.g. Support Chat',
395
+ },
396
+ 'chat.org_phone': {
397
+ text: 'Chat Org Phone',
398
+ type: 'dropdown',
399
+ value: 'org.org_phones',
400
+ filters: ['EQ', 'NEQ'],
401
+ },
402
+ 'chat.chat_type': {
403
+ text: 'Chat Type',
404
+ type: 'dropdown',
405
+ value: [
406
+ { id: 'user', value: 'user', label: 'User' },
407
+ { id: 'group', value: 'group', label: 'Group' },
408
+ ],
409
+ filters: ['EQ', 'NEQ'],
410
+ },
411
+ 'chat.members': {
412
+ text: 'Chat Members',
413
+ type: 'string',
414
+ filters: ['CONTAINS', 'NCONTAINS'],
415
+ placeholder: 'e.g. 919876543210'
416
+ },
417
+ 'chat.labels': {
418
+ text: 'Chat Labels',
419
+ type: 'dropdown',
420
+ value: 'org.labels',
421
+ filters: ['CONTAINS', 'NCONTAINS'],
422
+ },
423
+ 'chat.chat_id': {
424
+ text: 'Chat ID',
425
+ type: 'string',
426
+ filters: ['EQ', 'NEQ'],
427
+ placeholder: 'e.g. 12027747916749@c.us'
428
+ },
429
+ 'chat.chat_org_phones': {
430
+ text: 'Chat Org Phones',
431
+ type: 'dropdown',
432
+ filters: ['CONTAINS', 'NCONTAINS'],
433
+ hidden: true,
434
+ },
435
+ 'chat.org_id': {
436
+ text: 'Org ID',
437
+ type: 'string',
438
+ filters: [],
439
+ hidden: true,
440
+ },
441
+ 'chat.messages_admins_only': {
442
+ text: 'Messages Admins Only',
443
+ type: 'boolean',
444
+ filters: ['EQ', 'NEQ'],
445
+ value: [
446
+ {
447
+ id: 'true',
448
+ value: 'true',
449
+ label: 'True',
450
+ },
451
+ {
452
+ id: 'false',
453
+ value: 'false',
454
+ label: 'False',
455
+ },
456
+ ],
457
+ },
458
+ 'chat.is_muted': {
459
+ text: 'Chat is muted',
460
+ type: 'boolean',
461
+ filters: ['EQ', 'NEQ'],
462
+ value: [
463
+ {
464
+ id: 'true',
465
+ value: 'true',
466
+ label: 'True',
467
+ },
468
+ {
469
+ id: 'false',
470
+ value: 'false',
471
+ label: 'False',
472
+ },
473
+ ],
474
+ },
475
+ 'chat.is_exited': {
476
+ text: 'Chat is exited',
477
+ type: 'boolean',
478
+ filters: ['EQ', 'NEQ'],
479
+ value: [
480
+ {
481
+ id: 'true',
482
+ value: 'true',
483
+ label: 'True',
484
+ },
485
+ {
486
+ id: 'false',
487
+ value: 'false',
488
+ label: 'False',
489
+ },
490
+ ],
491
+ },
492
+ 'chat.is_archived': {
493
+ text: 'Chat is archived',
494
+ type: 'boolean',
495
+ filters: ['EQ', 'NEQ'],
496
+ value: [
497
+ {
498
+ id: 'true',
499
+ value: 'true',
500
+ label: 'True',
501
+ },
502
+ {
503
+ id: 'false',
504
+ value: 'false',
505
+ label: 'False',
506
+ },
507
+ ],
508
+ },
509
+ 'chat.info_admins_only': {
510
+ text: 'Info Admins Only',
511
+ type: 'boolean',
512
+ filters: ['EQ', 'NEQ'],
513
+ value: [
514
+ {
515
+ id: 'true',
516
+ value: 'true',
517
+ label: 'True',
518
+ },
519
+ {
520
+ id: 'false',
521
+ value: 'false',
522
+ label: 'False',
523
+ },
524
+ ],
525
+ },
526
+ 'chat.has_flagged_messages': {
527
+ text: 'Has Flagged Messages',
528
+ type: 'boolean',
529
+ filters: ['EQ', 'NEQ'],
530
+ value: [
531
+ {
532
+ id: 'true',
533
+ value: 'true',
534
+ label: 'True',
535
+ },
536
+ {
537
+ id: 'false',
538
+ value: 'false',
539
+ label: 'False',
540
+ },
541
+ ],
542
+ },
543
+ 'chat.group_description': {
544
+ text: 'Group Description',
545
+ type: 'string',
546
+ filters: ['CONTAINS', 'NCONTAINS', 'EQ', 'NEQ'],
547
+ placeholder: 'e.g. Group description',
548
+ },
549
+ 'chat.custom_properties': {
550
+ text: 'Custom Properties',
551
+ type: 'dropdown',
552
+ value: 'org.custom_properties',
553
+ filters: ['EQ', 'NEQ'],
554
+ },
555
+ 'chat.created_at': {
556
+ text: 'Chat Created At',
557
+ type: 'day-time',
558
+ filters: ['LT', 'LTE', 'GT', 'GTE', 'IS'],
559
+ },
560
+ };
561
+
562
+ export const TicketVariableNameMap: Record<
563
+ keyof AppendTypes<{ ticket: TicketVariablesType }, '.'>,
564
+ VariableNameValueType
565
+ > = {
566
+ 'ticket.subject': {
567
+ text: 'Ticket Subject',
568
+ type: 'string',
569
+ filters: ['CONTAINS', 'NCONTAINS', 'EQ', 'NEQ'],
570
+ placeholder: 'e.g. Test ticket',
571
+ },
572
+ 'ticket.status': {
573
+ text: 'Ticket Status',
574
+ type: 'dropdown',
575
+ value: [
576
+ { id: 'open', value: 'open', label: 'Open' },
577
+ { id: 'closed', value: 'closed', label: 'Closed' },
578
+ { id: 'inprogress', value: 'inprogress', label: 'In progress' },
579
+ ],
580
+ filters: ['EQ', 'NEQ'],
581
+ },
582
+ 'ticket.priority': {
583
+ text: 'Ticket Priority',
584
+ type: 'dropdown',
585
+ value: [
586
+ { id: '0', value: '0', label: 'No Prioriy' },
587
+ { id: '1', value: '1', label: 'Low' },
588
+ { id: '2', value: '2', label: 'Medium' },
589
+ { id: '3', value: '3', label: 'High' },
590
+ { id: '4', value: '4', label: 'Urgent' },
591
+ ],
592
+ filters: ['EQ', 'NEQ'],
593
+ },
594
+ 'ticket.assignee': {
595
+ text: 'Ticket Assignee',
596
+ type: 'dropdown',
597
+ value: 'org.members',
598
+ filters: ['EQ', 'NEQ', "KNOWN", "NKNOWN"],
599
+ },
600
+ 'ticket.labels': {
601
+ text: 'Ticket Labels',
602
+ type: 'dropdown',
603
+ value: 'org.labels',
604
+ filters: ['EQ', 'NEQ'],
605
+ },
606
+ 'ticket.is_deleted': {
607
+ text: 'Ticket is deleted',
608
+ type: 'boolean',
609
+ filters: ['IS'],
610
+ },
611
+ 'ticket.raised_by': {
612
+ text: 'Ticket Raised By',
613
+ type: 'dropdown',
614
+ value: 'org.members',
615
+ filters: ['EQ', 'NEQ'],
616
+ },
617
+ 'ticket.due_date': {
618
+ text: 'Ticket Due Date',
619
+ type: 'string',
620
+ filters: ['KNOWN', 'NKNOWN'],
621
+ },
622
+ 'ticket.ticket_id': {
623
+ text: 'Ticket ID',
624
+ type: 'string',
625
+ filters: ['EQ', 'NEQ'],
626
+ hidden: true,
627
+ },
628
+ 'ticket.org_id': {
629
+ text: 'Org ID',
630
+ type: 'string',
631
+ filters: ['EQ', 'NEQ'],
632
+ hidden: true,
633
+ },
634
+ 'ticket.custom_properties': {
635
+ text: 'Custom Properties',
636
+ type: 'dropdown',
637
+ value: 'org.custom_properties',
638
+ filters: ['EQ', 'NEQ'],
639
+ },
640
+ 'ticket.created_at': {
641
+ text: 'Ticket Created At',
642
+ type: 'day-time',
643
+ filters: ['LT', 'LTE', 'GT', 'GTE'],
644
+ },
645
+ 'ticket.chat_id': {
646
+ text: 'Chat ID',
647
+ type: 'string',
648
+ filters: ['EQ', 'NEQ'],
649
+ hidden: true,
650
+ },
651
+ };
652
+
653
+ export const ReactionVariableNameMap: Record<
654
+ keyof AppendTypes<{ reaction: ReactionVariablesType }, '.'>,
655
+ VariableNameValueType
656
+ > = {
657
+ 'reaction.reaction': {
658
+ text: 'Reaction',
659
+ type: 'string',
660
+ filters: ['EQ', 'NEQ'],
661
+ placeholder: 'e.g. 👍',
662
+ },
663
+ 'reaction.sender_id': {
664
+ text: 'Reaction Sender Phone',
665
+ type: 'string',
666
+ filters: ['EQ', 'NEQ'],
667
+ placeholder: 'e.g. 919876543210@c.us'
668
+ },
669
+ 'reaction.message_id': {
670
+ text: 'Reaction Message ID',
671
+ type: 'string',
672
+ filters: ['EQ', 'NEQ'],
673
+ hidden: true,
674
+ },
675
+ 'reaction.chat_id': {
676
+ text: 'Chat ID',
677
+ type: 'string',
678
+ filters: ['EQ', 'NEQ'],
679
+ hidden: true,
680
+ },
681
+ 'reaction.reaction_id': {
682
+ text: 'Reaction ID',
683
+ type: 'string',
684
+ filters: ['EQ', 'NEQ'],
685
+ hidden: true,
686
+ },
687
+ };
688
+
689
+ export enum FilterConditionMap {
690
+ 'CONTAINS' = 'contains',
691
+ 'NCONTAINS' = 'does not contain',
692
+ 'EQ' = '=',
693
+ 'NEQ' = '<>',
694
+ 'LT' = '<',
695
+ 'LTE' = '<=',
696
+ 'GT' = '>',
697
+ 'GTE' = '>=',
698
+ 'KNOWN' = 'is known',
699
+ 'NKNOWN' = 'is not known',
700
+ 'IS' = '=',
701
+ 'NIS' = '<>',
702
+ }
703
+
704
+ interface FilterGroup {
705
+ filters: Filter[];
706
+ condition: 'allOf' | 'anyOf';
707
+ }
708
+
709
+ export interface Conditions {
710
+ filters: FilterGroup[];
711
+ condition: 'allOf' | 'anyOf';
712
+ variables: string[];
713
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
714
+ org_phones: string[];
715
+ }
716
+
717
+ export type SendMessageAction = {
718
+ id: string;
719
+ type: 'send_message';
720
+ metadata: {
721
+ message: string;
722
+ media?: {
723
+ url: string;
724
+ type: 'image' | 'video' | 'audio' | 'document';
725
+ name: string;
726
+ };
727
+ };
728
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
729
+ };
730
+
731
+ export type NotifyHttpAction = {
732
+ id: string;
733
+ type: 'notify_http';
734
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
735
+ metadata: {
736
+ url: string;
737
+ };
738
+ };
739
+
740
+ export type CreateTicketAction = {
741
+ id: string;
742
+ type: 'create_ticket';
743
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
744
+ metadata: {
745
+ assignee?: string;
746
+ priority?: 0 | 1 | 2 | 3 | 4;
747
+ label?: string;
748
+ };
749
+ };
750
+
751
+ export type FlagMessageAction = {
752
+ id: string;
753
+ type: 'flag_message';
754
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
755
+ metadata: {};
756
+ };
757
+
758
+ export type AssignTicketAction = {
759
+ id: string;
760
+ type: 'assign_ticket';
761
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
762
+ metadata: {
763
+ email: string;
764
+ };
765
+ };
766
+
767
+ export type CloseTicketAction = {
768
+ id: string;
769
+ type: 'close_ticket';
770
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
771
+ metadata: {
772
+ closing_note: string;
773
+ };
774
+ };
775
+
776
+ export type AddChatLabelAction = {
777
+ id: string;
778
+ type: 'add_chat_label';
779
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
780
+ metadata: {
781
+ label: string;
782
+ };
783
+ };
784
+
785
+ export type AddTicketLabelAction = {
786
+ id: string;
787
+ type: 'add_ticket_label';
788
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
789
+ metadata: {
790
+ label: string;
791
+ };
792
+ };
793
+
794
+ export type AssignChatAction = {
795
+ id: string;
796
+ type: 'assign_chat';
797
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
798
+ metadata: {
799
+ email: string;
800
+ };
801
+ };
802
+
803
+ export type ForwardMessageAction = {
804
+ id: string;
805
+ type: 'forward_message';
806
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
807
+ metadata: {
808
+ chat_id: string;
809
+ prefix: string;
810
+ };
811
+ };
812
+
813
+ export type SendEmailAction = {
814
+ id: string;
815
+ type: 'send_email';
816
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
817
+ metadata: {
818
+ email: string;
819
+ subject: string;
820
+ body: string;
821
+ };
822
+ };
823
+
824
+ export type ReplyToMessageAction = {
825
+ id: string;
826
+ type: 'reply_to_message';
827
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
828
+ metadata: {
829
+ message: string;
830
+ media?: {
831
+ url: string;
832
+ type: 'image' | 'video' | 'audio' | 'document';
833
+ name: string;
834
+ };
835
+ };
836
+ };
837
+
838
+ export type UpdateCustomPropertyAction = {
839
+ id: string;
840
+ type: 'update_custom_property';
841
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
842
+ metadata: {
843
+ property_id: string;
844
+ value: string;
845
+ };
846
+ };
847
+
848
+ export type Action =
849
+ | SendMessageAction
850
+ | NotifyHttpAction
851
+ | CreateTicketAction
852
+ | FlagMessageAction
853
+ | AssignTicketAction
854
+ | CloseTicketAction
855
+ | AddChatLabelAction
856
+ | AddTicketLabelAction
857
+ | AssignChatAction
858
+ | ForwardMessageAction
859
+ | SendEmailAction
860
+ | ReplyToMessageAction
861
+ | UpdateCustomPropertyAction;
862
+
863
+ export const isSendMessageAction = (
864
+ action: Action
865
+ ): action is SendMessageAction => {
866
+ return action.type === 'send_message';
867
+ };
868
+
869
+ export type Rule = OverrideProperties<
870
+ Tables<'tbl_automation_rules'>,
871
+ {
872
+ actions: Action[];
873
+ conditions: Conditions;
874
+ trigger:
875
+ | 'message.created'
876
+ | 'message.updated'
877
+ | 'chat.created'
878
+ | 'ticket.updated'
879
+ | 'ticket.created'
880
+ | 'reaction.added'
881
+ | 'chat.label.updated'
882
+ | 'message.flagged';
883
+ }
884
+ >;
885
+
886
+ export const RuleNameMap: Record<
887
+ Rule['trigger'],
888
+ {
889
+ title: string;
890
+ description: string;
891
+ base_conditions: {
892
+ org_phone: boolean;
893
+ };
894
+ }
895
+ > = {
896
+ 'message.created': {
897
+ title: 'New Message Received',
898
+ description: 'When a new message is sent or received',
899
+ base_conditions: {
900
+ org_phone: true,
901
+ },
902
+ },
903
+ 'chat.created': {
904
+ title: 'New Chat Created',
905
+ description: 'When a new chat is created',
906
+ base_conditions: {
907
+ org_phone: true,
908
+ },
909
+ },
910
+ 'ticket.created': {
911
+ title: 'New Ticket Created',
912
+ description: 'When a new ticket is created',
913
+ base_conditions: {
914
+ org_phone: false,
915
+ },
916
+ },
917
+ 'reaction.added': {
918
+ title: 'New Reaction Added',
919
+ description: 'When a reaction is added on a message',
920
+ base_conditions: {
921
+ org_phone: true,
922
+ },
923
+ },
924
+ 'message.updated': {
925
+ title: 'Message Updated',
926
+ description: 'When a message is updated',
927
+ base_conditions: {
928
+ org_phone: true,
929
+ },
930
+ },
931
+ 'ticket.updated': {
932
+ title: 'Ticket Updated',
933
+ description: 'When a ticket is updated',
934
+ base_conditions: {
935
+ org_phone: false,
936
+ },
937
+ },
938
+ 'chat.label.updated': {
939
+ title: 'Chat Label Added/Removed',
940
+ description: 'When labels on chats are updated',
941
+ base_conditions: {
942
+ org_phone: false,
943
+ },
944
+ },
945
+ 'message.flagged': {
946
+ title: 'Message Flagged',
947
+ description: 'When a message is flagged',
948
+ base_conditions: {
949
+ org_phone: true,
950
+ },
951
+ },
952
+ };
953
+
954
+ export const ActionNameMap: Record<
955
+ Action['type'],
956
+ {
957
+ title: string;
958
+ description: string;
959
+ inputs: Record<
960
+ string,
961
+ {
962
+ id: string;
963
+ type:
964
+ | 'text'
965
+ | 'dropdown'
966
+ | 'editor'
967
+ | 'date'
968
+ | 'file'
969
+ | 'textarea'
970
+ | 'dynamic';
971
+ value:
972
+ | 'ticket.labels'
973
+ | 'org.members'
974
+ | 'chat.labels'
975
+ | 'org.chats'
976
+ | {
977
+ id: string;
978
+ value: string;
979
+ label: string;
980
+ }[]
981
+ | null
982
+ | 'org.custom_properties'
983
+ | 'custom_property_values';
984
+ label: string;
985
+ placeholder: string;
986
+ }
987
+ >;
988
+ validTriggers: Rule['trigger'][];
989
+ }
990
+ > = {
991
+ send_message: {
992
+ title: 'Send Message',
993
+ description: 'Send a message',
994
+ inputs: {
995
+ message: {
996
+ id: 'message',
997
+ type: 'editor',
998
+ label: 'Message',
999
+ placeholder: 'Enter message',
1000
+ value: null,
1001
+ },
1002
+ media: {
1003
+ id: 'media',
1004
+ type: 'file',
1005
+ label: 'Media',
1006
+ placeholder: 'Upload media',
1007
+ value: null,
1008
+ },
1009
+ },
1010
+ validTriggers: [
1011
+ 'message.created',
1012
+ 'message.updated',
1013
+ 'chat.created',
1014
+ 'ticket.updated',
1015
+ 'ticket.created',
1016
+ 'reaction.added',
1017
+ 'message.flagged',
1018
+ ],
1019
+ },
1020
+ reply_to_message: {
1021
+ title: 'Reply to message',
1022
+ description: 'Send a message with the quoted message',
1023
+ inputs: {
1024
+ message: {
1025
+ id: 'message',
1026
+ type: 'editor',
1027
+ label: 'Message',
1028
+ placeholder: 'Enter message',
1029
+ value: null,
1030
+ },
1031
+ media: {
1032
+ id: 'media',
1033
+ type: 'file',
1034
+ label: 'Media',
1035
+ placeholder: 'Upload media',
1036
+ value: null,
1037
+ },
1038
+ },
1039
+ validTriggers: [
1040
+ 'message.created',
1041
+ 'message.updated',
1042
+ 'reaction.added',
1043
+ 'message.flagged',
1044
+ ],
1045
+ },
1046
+ notify_http: {
1047
+ title: 'Notify HTTP',
1048
+ description: 'Notify an HTTP endpoint',
1049
+ inputs: {
1050
+ url: {
1051
+ id: 'url',
1052
+ type: 'text',
1053
+ label: 'URL',
1054
+ placeholder: 'Enter URL',
1055
+ value: null,
1056
+ },
1057
+ },
1058
+ validTriggers: [
1059
+ 'message.created',
1060
+ 'message.updated',
1061
+ 'chat.created',
1062
+ 'ticket.updated',
1063
+ 'ticket.created',
1064
+ 'reaction.added',
1065
+ 'message.flagged',
1066
+ ],
1067
+ },
1068
+ create_ticket: {
1069
+ title: 'Create Ticket',
1070
+ description: 'Create a ticket',
1071
+ inputs: {
1072
+ assignee: {
1073
+ id: 'assignee',
1074
+ type: 'dropdown',
1075
+ value: 'org.members',
1076
+ label: 'Assignee',
1077
+ placeholder: 'Select assignee',
1078
+ },
1079
+ priority: {
1080
+ id: 'priority',
1081
+ type: 'dropdown',
1082
+ value: [
1083
+ { id: '0', value: '0', label: 'Low' },
1084
+ { id: '1', value: '1', label: 'Medium' },
1085
+ { id: '2', value: '2', label: 'High' },
1086
+ { id: '3', value: '3', label: 'Urgent' },
1087
+ { id: '4', value: '4', label: 'Immediate' },
1088
+ ],
1089
+ label: 'Priority',
1090
+ placeholder: 'Select priority',
1091
+ },
1092
+ label: {
1093
+ id: 'label',
1094
+ type: 'dropdown',
1095
+ value: 'ticket.labels',
1096
+ label: 'Label',
1097
+ placeholder: 'Select label',
1098
+ },
1099
+ },
1100
+ validTriggers: [
1101
+ 'message.created',
1102
+ 'message.updated',
1103
+ 'chat.created',
1104
+ 'reaction.added',
1105
+ 'message.flagged',
1106
+ ],
1107
+ },
1108
+ flag_message: {
1109
+ title: 'Update flag status',
1110
+ description: 'Flag/Unflag a message',
1111
+ inputs: {
1112
+ flag: {
1113
+ id: 'flag',
1114
+ type: 'dropdown',
1115
+ value: [
1116
+ { id: 'flag', value: 'true', label: 'TRUE' },
1117
+ { id: 'unflag', value: 'false', label: 'FALSE' },
1118
+ ],
1119
+ label: 'Flag status',
1120
+ placeholder: 'Select flag',
1121
+ },
1122
+ },
1123
+ validTriggers: ['message.created', 'message.updated', 'reaction.added'],
1124
+ },
1125
+ assign_ticket: {
1126
+ title: 'Assign Ticket',
1127
+ description: 'Assign a ticket',
1128
+ inputs: {
1129
+ email: {
1130
+ id: 'email',
1131
+ type: 'dropdown',
1132
+ value: 'org.members',
1133
+ label: 'Assignee',
1134
+ placeholder: 'Select assignee',
1135
+ },
1136
+ },
1137
+ validTriggers: ['ticket.updated', 'ticket.created'],
1138
+ },
1139
+ close_ticket: {
1140
+ title: 'Close Ticket',
1141
+ description: 'Close a ticket',
1142
+ inputs: {
1143
+ closing_note: {
1144
+ id: 'closing_note',
1145
+ type: 'textarea',
1146
+ label: 'Closing Note',
1147
+ placeholder: 'Enter closing note',
1148
+ value: null,
1149
+ },
1150
+ },
1151
+ validTriggers: ['ticket.updated', 'ticket.created'],
1152
+ },
1153
+ add_chat_label: {
1154
+ title: 'Add Chat Label',
1155
+ description: 'Add a label to referred chat',
1156
+ inputs: {
1157
+ label: {
1158
+ id: 'label',
1159
+ type: 'dropdown',
1160
+ value: 'chat.labels',
1161
+ label: 'Label',
1162
+ placeholder: 'Select label',
1163
+ },
1164
+ },
1165
+ validTriggers: [
1166
+ 'message.created',
1167
+ 'message.updated',
1168
+ 'chat.created',
1169
+ 'ticket.updated',
1170
+ 'ticket.created',
1171
+ 'reaction.added',
1172
+ ],
1173
+ },
1174
+ add_ticket_label: {
1175
+ title: 'Add Ticket Label',
1176
+ description: 'Add a label to referred ticket',
1177
+ inputs: {
1178
+ label: {
1179
+ id: 'label',
1180
+ type: 'dropdown',
1181
+ value: 'ticket.labels',
1182
+ label: 'Label',
1183
+ placeholder: 'Select label',
1184
+ },
1185
+ },
1186
+ validTriggers: ['ticket.updated', 'ticket.created'],
1187
+ },
1188
+ update_custom_property: {
1189
+ description: 'Update a custom property',
1190
+ title: 'Update Chat Custom Property',
1191
+ validTriggers: [
1192
+ 'message.created',
1193
+ 'message.updated',
1194
+ 'chat.created',
1195
+ 'ticket.updated',
1196
+ 'ticket.created',
1197
+ 'reaction.added',
1198
+ 'message.flagged',
1199
+ ],
1200
+ inputs: {
1201
+ property_id: {
1202
+ id: 'property_id',
1203
+ type: 'dropdown',
1204
+ value: 'org.custom_properties',
1205
+ label: 'Property',
1206
+ placeholder: 'Select property',
1207
+ },
1208
+ value: {
1209
+ id: 'value',
1210
+ type: 'dynamic',
1211
+ label: 'Value',
1212
+ placeholder: 'Enter value',
1213
+ value: 'custom_property_values',
1214
+ },
1215
+ },
1216
+ },
1217
+ assign_chat: {
1218
+ title: 'Assign Chat',
1219
+ description: 'Assign a chat',
1220
+ inputs: {
1221
+ email: {
1222
+ id: 'email',
1223
+ type: 'dropdown',
1224
+ value: 'org.members',
1225
+ label: 'Assignee',
1226
+ placeholder: 'Select assignee',
1227
+ },
1228
+ },
1229
+ validTriggers: [
1230
+ 'message.created',
1231
+ 'message.updated',
1232
+ 'chat.created',
1233
+ 'ticket.updated',
1234
+ 'ticket.created',
1235
+ 'reaction.added',
1236
+ ],
1237
+ },
1238
+ forward_message: {
1239
+ title: 'Forward Message',
1240
+ description: 'Forward a message',
1241
+ inputs: {
1242
+ chat_id: {
1243
+ id: 'chat_id',
1244
+ type: 'dropdown',
1245
+ value: 'org.chats',
1246
+ label: 'Chat',
1247
+ placeholder: 'Select chat',
1248
+ },
1249
+ prefix: {
1250
+ id: 'prefix',
1251
+ type: 'editor',
1252
+ label: 'Prefix',
1253
+ placeholder: 'Enter prefix',
1254
+ value: null,
1255
+ },
1256
+ },
1257
+ validTriggers: [
1258
+ 'message.created',
1259
+ 'message.updated',
1260
+ 'chat.created',
1261
+ 'ticket.updated',
1262
+ 'ticket.created',
1263
+ 'reaction.added',
1264
+ 'message.flagged',
1265
+ ],
1266
+ },
1267
+ send_email: {
1268
+ title: 'Send Email',
1269
+ description: 'Send an email',
1270
+ inputs: {
1271
+ email: {
1272
+ id: 'email',
1273
+ type: 'text',
1274
+ label: 'Email',
1275
+ placeholder: 'Enter email',
1276
+ value: null,
1277
+ },
1278
+ subject: {
1279
+ id: 'subject',
1280
+ type: 'text',
1281
+ label: 'Subject',
1282
+ placeholder: 'Enter subject',
1283
+ value: null,
1284
+ },
1285
+ body: {
1286
+ id: 'body',
1287
+ type: 'editor',
1288
+ label: 'Body',
1289
+ placeholder: 'Enter body',
1290
+ value: null,
1291
+ },
1292
+ },
1293
+ validTriggers: [
1294
+ 'message.created',
1295
+ 'message.updated',
1296
+ 'chat.created',
1297
+ 'ticket.updated',
1298
+ 'ticket.created',
1299
+ 'reaction.added',
1300
+ 'message.flagged',
1301
+ ],
1302
+ },
1303
+ };