-
Notifications
You must be signed in to change notification settings - Fork 12
/
bamboohr-timesheet-month.user.js
191 lines (156 loc) · 5.97 KB
/
bamboohr-timesheet-month.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
// ==UserScript==
// @name BambooHR Timesheet Fill Month
// @namespace month.timesheet.bamboohr.sconde.net
// @version 1.3
// @description Fill BambooHR Timesheet month with templates
// @author Sergio Conde
// @match https://*.bamboohr.com/employees/timesheet/*
// @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/bamboohr-timesheet-month.user.js
// ==/UserScript==
'use strict';
/*
Don't touch this, won't persist across updates.
Load BambooHR for the first time with the script and then open this script Storage preferences and edit there.
*/
const DEFAULT_TEMPLATES = {
'default': [{ start: '8:15', end: '13:00' }, { start: '13:30', end: '16:45' }],
'Fri': [{ start: '8:15', end: '14:30' }, { start: '15:30', end: '17:15' }]
};
const DEFAULT_ENTROPY_MINUTES = 10;
const CONTAINER_CLASSLIST = 'TimesheetSummary__clockButtonWrapper';
const BUTTON_CLASSLIST = 'fab-Button fab-Button--small fab-Button--width100';
/* Here be dragons */
(async function() {
let TEMPLATES = await GM.getValue('TEMPLATES');
if (!TEMPLATES) {
TEMPLATES = DEFAULT_TEMPLATES;
GM.setValue('TEMPLATES', TEMPLATES);
}
let ENTROPY_MINUTES = await GM.getValue('ENTROPY_MINUTES');
if (!ENTROPY_MINUTES) {
ENTROPY_MINUTES = DEFAULT_ENTROPY_MINUTES;
GM.setValue('ENTROPY_MINUTES', ENTROPY_MINUTES);
}
/* Fill Month */
let container_fill = document.createElement('div');
container_fill.classList.value = CONTAINER_CLASSLIST;
let btn_fill = document.createElement('button');
container_fill.append(btn_fill);
btn_fill.type = 'button';
btn_fill.classList.value = BUTTON_CLASSLIST;
btn_fill.innerText = 'Fill Month';
btn_fill.onclick = function () {
let tsd = JSON.parse(document.getElementById('js-timesheet-data').innerHTML);
let skipped = [];
let entries = [];
let tracking_id = 0;
for (const [day, details] of Object.entries(tsd.timesheet.dailyDetails)) {
let date = new Date(day);
/* Skip weekend */
if ([0, 6].includes(date.getDay())) {
continue;
}
/* Skip holidays & time off */
let skip_reasons = [];
skip_reasons.push(...details.holidays.map(h => `${h.name.trim()} (${h.paidHours} hours)`));
skip_reasons.push(...details.timeOff.map(t => `${t.type.trim()} (${t.amount} ${t.unit})`));
if (skip_reasons.length > 0) {
skipped.push(`${day}: ${skip_reasons.join(", ")}`);
continue;
}
/* Get the working time slots for the dow */
let dow = date.toLocaleDateString("en-US", { weekday: 'short' });
let slots = TEMPLATES['default'];
if (TEMPLATES.hasOwnProperty(dow)) {
slots = TEMPLATES[dow];
}
/* Generate the entries for this day */
let minute_diff = [...Array(slots.length)].map(_ => Math.ceil(Math.random() * ENTROPY_MINUTES));
for (const [idx, slot] of slots.entries()) {
tracking_id += 1;
let start = new Date(`${day} ${slot.start}`)
start.setMinutes(start.getMinutes() + minute_diff[idx])
let end = new Date(`${day} ${slot.end}`)
end.setMinutes(end.getMinutes() + minute_diff[minute_diff.length - 1 - idx])
entries.push({
id: null,
trackingId: tracking_id,
employeeId: unsafeWindow.currentlyEditingEmployeeId,
date: day,
start: `${start.getHours()}:${('0' + start.getMinutes()).slice(-2)}`,
end: `${end.getHours()}:${('0' + end.getMinutes()).slice(-2)}`,
note: ''
});
}
}
fetch(
`${window.location.origin}/timesheet/clock/entries`,
{
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'content-type': 'application/json; charset=UTF-8',
'x-csrf-token': unsafeWindow.CSRF_TOKEN
},
body: JSON.stringify({ entries: entries })
}
).then(data => {
if (data.status == 200) {
alert(`Created ${entries.length} entries.\n\nSkipped days:\n${skipped.join('\n')}`);
location.reload();
} else {
data.text().then(t => alert(`Request error!\nHTTP Code: ${data.status}\nResponse:\n${t}`));
}
}).catch(err => alert(`Fetch error!\n\n${err}`));
return false;
}
/* Delete Month */
let container_del = document.createElement('div');
container_del.classList.value = CONTAINER_CLASSLIST;
let btn_del = document.createElement('button');
container_del.append(btn_del);
btn_del.type = 'button';
btn_del.classList.value = BUTTON_CLASSLIST;
btn_del.innerText = 'Delete Month';
btn_del.onclick = function () {
let tsd = JSON.parse(document.getElementById('js-timesheet-data').innerHTML);
let entries = [];
/* Grab all entries ids */
for (const [day, details] of Object.entries(tsd.timesheet.dailyDetails)) {
for (const entry of details.clockEntries) {
entries.push(entry.id)
}
}
fetch(
`${window.location.origin}/timesheet/clock/entries`,
{
method: 'DELETE',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'content-type': 'application/json; charset=UTF-8',
'x-csrf-token': unsafeWindow.CSRF_TOKEN
},
body: JSON.stringify({ entries: entries })
}
).then(data => {
if (data.status == 200) {
alert(`Deleted ${entries.length} entries.`);
location.reload();
} else {
data.text().then(t => alert(`Request error!\nHTTP Code: ${data.status}\nResponse:\n${t}`));
}
}).catch(err => alert(`Fetch error!\n\n${err}`));
return false;
}
/* Add buttons */
document.querySelector('.TimesheetSummary').prepend(container_del);
document.querySelector('.TimesheetSummary').prepend(container_fill);
})();