-
Notifications
You must be signed in to change notification settings - Fork 12
/
kenjo-attendance.user.js
310 lines (242 loc) · 8.42 KB
/
kenjo-attendance.user.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// ==UserScript==
// @name Kenjo Attendance Fill Month
// @namespace attendance.kenjo.sconde.net
// @version 1.1
// @description Fill Kenjo Attendance month with templates
// @author Sergio Conde
// @match https://app.kenjo.io/*
// @match https://app.orgos.io/*
// @grant GM.getValue
// @grant GM.setValue
// @homepageURL https://github.com/skgsergio/greasemonkey-scripts
// @supportURL https://github.com/skgsergio/greasemonkey-scripts/issues
// @updateURL https://raw.githubusercontent.com/skgsergio/greasemonkey-scripts/master/kenjo-attendance.user.js
// ==/UserScript==
'use strict';
/*
Don't touch this, won't persist across updates.
Load Kenjo for the first time with the script and then open this script Storage preferences and edit there.
*/
/* 0 = Sunday, 1 = Monday, ..., 6 = Saturday */
const DEFAULT_SCHEDULE = {
1: [{ start: '9:00', hours: '8:00', pause: '00:30' }],
2: [{ start: '9:00', hours: '8:00', pause: '00:30' }],
3: [{ start: '9:00', hours: '8:00', pause: '00:30' }],
4: [{ start: '9:00', hours: '8:00', pause: '00:30' }],
5: [{ start: '9:00', hours: '8:00', pause: '00:30' }]
};
const DEFAULT_ENTROPY_MINUTES = 15;
/**
* Here be dragons
**/
/* API Endpoints */
const API_URL = 'https://api.kenjo.io';
const AUTH_COOKIE_URL = `${API_URL}/auth/cookie`;
const ME_URL = `${API_URL}/user-account-db/user-accounts/me`;
const TIMEOFF_URL = `${API_URL}/user-time-off-request/find`;
const CALENDAR_URL = `${API_URL}/calendar-db/find`;
const TEMPLATES_URL = `${API_URL}/calendar-template-db/templates`;
const ATTENDANCE_URL = `${API_URL}/user-attendance-db`;
function USERWORK_URL(userId) {
return `${API_URL}/user-work-db/${userId}/calendar`;
}
/* Fetch function */
async function fetchUrl(auth, url, method = 'GET', body = null) {
const headers = { 'Content-Type': 'application/json' }
if (auth) {
headers.Authorization = auth;
}
try {
const response = await fetch(url, { method, credentials: 'include', headers, body })
if (!response.ok) {
throw Error(`HTTP Code: ${response.status}`);
}
return await response.json();
} catch (err) {
throw new Error(`Failed performing request, reload the site and try again.\n\n${method} ${url}\n${err}`);
}
}
/* AUTH */
async function getAuth() {
const data = await fetchUrl(null, AUTH_COOKIE_URL);
return `${data.token_type} ${data.access_token}`;
}
/* GET */
function getUser(auth) {
return fetchUrl(auth, ME_URL);
}
function getUserCalendar(auth, userId) {
return fetchUrl(auth, USERWORK_URL(userId));
}
function getCalendarTemplates(auth) {
return fetchUrl(auth, TEMPLATES_URL);
}
/* POST */
function getCalendar(auth, calendarId) {
return fetchUrl(
auth,
CALENDAR_URL,
'POST',
JSON.stringify({
_id: calendarId
})
);
}
function getUserTimeOff(auth, userId, fromDate, toDate) {
return fetchUrl(
auth,
TIMEOFF_URL,
'POST',
JSON.stringify({
_from: { $gte: fromDate },
_to: { $lte: toDate },
_userId: userId
})
);
}
function addEntry(auth, userId, date, startTime, endTime, breakTime) {
return fetchUrl(
auth,
ATTENDANCE_URL,
'POST',
JSON.stringify({
ownerId: userId,
date: date,
startTime: startTime,
endTime: endTime,
breakTime: breakTime,
_approved: false,
_changesTracking: [],
_deleted: false,
_userId: userId
})
);
}
/* HELPERS */
function startOfMonth(date) {
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0));
}
function endOfMonth(date) {
return new Date(Date.UTC(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59, 999));
}
function hhmmToMinutes(str) {
return str.split(':').reduce((acc, curr) => (acc*60) + +curr);
}
/* MAIN */
var SCHEDULE = {};
var ENTROPY_MINUTES = 0;
async function fillMonth(statusContainer) {
try {
const monthStart = startOfMonth(new Date());
const monthEnd = endOfMonth(new Date());
/* Get user info */
statusContainer.innerText = "Getting user info...";
const auth = await getAuth();
const user = await getUser(auth);
statusContainer.innerText = "Getting user time off...";
const timeOff = await getUserTimeOff(auth, user.ownerId, monthStart.toISOString(), monthEnd.toISOString());
/* Get calendar info */
statusContainer.innerText = "Getting user calendar...";
const userCalendar = await getUserCalendar(auth, user.ownerId);
const calendars = await getCalendar(auth, userCalendar.calendarId);
const templates = await getCalendarTemplates(auth);
const template = templates.filter(tpl => tpl.templateKey == calendars[0]._calendarTemplateKey)[0];
/* Parse non working days */
statusContainer.innerText = "Processing non working days...";
const nonWorkingDays = [];
timeOff.forEach((t) => {
nonWorkingDays.push({
reason: t._policyName,
start: new Date(Date.parse(t._from)),
end: new Date(Date.parse(t._to))
});
});
template.holidays.forEach((h) => {
const start = new Date(Date.parse(`${h.holidayDate}T00:00:00.000Z`));
const end = new Date(Date.parse(`${h.holidayDate}T23:59:59.999Z`));
if (start >= monthStart && start <= monthEnd) {
nonWorkingDays.push({
reason: h.holidayKey,
start: start,
end: end
});
}
});
calendars[0]._customHolidays.forEach((h) => {
const holidayDate = h.holidayDate.split("T")[0];
const start = new Date(Date.parse(`${holidayDate}T00:00:00.000Z`));
const end = new Date(Date.parse(`${holidayDate}T23:59:59.999Z`));
if (start >= monthStart && start <= monthEnd) {
nonWorkingDays.push({
reason: h.holidayName,
start: start,
end: end
});
}
});
/* Generate month sheet */
statusContainer.innerText = "Generating attendance sheet...";
const entries = [];
const skippedDays = [];
for (let day = monthStart; day <= monthEnd; day.setDate(day.getDate() + 1)) {
/* Check if the day has an schedule */
if (!(day.getDay() in SCHEDULE) || SCHEDULE[day.getDay()].length == 0) {
continue;
}
/* Check if the day should be skipped (holiday or time off) */
const skipReasons = nonWorkingDays.filter((nwd) => day >= nwd.start && day <= nwd.end);
if (skipReasons.length > 0) {
skippedDays.push({ day: new Date(day.getTime()), reasons: skipReasons.map(sr => sr.reason) });
continue;
}
/* Produce an entry for this day */
SCHEDULE[day.getDay()].forEach((sch) => {
const start = hhmmToMinutes(sch.start) + Math.ceil(Math.random() * ENTROPY_MINUTES);
const pause = hhmmToMinutes(sch.pause);
const end = start + pause + hhmmToMinutes(sch.hours);
entries.push({
date: day.toISOString(),
start: start,
end: end,
pause: pause
});
});
}
/* Store sheet */
for (const [idx, ts] of entries.entries()) {
statusContainer.innerText = `Saving day ${idx+1} of ${entries.length}...`;
console.log(await addEntry(auth, user.ownerId, ts.date, ts.start, ts.end, ts.pause));
}
/* Show info to the user */
statusContainer.innerText = "Done";
let skippedTxt = "";
skippedDays.forEach((s) => { skippedTxt += `\n${s.day.toISOString().split("T")[0]}: ${s.reasons.join(', ')}` });
alert(`Created ${entries.length} entries.\n\nSkipped days:${skippedTxt}`);
/* Reload page to reflect changes */
location.assign(`${location.origin}/cloud/people/${user.ownerId}/attendance`);
} catch(err) {
alert(`Kenjo Attendance Fill Month error:\n${err}`);
}
}
(async function() {
/* Make schedule and entropy configurable */
SCHEDULE = await GM.getValue('SCHEDULE');
if (!SCHEDULE) {
SCHEDULE = DEFAULT_SCHEDULE;
GM.setValue('SCHEDULE', SCHEDULE);
}
ENTROPY_MINUTES = await GM.getValue('ENTROPY_MINUTES');
if (!ENTROPY_MINUTES) {
ENTROPY_MINUTES = DEFAULT_ENTROPY_MINUTES;
GM.setValue('ENTROPY_MINUTES', ENTROPY_MINUTES);
}
/* Add button */
const extDiv = document.createElement('div');
extDiv.style.textAlign = "center";
const monthBtn = document.createElement('button');
monthBtn.type = 'button';
monthBtn.innerText = 'Attendance: Fill Month';
monthBtn.onclick = function() { this.disabled = "disabled"; fillMonth(this); }
extDiv.append(monthBtn);
document.body.insertBefore(extDiv, document.body.firstChild);
})();