1
+ import { CreateTriggerDto } from "../../api/v1/data-contracts" ;
2
+ import { triggerControllerCreate , triggerControllerFindAll , triggerControllerRemove } from "../../api/v1/trigger" ;
3
+ import { AppSchema } from "../../schema/app" ;
4
+ import * as Table from 'cli-table3'
5
+ import { getEmoji } from "../../util/print" ;
6
+ import { formatDate } from "../../util/format" ;
7
+
8
+ export async function list ( ) : Promise < void > {
9
+ const appSchema = AppSchema . read ( )
10
+ const triggers = await triggerControllerFindAll ( appSchema . appid )
11
+ const table = new Table ( {
12
+ head : [ 'id' , 'name' , 'target' , 'cron' , 'createdAt' ] ,
13
+ } )
14
+
15
+ for ( let trigger of triggers ) {
16
+ table . push ( [
17
+ trigger . _id ,
18
+ trigger . desc ,
19
+ trigger . target ,
20
+ trigger . cron ,
21
+ formatDate ( trigger . createdAt ) ,
22
+ ] )
23
+ }
24
+ console . log ( table . toString ( ) )
25
+ }
26
+
27
+ export async function create ( name : string , target : string , cron : string ) : Promise < void > {
28
+ const appSchema = AppSchema . read ( )
29
+
30
+ const createDto : CreateTriggerDto = {
31
+ desc : name ,
32
+ target,
33
+ cron,
34
+ }
35
+ await triggerControllerCreate ( appSchema . appid , createDto )
36
+ console . log ( `${ getEmoji ( '✅' ) } trigger "${ name } " created` )
37
+ }
38
+
39
+
40
+ export async function del ( options : {
41
+ id ?: string
42
+ name ?: string
43
+ } ) : Promise < void > {
44
+ const appSchema = AppSchema . read ( )
45
+ if ( options . id ) {
46
+ await triggerControllerRemove ( options . id , appSchema . appid )
47
+ console . log ( `${ getEmoji ( '✅' ) } trigger "${ options . id } " deleted` )
48
+ } else if ( options . name ) {
49
+ const triggers = await triggerControllerFindAll ( appSchema . appid )
50
+ const trigger = triggers . find ( ( t ) => t . desc === options . name )
51
+ if ( trigger ) {
52
+ await triggerControllerRemove ( trigger . _id , appSchema . appid )
53
+ console . log ( `${ getEmoji ( '✅' ) } trigger "${ options . name } " deleted` )
54
+ } else {
55
+ console . log ( `${ getEmoji ( '❌' ) } trigger "${ options . name } " not found` )
56
+ }
57
+ }
58
+ }
0 commit comments