-
-
Save KidkArolis/3913fcde3ca3bc5cb7a0af439ce58e62 to your computer and use it in GitHub Desktop.
Fetching Humaans data for Payroll
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
// Paste your API KEY here |
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
const fs = require('fs') | |
const axios = require('axios') | |
const API_KEY = fs.readFileSync('./API_KEY').toString() | |
const fetchAllPages = async (url, params = {}) => { | |
let allPages = [] | |
let $skip = 0 | |
while (true) { | |
const { data: result } = await axios.get(url, { | |
params: { | |
...params, | |
$skip: $skip | |
}, | |
headers: { | |
'Authorization': `Bearer ${API_KEY}` | |
} | |
}) | |
const { data, limit, total } = result | |
// merge the fetched data | |
allPages = allPages.concat(data) | |
// and in the next loop tick, skip the items | |
// that we've already fetched | |
$skip += data.length | |
// stop if we fetched an empty page, or if fetched all items | |
if (!data.length || allPages.length >= total) { | |
break | |
} | |
} | |
return allPages | |
} | |
async function main () { | |
const today = new Date().toISOString().substr(0, 10) | |
// fetch all active people (excluding new hires and offboarded people by default) | |
const people = await fetchAllPages('https://app.humaans.io/api/people') | |
// fetch the bank accounts | |
const bankAccounts = await fetchAllPages('https://app.humaans.io/api/bank-accounts', { $asOf: today }) | |
// fetch all the job roles as of today | |
const jobRoles = await fetchAllPages('https://app.humaans.io/api/job-roles', { $asOf: today }) | |
// fetch all of the compensation, specifically salaries, as of today | |
const compensations = await fetchAllPages('https://app.humaans.io/api/compensations', { $asOf: today, type: 'salary' }) | |
// combine all the data totgether | |
const peopleWithDetails = [] | |
for (const person of people) { | |
const bankAccount = bankAccounts.find(b => b.personId === person.id) | |
const currentJobRole = jobRoles.find(j => j.personId === person.id) | |
const currentSalary = compensations.find(c => c.personId === person.id) | |
peopleWithDetails.push({ | |
person, | |
bankAccount, | |
currentJobRole, | |
currentSalary | |
}) | |
} | |
console.log('Fetched', peopleWithDetails.length, 'people') | |
console.log('E.g.:', peopleWithDetails[0]) | |
} | |
main().then(() => console.log('All done!')) |
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
{ | |
"name": "payroll-data", | |
"version": "1.0.0", | |
"description": "Example of how to fetch data from Humaans API", | |
"main": "index.js", | |
"author": "Humaans", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^0.24.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment