1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get Employees [Batch] |
8 | * **Summary Description** |
9 |
|
10 | The GET Employee List by Company ID API endpoint provides a list of up to 20 employees per call from the Paylocity system. |
11 | */ |
12 | export async function main( |
13 | auth: Paylocity, |
14 | companyId: string, |
15 | limit: string | undefined, |
16 | nextToken: string | undefined, |
17 | include: string | undefined, |
18 | activeOnly: string | undefined, |
19 | testMode?: string |
20 | ) { |
21 | const url = new URL( |
22 | `https://dc1prodgwext.paylocity.com/coreHr/v1/companies/${companyId}/employees` |
23 | ) |
24 | for (const [k, v] of [ |
25 | ['limit', limit], |
26 | ['nextToken', nextToken], |
27 | ['include', include], |
28 | ['activeOnly', activeOnly] |
29 | ]) { |
30 | if (v !== undefined && v !== '' && k !== undefined) { |
31 | url.searchParams.append(k, v) |
32 | } |
33 | } |
34 | const response = await fetch(url, { |
35 | method: 'GET', |
36 | headers: { |
37 | ...(testMode ? { testMode: testMode } : {}), |
38 | Authorization: |
39 | 'Bearer ' + |
40 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
41 | }, |
42 | body: undefined |
43 | }) |
44 | if (!response.ok) { |
45 | const text = await response.text() |
46 | throw new Error(`${response.status} ${text}`) |
47 | } |
48 | return await response.json() |
49 | } |
50 |
|
51 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
52 | const params = new URLSearchParams({ |
53 | grant_type: 'client_credentials', |
54 | client_id: auth.clientId, |
55 | client_secret: auth.clientSecret |
56 | }) |
57 |
|
58 | const response = await fetch(tokenUrl, { |
59 | method: 'POST', |
60 | headers: { |
61 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
62 | 'Content-Type': 'application/x-www-form-urlencoded' |
63 | }, |
64 | body: params.toString() |
65 | }) |
66 |
|
67 | if (!response.ok) { |
68 | const text = await response.text() |
69 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
70 | } |
71 |
|
72 | const data = await response.json() |
73 | return data.access_token |
74 | } |
75 |
|