Last active
January 3, 2022 13:42
-
-
Save saade/ac8354363c964259f8f8a27178318632 to your computer and use it in GitHub Desktop.
Puntu CSV Export
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
const controller = new AbortController() | |
const timeoutId = setTimeout(() => controller.abort(), 1000 * 60) | |
const date = new Date(); | |
const m = parseInt(prompt('Digite o mês que deseja exportar', date.getMonth() + 1)) - 1; | |
const y = prompt('Digite o ano que deseja exportar', date.getFullYear()); | |
if( window.location.host !== 'app.puntu.com.br' ) { | |
prompt('Para usar o script, vá para o Puntu Web.', 'https://app.puntu.com.br'); | |
throw new Error('Para usar o script, vá para o Puntu Web.'); | |
} | |
const loadingElement = document.createElement('div') | |
loadingElement.style = 'padding: 10px 5px; background: #1e3664; color: white;' | |
loadingElement.innerText = 'Exportando CSV...' | |
document.body.prepend(loadingElement) | |
fetch("https://api.puntu.com.br/timeSheet/generate/pointSheet/app/v3?type=RECORDS", { | |
headers: { | |
"accept": "application/json, text/plain, */*", | |
"authorization": "Bearer " + localStorage.token, | |
"content-type": "application/json;charset=UTF-8" | |
}, | |
body: JSON.stringify({ | |
"start": new Date(y, m, 1), | |
"end": new Date(y, m + 1, 0) | |
}), | |
method: "POST", | |
signal: controller.signal | |
}) | |
.then( response => { document.body.removeChild(loadingElement); return response } ) | |
.then(response => response.json()) | |
.then(({ records: { itens } }) => { | |
const _records = itens.reduce(function (r, a) { r[a.date] = r[a.date] || []; r[a.date].push(a); return r; }, {}) | |
return Object.keys(_records).map( date => [date, ..._records[date].map(reg => reg.hour)].join(';')).join('\n') | |
}) | |
.then(csv => prompt('CTRL+C para copiar:', csv)) | |
.catch(err => { | |
if( controller.signal.aborted ) { | |
alert("Timeout na requisição. Tente novamente") | |
console.error(err.message) | |
} else { | |
alert(err.message) | |
console.error(err) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment