1 | |
2 | |
3 | * Company Pay Components |
4 | * Array of pay components that are configured for a company. Pay components are earnings and deductions which are used for payroll. |
5 | */ |
6 | export async function main( |
7 | auth: RT.Paychex, |
8 | companyId: string, |
9 | effectonpay?: string | undefined, |
10 | asof?: string | undefined, |
11 | classificationtype?: string | undefined, |
12 | name?: string | undefined |
13 | ) { |
14 | const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token') |
15 | const url = new URL(`https://api.paychex.com/companies/${companyId}/paycomponents`) |
16 | for (const [k, v] of [ |
17 | ['effectonpay', effectonpay], |
18 | ['asof', asof], |
19 | ['classificationtype', classificationtype], |
20 | ['name', name] |
21 | ]) { |
22 | if (v !== undefined && v !== '') { |
23 | url.searchParams.append(k, v) |
24 | } |
25 | } |
26 | const response = await fetch(url, { |
27 | method: 'GET', |
28 | headers: { |
29 | Authorization: 'Bearer ' + accessToken |
30 | }, |
31 | body: undefined |
32 | }) |
33 | if (!response.ok) { |
34 | const text = await response.text() |
35 | throw new Error(`${response.status} ${text}`) |
36 | } |
37 | return await response.json() |
38 | } |
39 |
|
40 | async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> { |
41 | const params = new URLSearchParams({ |
42 | grant_type: 'client_credentials' |
43 | }) |
44 |
|
45 | const response = await fetch(tokenUrl, { |
46 | method: 'POST', |
47 | headers: { |
48 | Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`), |
49 | 'Content-Type': 'application/x-www-form-urlencoded' |
50 | }, |
51 | body: params.toString() |
52 | }) |
53 |
|
54 | if (!response.ok) { |
55 | const text = await response.text() |
56 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
57 | } |
58 |
|
59 | const data = await response.json() |
60 | return data.access_token |
61 | } |
62 |
|