Last active
January 24, 2023 18:27
-
-
Save eusonlito/052ae182f04ee5b21e9beb5994149629 to your computer and use it in GitHub Desktop.
This file contains 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 year = new Date().getFullYear(); | |
const month = new Date().getMonth() + 1; | |
const times = [[ '08:00', '14:00' ], [ '15:30', '17:30' ]]; | |
const allowFuture = true; | |
const today = new Date().toISOString().slice(0, 10); | |
function datetime(date) { | |
return new Date(new Date(date).setDate(new Date(date).getDate() - 1)).toISOString().substring(0, 10) + 'T22:00.00.000Z'; | |
} | |
async function getPeriodId(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 periodId = await getPeriodId(year, month); | |
document.querySelectorAll('table > tbody > tr').forEach(tr => { | |
const tdDay = tr.getElementsByTagName('td')[0]; | |
const tdInput = tr.getElementsByTagName('td')[1]; | |
const monthDay = tdDay.querySelector(':scope > div:first-child > span'); | |
if (!monthDay) { | |
return console.log('[] No Month Day'); | |
} | |
const day = parseInt(monthDay.innerText); | |
if ((day === 0) || (day === NaN)) { | |
return console.log('[] Invalid Day ' + monthDay.innerText); | |
} | |
const date = year + '-' + month + '-' + ('0' + day).slice(-2); | |
if ((date > today) && !allowFuture) { | |
return console.log('[' + date + '] Future Date'); | |
} | |
const dateString = tdDay.querySelector(':scope > div:first-child').innerText; | |
if (dateString.match('domingo') || dateString.match('sábado')) { | |
return console.log('[' + date + '] Weekend'); | |
} | |
if (tr.className.match('disabled') || tr.className.match('j2x9cji')) { | |
return console.log('[' + date + '] Date Disabled'); | |
} | |
const note = tdDay.querySelector(':scope > div:first-child > div:nth-child(3)'); | |
if (note) { | |
return console.log('[' + date + '] Date With Note: ' + note.innerText); | |
} | |
const input = tdInput.querySelector('input[type="text"]'); | |
if (input && input.value) { | |
return console.log('[' + date + '] Date With Value ' + input.value); | |
} | |
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: datetime(date), | |
half_day: null | |
}) | |
}) | |
.then(() => { | |
console.log('[' + date + '] Processed ' + time[0] + ' - ' + time[1]); | |
}) | |
.catch(error => { | |
console.error('[' + date + '] Error: ' + error.message); | |
}); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment