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