//native
/**
* Company Workers
* Array of workers (employee and contractor) for all of the companies who are associated with a specific company that your application has been granted access to. The combination of query parameters to be used with this endpoint are as follows: 1. givenname, familyname, legallastfour 2. from, to (start date, end date) 3. employeeid 4. locationid 5. offset, limit (paging). Note: Paging and filtering attributes cannot be applied together.
*/
export async function main(
auth: RT.Paychex,
companyId: string,
givenname?: string | undefined,
familyname?: string | undefined,
legallastfour?: string | undefined,
employeeid?: string | undefined,
from?: string | undefined,
to?: string | undefined,
locationid?: string | undefined
) {
const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
const url = new URL(`https://api.paychex.com/companies/${companyId}/workers`)
for (const [k, v] of [
['givenname', givenname],
['familyname', familyname],
['legallastfour', legallastfour],
['employeeid', employeeid],
['from', from],
['to', to],
['locationid', locationid]
]) {
if (v !== undefined && v !== '') {
url.searchParams.append(k, v)
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: 'Bearer ' + accessToken
},
body: undefined
})
if (!response.ok) {
const text = await response.text()
throw new Error(`${response.status} ${text}`)
}
return await response.json()
}
async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
const params = new URLSearchParams({
grant_type: 'client_credentials'
})
const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params.toString()
})
if (!response.ok) {
const text = await response.text()
throw new Error(`OAuth token request failed: ${response.status} ${text}`)
}
const data = await response.json()
return data.access_token
}
Submitted by hugo697 235 days ago