Skip to content

Commit

Permalink
Merge pull request #28 from tago-io/PE-65-AlertFixes
Browse files Browse the repository at this point in the history
Pe-65-AlertFixes
  • Loading branch information
Freddyminu committed Mar 15, 2024
2 parents 95d08e3 + 988561f commit 498dc87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
52 changes: 35 additions & 17 deletions src/analysis/alert-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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
Expand All @@ -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, {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -215,9 +230,12 @@ async function analysisAlert(context: TagoContext, scope: Data[]): Promise<void>
}
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";
}

Expand Down
5 changes: 3 additions & 2 deletions src/services/alerts/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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" } });

Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 498dc87

Please sign in to comment.