1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Delete Earning by Earning Code and Start Date |
8 | * Delete Earning by Earning Code and Start Date |
9 | */ |
10 | export async function main( |
11 | auth: Paylocity, |
12 | companyId: string, |
13 | employeeId: string, |
14 | earningCode: string, |
15 | startDate: string |
16 | ) { |
17 | const url = new URL( |
18 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employees/${employeeId}/earnings/${earningCode}/${startDate}` |
19 | ) |
20 |
|
21 | const response = await fetch(url, { |
22 | method: 'DELETE', |
23 | headers: { |
24 | Authorization: |
25 | 'Bearer ' + |
26 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
27 | }, |
28 | body: undefined |
29 | }) |
30 | if (!response.ok) { |
31 | const text = await response.text() |
32 | throw new Error(`${response.status} ${text}`) |
33 | } |
34 | return await response.text() |
35 | } |
36 |
|
37 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
38 | const params = new URLSearchParams({ |
39 | grant_type: 'client_credentials', |
40 | client_id: auth.clientId, |
41 | client_secret: auth.clientSecret |
42 | }) |
43 |
|
44 | const response = await fetch(tokenUrl, { |
45 | method: 'POST', |
46 | headers: { |
47 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
48 | 'Content-Type': 'application/x-www-form-urlencoded' |
49 | }, |
50 | body: params.toString() |
51 | }) |
52 |
|
53 | if (!response.ok) { |
54 | const text = await response.text() |
55 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
56 | } |
57 |
|
58 | const data = await response.json() |
59 | return data.access_token |
60 | } |
61 |
|