@periskope/types 0.6.146 → 0.6.147

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