Last active
July 5, 2024 12:26
-
-
Save eusonlito/052ae182f04ee5b21e9beb5994149629 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function(){ | |
const endpointShifts = 'https://api.factorialhr.com/attendance/shifts'; | |
const endpointPeriods = 'https://api.factorialhr.com/attendance/periods'; | |
const url = window.location.href.split('/'); | |
const month = url.pop(); | |
const year = url.pop(); | |
const times = [[ '08:00', '14:00' ], [ '15:30', '17:30' ]]; | |
const allowFuture = true; | |
const today = new Date().toISOString().slice(0, 10); | |
const getPeriodId = async (year, month) => { | |
const response = await fetch(endpointPeriods + '?year=' + year + '&month=' + month, { | |
method: 'GET', | |
credentials: 'include', | |
headers: { | |
'accept': 'application/json, text/plain, */*', | |
'content-type': 'application/json;charset=UTF-8' | |
} | |
}); | |
return (await response.json())[0].id; | |
} | |
const innerText = string => { | |
return string.replace(/(\r\n|\n|\r)/gm, ' ').replace(/\s+/m, ' '); | |
} | |
const periodId = await getPeriodId(year, month); | |
document.querySelectorAll('table > tbody > tr').forEach(tr => { | |
const tdDay = tr.getElementsByTagName('td')[0]; | |
const tdInput = tr.getElementsByTagName('td')[1]; | |
if (!tdDay) { | |
return console.log('[] No day available'); | |
} | |
const monthDay = innerText(tdDay.innerText); | |
const day = parseInt(monthDay); | |
if ((day === 0) || (day === NaN)) { | |
return console.log('[] Invalid Day ' + monthDay); | |
} | |
const date = year + '-' + month + '-' + ('0' + day).slice(-2); | |
if ((date > today) && !allowFuture) { | |
return console.log('[' + date + '] Future Date'); | |
} | |
if (!tdInput) { | |
return console.log('[' + date + '] No Work Time Available'); | |
} | |
const workTime = innerText(tdInput.innerText); | |
if (workTime === '0h 00m / 0h 00m') { | |
return console.log('[' + monthDay + '] Date Without Work Time'); | |
} | |
if (parseInt(workTime)) { | |
return console.log('[' + monthDay + '] Date Already Filled ' + workTime); | |
} | |
times.forEach(time => { | |
fetch(endpointShifts, { | |
method: 'POST', | |
credentials: 'include', | |
headers: { | |
'accept': 'application/json, text/plain, */*', | |
'content-type': 'application/json;charset=UTF-8' | |
}, | |
body: JSON.stringify({ | |
period_id: periodId, | |
clock_in: time[0], | |
clock_out: time[1], | |
minutes: 0, | |
day: day, | |
observations: null, | |
history: [], | |
date: date, | |
reference_date: date, | |
half_day: null, | |
workable: true | |
}) | |
}) | |
.then(() => { | |
console.log('[' + date + ' / ' + dateRequest + '] Processed ' + time[0] + ' - ' + time[1]); | |
}) | |
.catch(error => { | |
console.error('[' + date + ' / ' + dateRequest + '] Error: ' + error.message); | |
}); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment