Skip to content

Instantly share code, notes, and snippets.

@hongymagic
Last active July 11, 2016 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongymagic/d0491e5fc940b8710e48f68906721325 to your computer and use it in GitHub Desktop.
Save hongymagic/d0491e5fc940b8710e48f68906721325 to your computer and use it in GitHub Desktop.
Add timesheet entries into Employement Hero from console
const add_timesheet_entry = (date) => {
const form = {
"utf8": "✓",
"timesheet_entry[created_by]": <your employment hero user id>,
"timesheet_entry[date]": `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`,
"timesheet_entry[start_hm]": "9:00 AM",
"timesheet_entry[end_hm]": "5:00 PM",
"timesheet_entry[units]": "",
"timesheet_entry[member_id]": "<your employment hero user id>",
"timesheet_entry[work_type_id]": "",
"timesheet_entry[location_id]": "<your employment hero location id>",
"timesheet_entry[comment]": "Added via EmploymentHero CLI",
"commit": "Save",
"management": "",
"from": ""
};
const data = new FormData();
for (let key in form) {
data.append(key, form[key]);
}
const request = new Request('/timesheet_entries', {
method: 'POST',
headers: new Headers({
'X-CSRF-Token': document.querySelector('[name=csrf-token]').content,
}),
credentials: 'same-origin',
body: data
});
fetch(request)
.then(response => response.text())
.then((body) => console.log(body))
.catch((error) => console.error(error));
};
// Change these values accordingly. Dates are inclusive, and month is zero-based
// index.
const start_date = new Date(2016, 5, 15); // 15 JUNE 2016
const end_date = new Date(2016, 6, 14); // 14 JULY 2016
// 0: Sunday, 1: Monday, ..., 6: Saturday. So work day is 1-5.
const is_workday = (date) => date.getDay() > 0 && date.getDay() < 6;
// Given two date ranges, walk through the each of the dates and run callback
// function when a date is a work day. The callback function is given the
// current date instance.
const walk = (start, end, cb) => {
if (start > end) {
return;
}
if (typeof cb === 'function' && is_workday(start)) {
cb(start);
}
walk(new Date(start_date.setDate(start.getDate() + 1)), end, cb);
};
walk(start_date, end_date, (date) => add_timesheet_entry(date));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment