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