Skip to content

Commit 498dc87

Browse files
authored
Merge pull request #28 from tago-io/PE-65-AlertFixes
Pe-65-AlertFixes
2 parents 95d08e3 + 988561f commit 498dc87

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

src/analysis/alert-trigger.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* The analysis runs every time a device uplink matches an alert and must send an email, sms or notification.
66
*/
77
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";
99

1010
import { 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

src/services/alerts/register.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Data, DataToSend, DeviceListItem } from "@tago-io/sdk/lib/types";
33

44
import { parseTagoObject } from "../../lib/data.logic";
55
import { fetchDeviceList } from "../../lib/fetch-device-list";
6-
import { getDashboardByTagID } from "../../lib/find-resource";
6+
import { getAnalysisByTagID } from "../../lib/find-resource";
77
import { RouterConstructorData } from "../../types";
88
import { checkInAlertSet } from "./check-in-alerts";
99
import { geofenceAlertCreate } from "./geofence-alert";
@@ -154,6 +154,7 @@ async function createAlert({ environment, scope }: RouterConstructorData) {
154154
if (!environment || !scope) {
155155
throw new Error("Missing parameters");
156156
}
157+
const resources = new Resources({ token: environment.ACCOUNT_TOKEN });
157158
const organization_id = scope[0].device;
158159
await Resources.devices.sendDeviceData(organization_id, { variable: "action_validation", value: "#VAL.CREATING_ALERT#", metadata: { type: "warning" } });
159160

@@ -221,7 +222,7 @@ async function createAlert({ environment, scope }: RouterConstructorData) {
221222
device_list = await getGroupDevices(group_id, groupKey);
222223
}
223224

224-
const script_id = await getDashboardByTagID("alertTrigger");
225+
const script_id = await getAnalysisByTagID(resources, "alertTrigger");
225226

226227
if (!action_sendto?.value) {
227228
throw "Missing action_sendto";

0 commit comments

Comments
 (0)