Skip to content

Commit

Permalink
Merge pull request #23 from tago-io/feat/PE-51
Browse files Browse the repository at this point in the history
fixing alert handler PE-51
  • Loading branch information
vitorfdl authored Aug 2, 2023
2 parents f9edef5 + 593062a commit 65c5943
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
50 changes: 27 additions & 23 deletions src/services/alerts/edit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Account, Device } from "@tago-io/sdk";
import { Account, Device, Utils } from "@tago-io/sdk";
import { Data } from "@tago-io/sdk/out/common/common.types";
import { ActionQuery } from "@tago-io/sdk/out/modules/Account/actions.types";
import sendNotificationError from "../../lib/notificationError";
Expand Down Expand Up @@ -27,7 +27,7 @@ async function addDeviceToAlert(account: Account, org_dev: Device, action_id: st
return;
}
const action_info = await account.actions.info(action_id);
if(!action_info.tags) {
if (!action_info.tags) {
throw "Action not found";
}
const device_list = [...new Set(action_info.tags.filter((tag) => tag.key === "device_id").map((tag) => tag.value))];
Expand All @@ -51,7 +51,7 @@ async function listDeviceAction(account: Account, { device_id, action_id, group_
const filter: ActionQuery["filter"] = {
tags: [],
};
if(!filter.tags) {
if (!filter.tags) {
filter.tags = [];
}
if (device_id) {
Expand All @@ -78,13 +78,18 @@ async function undoChanges(device: Device, scope: Data[]) {
* @param scope Number of devices that will be listed
* @param config_dev Device of the organization
*/
async function editAlert({ account, environment, scope, config_dev: org_dev }: RouterConstructorData) {
async function editAlert({ account, environment, scope }: RouterConstructorData) {
if (!scope || !account) {
throw "Organization device not found";
}

if(!org_dev || !scope || !account) {
const organization_id = scope[0].device;
if (!organization_id) {
throw "Organization device not found";
}
const org_dev = await Utils.getDevice(account, organization_id);
const { group: action_id } = scope[0];
if(!action_id) {
if (!action_id) {
throw "Action not found";
}

Expand All @@ -97,40 +102,38 @@ async function editAlert({ account, environment, scope, config_dev: org_dev }: R
const action_condition = scope.find((x) => ["action_list_condition", "action_group_condition"].includes(x.variable));
const action_value = scope.find((x) => ["action_list_value", "action_group_value"].includes(x.variable));

if(!action_variable || !action_value) {
throw "Action variable and value not found";
}

const action_type = scope.find((x) => ["action_list_type", "action_group_type"].includes(x.variable));
const action_sendto = scope.find((x) => ["action_list_sendto", "action_group_sendto"].includes(x.variable));

if (!action_variable) {
[action_variable] = await org_dev.getData({ variables: ["action_list_variable", "action_group_variable"], qty: 1, groups: action_id });
}



if (!action_variable) {
console.debug("[Error] Update action: action_variable not found");
undoChanges(org_dev, scope);
return sendNotificationError(account, environment, "An error ocurred, please try again", "Error when editing alert");
}

// if (action_variable.value === "geofence" && (action_value || action_condition)) {
// console.debug("[Error] Updating geofence value or condition is not allowed");
// undoChanges(org_dev, scope);
// return sendNotificationError(account, environment, "Erro ao editar alerta", "Não é possível editar valor e condição de alertas de geofence. Delete e crie um novo alerta.");
// }

let device_list: string[] = [];
if (action_devices) {
device_list = (action_devices.value as string).split(";");
} else if (action_group) {
device_list = await getGroupDevices(account, action_group.value as string);
} else {
const action_info = await account.actions.info(action_id);
if(!action_info.tags) {
if (!action_info.tags) {
throw "Action tags not found";
}
device_list = action_info.tags.filter((tag) => tag.key === "device_id").map((tag) => tag.value);
const group_id = action_info.tags.find((tag) => tag.key === "group_id")?.value;
if (group_id) {
device_list = await getGroupDevices(account, group_id);
}
else {
device_list = action_info.tags.filter((tag) => tag.key === "device_id").map((tag) => tag.value);
}
}

const structure: ActionStructureParams = action_variable.metadata as any;
Expand All @@ -156,11 +159,12 @@ async function editAlert({ account, environment, scope, config_dev: org_dev }: R
if (action_sendto) {
structure.send_to = action_sendto.value as string;
}

if (structure.condition === "><" && (action_value.value as string)?.split(";").length !== 2) {
undoChanges(org_dev, scope);
sendNotificationError(account, environment, "Invalid between condition, you must enter the value such as: 2;15");
throw `[Error] Invalid between value: ${action_value.value}`;
if (action_value) {
if (structure.condition === "><" && (action_value.value as string)?.split(";").length !== 2) {
undoChanges(org_dev, scope);
sendNotificationError(account, environment, "Invalid between condition, you must enter the value such as: 2;15");
throw `[Error] Invalid between value: ${action_value.value}`;
}
}

const action_structure = generateActionStructure(structure, device_list);
Expand Down
22 changes: 7 additions & 15 deletions src/services/alerts/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ function generateActionStructure(structure: ActionStructureParams, device_ids: s
},
};

action_structure.tags = action_structure.tags.concat(
device_ids.map((id) => {
return { key: "device_id", value: id };
})
);

const value_type = Number.isNaN(Number(structure.trigger_value)) ? "string" : "number";
const variables = (structure.variable as string).split(",");
for (const device_id of device_ids) {
Expand Down Expand Up @@ -170,13 +164,7 @@ async function createAlert({ account, environment, scope, config_dev: org_dev, c
// Get the fields from the Input widget.
const action_group = scope.find((x) => x.variable === "action_group_list");
const action_dev_list = scope.find((x) => x.variable === "action_device_list" && x.metadata?.sentValues);

const action_set_unlock = scope.find((x) => x.variable === "action_set_unlock");
const action_sendto = scope.find((x) => x.variable === "action_sendto");
console.log("action_sendto", action_sendto);
console.log("action_set_unlock", action_set_unlock);
console.log("action_dev_list", action_dev_list);
console.log("action_group", action_group);

const action_variable = scope.find((x) => x.variable === "action_variable");
let action_condition: Data | DataToSend | undefined = scope.find((x) => x.variable === "action_condition");
Expand All @@ -198,8 +186,8 @@ async function createAlert({ account, environment, scope, config_dev: org_dev, c
// const action_unlock_value = scope.find((x) => x.variable === "action_unlock_value");

const action_value_unit = scope.find((x) => x.variable === "action_value_unit");
if (!action_value_unit?.value) {
throw "Missing action_value_unit";
if(!action_value?.value){
throw "Missing action_value";
}

if (action_value_unit?.value === "F") {
Expand Down Expand Up @@ -230,7 +218,7 @@ async function createAlert({ account, environment, scope, config_dev: org_dev, c
const organization_id = scope[0].device;
let device_list: string[] = [];

if (action_dev_list) {
if (action_dev_list?.metadata?.sentValues) {
device_list = action_dev_list?.metadata?.sentValues.map((x) => x.value as string);
} else if (action_group) {
const group_id = action_group.value as string;
Expand All @@ -243,6 +231,10 @@ async function createAlert({ account, environment, scope, config_dev: org_dev, c

const script_id = await findAnalysisByExportID(account, "alertTrigger");

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

// Create the action structure.
const structure: ActionStructureParams = {
org_id: organization_id,
Expand Down
1 change: 1 addition & 0 deletions tagoconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 65c5943

Please sign in to comment.