Skip to content

Instantly share code, notes, and snippets.

@KidkArolis
Last active January 26, 2022 19:20
Show Gist options
  • Save KidkArolis/3913fcde3ca3bc5cb7a0af439ce58e62 to your computer and use it in GitHub Desktop.
Save KidkArolis/3913fcde3ca3bc5cb7a0af439ce58e62 to your computer and use it in GitHub Desktop.
Fetching Humaans data for Payroll
// Paste your API KEY here
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!'))
{
"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