From 7a2a322bb59ed1fea1c8bebe78ef8db1d68dc55e Mon Sep 17 00:00:00 2001 From: Frederico Minuzzi Date: Fri, 15 Mar 2024 13:34:49 -0300 Subject: [PATCH 1/2] Incorrect function was being called. --- src/services/alerts/register.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/services/alerts/register.ts b/src/services/alerts/register.ts index 8660402..a5fa3bd 100644 --- a/src/services/alerts/register.ts +++ b/src/services/alerts/register.ts @@ -3,7 +3,7 @@ import { Data, DataToSend, DeviceListItem } from "@tago-io/sdk/lib/types"; import { parseTagoObject } from "../../lib/data.logic"; import { fetchDeviceList } from "../../lib/fetch-device-list"; -import { getDashboardByTagID } from "../../lib/find-resource"; +import { getAnalysisByTagID } from "../../lib/find-resource"; import { RouterConstructorData } from "../../types"; import { checkInAlertSet } from "./check-in-alerts"; import { geofenceAlertCreate } from "./geofence-alert"; @@ -154,6 +154,7 @@ async function createAlert({ environment, scope }: RouterConstructorData) { if (!environment || !scope) { throw new Error("Missing parameters"); } + const resources = new Resources({ token: environment.ACCOUNT_TOKEN }); const organization_id = scope[0].device; await Resources.devices.sendDeviceData(organization_id, { variable: "action_validation", value: "#VAL.CREATING_ALERT#", metadata: { type: "warning" } }); @@ -221,7 +222,7 @@ async function createAlert({ environment, scope }: RouterConstructorData) { device_list = await getGroupDevices(group_id, groupKey); } - const script_id = await getDashboardByTagID("alertTrigger"); + const script_id = await getAnalysisByTagID(resources, "alertTrigger"); if (!action_sendto?.value) { throw "Missing action_sendto"; From 988561f11a38e47f8131203bee97388dde92c90c Mon Sep 17 00:00:00 2001 From: Frederico Minuzzi Date: Fri, 15 Mar 2024 13:35:22 -0300 Subject: [PATCH 2/2] fixed alert variable handling --- src/analysis/alert-trigger.ts | 52 +++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/src/analysis/alert-trigger.ts b/src/analysis/alert-trigger.ts index a0ade30..ce1a16c 100644 --- a/src/analysis/alert-trigger.ts +++ b/src/analysis/alert-trigger.ts @@ -5,7 +5,7 @@ * The analysis runs every time a device uplink matches an alert and must send an email, sms or notification. */ import { Analysis, Resources, Services, Utils } from "@tago-io/sdk"; -import { Data, DeviceInfo, TagoContext, UserInfo } from "@tago-io/sdk/lib/types"; +import { Conditionals, Data, DeviceInfo, TagoContext, UserInfo } from "@tago-io/sdk/lib/types"; import { checkAndChargeUsage } from "../services/plan/check-and-charge-usage"; @@ -16,6 +16,17 @@ interface IMessageDetail { value: string; variable: string; } + +type triggerType = { + device: string; + variable: string; + is: Conditionals; + value: string; + second_value?: string; + value_type: "string" | "number" | "boolean" | "*"; + unlock?: boolean; +}; + /** * Notification messages to be sent * @param type Type of message to be sent @@ -31,10 +42,12 @@ async function notificationMessages(type: string[], context: TagoContext, org_id if (has_service_limit) { for (const user of users_info) { - void Resources.run.notificationCreate(user.id, { - message, - title: "Alert Trigger", - }); + void Resources.run + .notificationCreate(user.id, { + message, + title: "Alert Trigger", + }) + .then(() => console.debug("Notification sent")); } } else { await Resources.devices.sendDeviceData(org_id, { @@ -62,16 +75,18 @@ async function emailMessages(type: string[], context: TagoContext, org_id: strin if (has_service_limit) { const email = new Services({ token: context.token }).email; - void email.send({ - to: users_info.map((x) => x.email).join(","), - template: { - name: "email_alert", - params: { - device_name: device_info.name, - alert_message: message, + void email + .send({ + to: users_info.map((x) => x.email).join(","), + template: { + name: "email_alert", + params: { + device_name: device_info.name, + alert_message: message, + }, }, - }, - }); + }) + .then((msg) => console.debug(msg)); } else { await Resources.devices.sendDeviceData(org_id, { variable: "plan_status", @@ -215,9 +230,12 @@ async function analysisAlert(context: TagoContext, scope: Data[]): Promise } const [message_var] = await Resources.devices.getDeviceData(org_id, { variables: ["action_list_message", "action_group_message"], groups: alert_id, qty: 1 }); - // @ts-ignore - const trigger_variable = scope.find((x) => x.variable === (action_info?.trigger[0] as any)?.variable) ?? null; - if (!trigger_variable?.value) { + // Get the triggered variable + const trigger = action_info.trigger as unknown as triggerType[]; + const trigger_variables = trigger?.filter((x) => !x.unlock).map((x) => x.variable); + const trigger_variable = scope.find((x) => trigger_variables.includes(x.variable)); + + if (!trigger_variable) { throw "trigger_variable.value not found"; }