1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * GET Recurring Deduction Details for a specific past/present/future deduction |
8 | * **Summary Description** |
9 |
|
10 | This function will allow the API user to get details for a particular deduction code that occurs in the past/present/future. |
11 | */ |
12 | export async function main( |
13 | auth: Paylocity, |
14 | companyId: string, |
15 | employeeId: string, |
16 | deductionCode: string, |
17 | resourceId: string, |
18 | testMode?: string |
19 | ) { |
20 | const url = new URL( |
21 | `https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/employees/${employeeId}/deductions/${deductionCode}/${resourceId}` |
22 | ) |
23 |
|
24 | const response = await fetch(url, { |
25 | method: 'GET', |
26 | headers: { |
27 | ...(testMode ? { testMode: testMode } : {}), |
28 | Authorization: |
29 | 'Bearer ' + |
30 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
31 | }, |
32 | body: undefined |
33 | }) |
34 | if (!response.ok) { |
35 | const text = await response.text() |
36 | throw new Error(`${response.status} ${text}`) |
37 | } |
38 | return await response.json() |
39 | } |
40 |
|
41 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
42 | const params = new URLSearchParams({ |
43 | grant_type: 'client_credentials', |
44 | client_id: auth.clientId, |
45 | client_secret: auth.clientSecret |
46 | }) |
47 |
|
48 | const response = await fetch(tokenUrl, { |
49 | method: 'POST', |
50 | headers: { |
51 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
52 | 'Content-Type': 'application/x-www-form-urlencoded' |
53 | }, |
54 | body: params.toString() |
55 | }) |
56 |
|
57 | if (!response.ok) { |
58 | const text = await response.text() |
59 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
60 | } |
61 |
|
62 | const data = await response.json() |
63 | return data.access_token |
64 | } |
65 |
|