1 | |
2 | |
3 | * Worker Pay Rates |
4 | * Information about a workers compensation rates. A worker can have up to 25 different rates.It’s not required for a worker to have a rate in the system. |
5 | */ |
6 | export async function main(auth: RT.Paychex, workerId: string, asof?: string | undefined) { |
7 | const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token') |
8 | const url = new URL(`https://api.paychex.com/workers/${workerId}/compensation/payrates`) |
9 | for (const [k, v] of [['asof', asof]]) { |
10 | if (v !== undefined && v !== '') { |
11 | url.searchParams.append(k, v) |
12 | } |
13 | } |
14 | const response = await fetch(url, { |
15 | method: 'GET', |
16 | headers: { |
17 | Authorization: 'Bearer ' + accessToken |
18 | }, |
19 | body: undefined |
20 | }) |
21 | if (!response.ok) { |
22 | const text = await response.text() |
23 | throw new Error(`${response.status} ${text}`) |
24 | } |
25 | return await response.json() |
26 | } |
27 |
|
28 | async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> { |
29 | const params = new URLSearchParams({ |
30 | grant_type: 'client_credentials' |
31 | }) |
32 |
|
33 | const response = await fetch(tokenUrl, { |
34 | method: 'POST', |
35 | headers: { |
36 | Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`), |
37 | 'Content-Type': 'application/x-www-form-urlencoded' |
38 | }, |
39 | body: params.toString() |
40 | }) |
41 |
|
42 | if (!response.ok) { |
43 | const text = await response.text() |
44 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
45 | } |
46 |
|
47 | const data = await response.json() |
48 | return data.access_token |
49 | } |
50 |
|