5
5
* The analysis runs every time a device uplink matches an alert and must send an email, sms or notification.
6
6
*/
7
7
import { Analysis , Resources , Services , Utils } from "@tago-io/sdk" ;
8
- import { Data , DeviceInfo , TagoContext , UserInfo } from "@tago-io/sdk/lib/types" ;
8
+ import { Conditionals , Data , DeviceInfo , TagoContext , UserInfo } from "@tago-io/sdk/lib/types" ;
9
9
10
10
import { checkAndChargeUsage } from "../services/plan/check-and-charge-usage" ;
11
11
@@ -16,6 +16,17 @@ interface IMessageDetail {
16
16
value : string ;
17
17
variable : string ;
18
18
}
19
+
20
+ type triggerType = {
21
+ device : string ;
22
+ variable : string ;
23
+ is : Conditionals ;
24
+ value : string ;
25
+ second_value ?: string ;
26
+ value_type : "string" | "number" | "boolean" | "*" ;
27
+ unlock ?: boolean ;
28
+ } ;
29
+
19
30
/**
20
31
* Notification messages to be sent
21
32
* @param type Type of message to be sent
@@ -31,10 +42,12 @@ async function notificationMessages(type: string[], context: TagoContext, org_id
31
42
32
43
if ( has_service_limit ) {
33
44
for ( const user of users_info ) {
34
- void Resources . run . notificationCreate ( user . id , {
35
- message,
36
- title : "Alert Trigger" ,
37
- } ) ;
45
+ void Resources . run
46
+ . notificationCreate ( user . id , {
47
+ message,
48
+ title : "Alert Trigger" ,
49
+ } )
50
+ . then ( ( ) => console . debug ( "Notification sent" ) ) ;
38
51
}
39
52
} else {
40
53
await Resources . devices . sendDeviceData ( org_id , {
@@ -62,16 +75,18 @@ async function emailMessages(type: string[], context: TagoContext, org_id: strin
62
75
if ( has_service_limit ) {
63
76
const email = new Services ( { token : context . token } ) . email ;
64
77
65
- void email . send ( {
66
- to : users_info . map ( ( x ) => x . email ) . join ( "," ) ,
67
- template : {
68
- name : "email_alert" ,
69
- params : {
70
- device_name : device_info . name ,
71
- alert_message : message ,
78
+ void email
79
+ . send ( {
80
+ to : users_info . map ( ( x ) => x . email ) . join ( "," ) ,
81
+ template : {
82
+ name : "email_alert" ,
83
+ params : {
84
+ device_name : device_info . name ,
85
+ alert_message : message ,
86
+ } ,
72
87
} ,
73
- } ,
74
- } ) ;
88
+ } )
89
+ . then ( ( msg ) => console . debug ( msg ) ) ;
75
90
} else {
76
91
await Resources . devices . sendDeviceData ( org_id , {
77
92
variable : "plan_status" ,
@@ -215,9 +230,12 @@ async function analysisAlert(context: TagoContext, scope: Data[]): Promise<void>
215
230
}
216
231
const [ message_var ] = await Resources . devices . getDeviceData ( org_id , { variables : [ "action_list_message" , "action_group_message" ] , groups : alert_id , qty : 1 } ) ;
217
232
218
- // @ts -ignore
219
- const trigger_variable = scope . find ( ( x ) => x . variable === ( action_info ?. trigger [ 0 ] as any ) ?. variable ) ?? null ;
220
- if ( ! trigger_variable ?. value ) {
233
+ // Get the triggered variable
234
+ const trigger = action_info . trigger as unknown as triggerType [ ] ;
235
+ const trigger_variables = trigger ?. filter ( ( x ) => ! x . unlock ) . map ( ( x ) => x . variable ) ;
236
+ const trigger_variable = scope . find ( ( x ) => trigger_variables . includes ( x . variable ) ) ;
237
+
238
+ if ( ! trigger_variable ) {
221
239
throw "trigger_variable.value not found" ;
222
240
}
223
241
0 commit comments