Skip to content

Commit d346887

Browse files
Fixes related to biome, build to TS and unit tests.
1 parent e1a7fe6 commit d346887

17 files changed

+30
-41
lines changed

src/analysis/data-retention.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,12 @@ async function resolveDataRetentionByOrg(org_id: string) {
2323
});
2424

2525
for (const device_obj of device_list) {
26-
// @ts-ignore: Unreachable code error
27-
if (!device_obj.bucket) {
28-
return;
29-
}
30-
31-
const bucket_variables = await Resources.buckets.listVariables(device_obj.bucket.id);
32-
if (!bucket_variables[0]) {
26+
const variables = await Resources.devices.getDeviceData(device_obj.id, { qty: 9999 });
27+
if (!variables[0]) {
3328
return;
3429
}
3530

36-
const bucket_vars = bucket_variables.map((v) => v.variable);
31+
const bucket_vars = variables.map((v) => v.variable);
3732
const data_retention_ignore = bucket_vars
3833
.map((r) => {
3934
if (!r.includes("action")) {

src/analysis/send-report.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* - When setting a scheduled report, an action will trigger this script.
1010
*/
1111

12-
import dayjs from "dayjs";
12+
import { DateTime } from "luxon";
1313

1414
import { Analysis, Resources } from "@tago-io/sdk";
1515
import { ActionInfo, TagoContext, UserInfo } from "@tago-io/sdk/lib/types";
@@ -93,7 +93,7 @@ async function resolveReport(context: TagoContext, action_info: ActionInfo, org_
9393
status: status_history,
9494
battery: `${(battery?.value as string) ?? "N/A"}${battery?.unit ?? ""}`,
9595
rssi: (rssi?.value as string) ?? "N/A",
96-
date: dayjs(String(sensor_info.last_input)).format("YYYY-MM-DD HH:mm:ss"),
96+
date: DateTime.fromISO(String(sensor_info.last_input)).toFormat("yyyy-MM-dd HH:mm:ss"),
9797
});
9898
}
9999

src/analysis/status-updater.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
import async from "async";
14-
import dayjs from "dayjs";
14+
import { DateTime } from "luxon";
1515

1616
import { Analysis, Resources, Utils } from "@tago-io/sdk";
1717
import { DeviceInfo, DeviceListItem, TagoContext } from "@tago-io/sdk/lib/types";
@@ -38,9 +38,9 @@ async function resolveOrg(org: DeviceListItem) {
3838
});
3939

4040
for (const sensor of sensorList) {
41-
const last_input = dayjs(sensor.last_input);
42-
const now = dayjs();
43-
const diff_time = now.diff(last_input, "hours");
41+
const last_input = DateTime.fromISO(String(sensor.last_input || ""));
42+
const now = DateTime.now();
43+
const diff_time = now.diff(last_input, "hours").hours;
4444
total_qty++;
4545
if (diff_time < 24) {
4646
active_qty++;
@@ -136,15 +136,15 @@ async function resolveDevice(context: TagoContext, org_id: string, device_id: st
136136
return Promise.reject("Device not found");
137137
}
138138

139-
const checkin_date = dayjs(device_info.last_input);
139+
const checkin_date = DateTime.fromISO(device_info.last_input.toString());
140140

141-
if (!checkin_date) {
141+
if (!checkin_date.isValid) {
142142
return "no data";
143143
}
144144

145-
let diff_hours: string | number = dayjs().diff(checkin_date, "hours");
145+
let diff_hours: string | number = DateTime.now().diff(checkin_date, "hours").hours;
146146

147-
if (diff_hours !== diff_hours) {
147+
if (isNaN(diff_hours)) {
148148
diff_hours = "-";
149149
} //checking for NaN
150150

src/lib/device-name-exists.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { deviceNameExists } from "./device-name-exists";
22

3-
jest.mock("../lib/fetch-device-list", () => {
3+
vitest.mock("../lib/fetch-device-list", () => {
44
return {
5-
fetchDeviceList: jest
5+
fetchDeviceList: vitest
66
.fn()
77
.mockImplementationOnce(() => Promise.resolve([]))
88
.mockImplementationOnce(() => Promise.resolve([{ id: "orgOne" }]))

src/lib/install-template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function InstallTemplate(templates: string[], replaceObj: any) {
2222
}
2323

2424
for (const dashboard of dash_list) {
25-
let dash_info = (await Resources.dashboards.info(dashboard)) as Writeable<DashboardInfo> & { setup: {} };
25+
let dash_info = (await Resources.dashboards.info(dashboard)) as Writeable<DashboardInfo> & { setup: Record<string, unknown> };
2626
const hidden_var = dash_info.tags?.find((x) => x.key === "hidden");
2727
dash_info.visible = !hidden_var;
2828
dash_info = replaceJSON(dash_info, replaceObj);

src/lib/pdf-template.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import dayjs from "dayjs";
1+
import { DateTime } from "luxon";
22

33
const headerTemplate = `
44
<div class="header" style="background-color: rgba(47, 48, 101, 0.1); align-items: center; padding: 0px 30px; font-size: 30px; width: 100%; color:black !important; margin:0px 20px 80px 20px; border-radius: 8px 8px 0 0; -webkit-print-color-adjust: exact; height: 60rem;">
55
<div style="display: inline-block; margin: 0 2rem; vertical-align: top; text-align: start; width: 70%;">
66
<h1 style="font-weight: 400; color:black !important; font-size: 18px;">$ORG_NAME$ - Sensor Report</h1>
7-
<h1 style="font-weight: 400; font-size: 10px;">${dayjs().format("YYYY-MM-DD HH:mm:ss")}</h1>
7+
<h1 style="font-weight: 400; font-size: 10px;">${DateTime.now().toFormat("yyyy-MM-dd HH:mm:ss")}</h1>
88
</div>
99
<div style="display: inline-block; width: 25%; margin-top:15px;">
1010
<img

src/lib/send-pdf.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ async function createPDF(context: TagoContext, htmlBody: string, users_info_list
4141
const report = await pdf.generate({ base64, options }).catch((error) => console.debug(error));
4242

4343
// Send the email.
44-
// eslint-disable-next-line unicorn/no-array-for-each
45-
users_info_list.forEach(async (user_info) => {
44+
for (const user_info of users_info_list) {
4645
if (user_info?.email) {
4746
await email
4847
.send({
@@ -60,7 +59,7 @@ async function createPDF(context: TagoContext, htmlBody: string, users_info_list
6059
} else {
6160
console.debug("Error - couldnt find user");
6261
}
63-
});
62+
}
6463

6564
const filename = `/reports/${org_id}/sensor_report_${uuidv4()}.pdf`;
6665

src/lib/undo-device-changes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ async function undoDeviceChanges({ scope, deviceInfo }: { scope: DeviceListScope
1919
for (const key of Object.keys(deviceScope)) {
2020
if (key === "name") {
2121
const oldName = deviceScope?.old?.[key] as string;
22-
// eslint-disable-next-line no-undef
2322
await Resources.devices.edit(deviceInfo.id, { name: oldName });
2423
} else if (key.includes("param.")) {
2524
const paramKey = key.replace("param.", "");

src/lib/validation.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { DateTime } from "luxon";
22

33
import { Resources } from "@tago-io/sdk";
44

5-
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
65
type validation_type = "success" | "danger" | "warning" | string;
76
interface IValidateOptions {
87
show_markdown?: boolean;

src/services/alerts/check-in-alerts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dayjs from "dayjs";
1+
import { DateTime } from "luxon";
22

33
import { Resources } from "@tago-io/sdk";
44
import { TagoContext } from "@tago-io/sdk/lib/types";
@@ -18,7 +18,7 @@ interface ICheckInParam {
1818
*/
1919
async function checkInTrigger(context: TagoContext, org_id: string, params: ICheckInParam) {
2020
const { last_input, device_id } = params;
21-
const checkin_date = dayjs(last_input);
21+
const checkin_date = last_input ? DateTime.fromJSDate(last_input) : null;
2222
if (!checkin_date) {
2323
return "no data";
2424
}
@@ -28,7 +28,7 @@ async function checkInTrigger(context: TagoContext, org_id: string, params: IChe
2828
const actionList = paramList.filter((param) => param.key.startsWith("checkin"));
2929
for (const param of actionList) {
3030
const [interval] = param.value.split(",");
31-
const diff_hours: string | number = dayjs().diff(checkin_date, "hours");
31+
const diff_hours: number = DateTime.now().diff(checkin_date, "hours").hours;
3232

3333
if (diff_hours >= Number(interval) && !param.sent) {
3434
const action_id = param.key.replace("checkin", "");

0 commit comments

Comments
 (0)