-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
99 lines (87 loc) · 2.8 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const NodeHelper = require("node_helper");
const axios = require("axios");
let endpoint = "";
let apiToken = "";
const getRecords = async (startTime, endTime) => {
try {
const response = await axios({
method: "get",
url: `${endpoint}/records?timerange=${startTime}-${endTime}`,
headers: { authtoken: apiToken },
});
return response?.data;
} catch (error) {
console.error("Error querying records!");
return null;
}
};
const calculateHoursWorked = (records) => {
const sumValues = records?.reduce(
(prevValue, currentValue) =>
prevValue + Math.abs(currentValue?.t1 - currentValue?.t2),
0
);
const sumDiff = Math.floor(sumValues / 3600);
const minDiff = Math.round((sumValues - sumDiff * 3600) / 60);
return { sumDiff, minDiff };
};
function getDayRecords(records) {
const now = new Date();
const today = new Date();
today.setFullYear(now.getFullYear());
today.setDate(now.getDate());
today.setMonth(now.getMonth());
today.setHours(0);
today.setMinutes(0);
return records.filter((record) => record?.t2 > today.getTime() / 1000 ? record : null)
}
function getMondayOfCurrentWeek() {
const today = new Date();
const first = today.getDate() - today.getDay() + 1;
const monday = new Date(today.setDate(first));
return monday;
}
module.exports = NodeHelper.create({
start: function () {
console.info("MMM-Timetagger started!");
},
socketNotificationReceived: async function (notification, payload) {
const self = this;
switch (notification) {
case "GET_TIMETAGGER_DATA":
endpoint = payload?.endpoint;
apiToken = payload?.apiToken;
const monday = getMondayOfCurrentWeek();
monday.setHours(0);
monday.setMinutes(0);
const now = new Date();
const response = await getRecords(
Math.trunc(monday.getTime() / 1000),
Math.trunc(now.getTime() / 1000)
);
if (response) {
const weekResult = calculateHoursWorked(response?.records);
const dayRecords = getDayRecords(response?.records);
const dayResult = calculateHoursWorked(dayRecords);
let workInProgress = false;
if (dayRecords?.length > 0) {
console.log("dayRecords in here!")
const lastRecord = dayRecords[dayRecords.length-1];
if (lastRecord.t1 === lastRecord.t2) {
workInProgress = true;
}
}
console.log("after dayRecords!!")
self.sendSocketNotification("UPDATE_TIMETAGGER_DATA", {
...weekResult,
daySumDiff: dayResult?.sumDiff,
dayMinDiff: dayResult?.minDiff,
workInProgress
});
}
break;
default:
console.error("Switch item {} is missing", notification);
}
},
});