1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get employee pay statement summary data for the specified year and check date. |
8 | * Get pay statement summary API will return employee pay statement summary data currently available in Paylocity Payroll/HR solution for the specified year and check date. |
9 | */ |
10 | export async function main( |
11 | auth: Paylocity, |
12 | companyId: string, |
13 | employeeId: string, |
14 | year: string, |
15 | checkDate: string, |
16 | pagesize: string | undefined, |
17 | pagenumber: string | undefined, |
18 | includetotalcount: string | undefined, |
19 | codegroup: string | undefined |
20 | ) { |
21 | const url = new URL( |
22 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employees/${employeeId}/paystatement/summary/${year}/${checkDate}` |
23 | ) |
24 | for (const [k, v] of [ |
25 | ['pagesize', pagesize], |
26 | ['pagenumber', pagenumber], |
27 | ['includetotalcount', includetotalcount], |
28 | ['codegroup', codegroup] |
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 | 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 |
|