1 | |
2 | |
3 | * Company Worker Statuses |
4 | * Array of statuses set at the company level which a worker could be assigned. |
5 | */ |
6 | export async function main(auth: RT.Paychex, companyId: string) { |
7 | const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token') |
8 | const url = new URL(`https://api.paychex.com/companies/${companyId}/workerstatuses`) |
9 |
|
10 | const response = await fetch(url, { |
11 | method: 'GET', |
12 | headers: { |
13 | Authorization: 'Bearer ' + accessToken |
14 | }, |
15 | body: undefined |
16 | }) |
17 | if (!response.ok) { |
18 | const text = await response.text() |
19 | throw new Error(`${response.status} ${text}`) |
20 | } |
21 | return await response.json() |
22 | } |
23 |
|
24 | async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> { |
25 | const params = new URLSearchParams({ |
26 | grant_type: 'client_credentials' |
27 | }) |
28 |
|
29 | const response = await fetch(tokenUrl, { |
30 | method: 'POST', |
31 | headers: { |
32 | Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`), |
33 | 'Content-Type': 'application/x-www-form-urlencoded' |
34 | }, |
35 | body: params.toString() |
36 | }) |
37 |
|
38 | if (!response.ok) { |
39 | const text = await response.text() |
40 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
41 | } |
42 |
|
43 | const data = await response.json() |
44 | return data.access_token |
45 | } |
46 |
|