55 * The analysis runs every time a device uplink matches an alert and must send an email, sms or notification.
66 */
77import { 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" ;
99
1010import { checkAndChargeUsage } from "../services/plan/check-and-charge-usage" ;
1111
@@ -16,6 +16,17 @@ interface IMessageDetail {
1616 value : string ;
1717 variable : string ;
1818}
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+
1930/**
2031 * Notification messages to be sent
2132 * @param type Type of message to be sent
@@ -31,10 +42,12 @@ async function notificationMessages(type: string[], context: TagoContext, org_id
3142
3243 if ( has_service_limit ) {
3344 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" ) ) ;
3851 }
3952 } else {
4053 await Resources . devices . sendDeviceData ( org_id , {
@@ -62,16 +75,18 @@ async function emailMessages(type: string[], context: TagoContext, org_id: strin
6275 if ( has_service_limit ) {
6376 const email = new Services ( { token : context . token } ) . email ;
6477
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+ } ,
7287 } ,
73- } ,
74- } ) ;
88+ } )
89+ . then ( ( msg ) => console . debug ( msg ) ) ;
7590 } else {
7691 await Resources . devices . sendDeviceData ( org_id , {
7792 variable : "plan_status" ,
@@ -215,9 +230,12 @@ async function analysisAlert(context: TagoContext, scope: Data[]): Promise<void>
215230 }
216231 const [ message_var ] = await Resources . devices . getDeviceData ( org_id , { variables : [ "action_list_message" , "action_group_message" ] , groups : alert_id , qty : 1 } ) ;
217232
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 ) {
221239 throw "trigger_variable.value not found" ;
222240 }
223241
0 commit comments