Created
March 20, 2025 16:28
-
-
Save KidkArolis/5c3cacdb7380c92d75051fd341e6957f to your computer and use it in GitHub Desktop.
Fetching custom values
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 API_KEY = process.env.API_KEY; | |
const fetchAllPages = async (url, params = {}) => { | |
let allPages = []; | |
let $skip = 0; | |
let $limit = 250; | |
while (true) { | |
const queryParams = new URLSearchParams({ | |
...params, | |
$skip, | |
$limit, | |
}); | |
const fullUrl = `${url}?${queryParams.toString()}`; | |
const response = await fetch(fullUrl, { | |
headers: { | |
Authorization: `Bearer ${API_KEY}`, | |
}, | |
}); | |
const result = await response.json(); | |
const { data } = result; | |
allPages = allPages.concat(data); | |
$skip += data.length; | |
if (!data.length || data.length < $limit) { | |
break; | |
} | |
} | |
return allPages; | |
}; | |
async function main() { | |
const today = new Date().toISOString().substr(0, 10); | |
// fetch all active people (excluding new hires and offboarded people) | |
const people = await fetchAllPages("https://app.humaans.io/api/people"); | |
// 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 custom values | |
const allCustomFields = await fetchAllPages( | |
"https://app.humaans.io/api/custom-fields", | |
); | |
const customFieldNames = allCustomFields.reduce((acc, field) => { | |
acc[field.id] = field.name; | |
return acc; | |
}, {}); | |
// fetch all of the custom field values | |
const allCustomValues = await fetchAllPages( | |
"https://app.humaans.io/api/custom-values", | |
); | |
// combine all the data totgether | |
const peopleWithDetails = []; | |
for (const person of people) { | |
const jobRole = jobRoles.find((j) => j.personId === person.id); | |
const customFieldsObj = allCustomValues.reduce((acc, field) => { | |
if (field.personId === person.id) { | |
acc[customFieldNames[field.customFieldId]] = field.value; | |
} | |
return acc; | |
}, {}); | |
peopleWithDetails.push({ | |
person, | |
jobRole, | |
customFields: customFieldsObj, | |
}); | |
} | |
console.log("Fetched", peopleWithDetails.length, "people"); | |
console.log("E.g.:", peopleWithDetails[0]); | |
// This will log a person object with the job role and custom fields included, e.g. | |
// { | |
// person: { | |
// id: 'i4gNmUa5g1Svv7uQ7QFoooh2', | |
// firstName: 'Jane', | |
// lastName: 'Doe', | |
// ... | |
// }, | |
// jobRole: { | |
// id: 'w0Z7nMgVvtOEWEC6JBuukcKJ', | |
// jobTitle: 'Account Executive', | |
// department: 'Sales', | |
// ... | |
// }, | |
// customFields: { | |
// 'Product area': 'YouTube', | |
// 'InfoSec training completed': '2025-04-01 | |
// } | |
// } | |
} | |
main().then(() => console.log("All done!")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment