1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Delete Assessment Package |
8 | * > 🚧 Partner Restricted |
9 | > All assessment API endpoints are restricted to assessment providers that have signed a Paylocity technology partnership agreement. |
10 | */ |
11 | export async function main( |
12 | auth: Paylocity, |
13 | companyId: string, |
14 | packageId: string, |
15 | testMode?: string |
16 | ) { |
17 | const url = new URL( |
18 | `https://dc1prodgwext.paylocity.com/apiHub/performanceManagement/v1/companies/${companyId}/assessmentPackages/${packageId}` |
19 | ) |
20 |
|
21 | const response = await fetch(url, { |
22 | method: 'DELETE', |
23 | headers: { |
24 | ...(testMode ? { testMode: testMode } : {}), |
25 | Authorization: |
26 | 'Bearer ' + |
27 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
28 | }, |
29 | body: undefined |
30 | }) |
31 | if (!response.ok) { |
32 | const text = await response.text() |
33 | throw new Error(`${response.status} ${text}`) |
34 | } |
35 | return await response.text() |
36 | } |
37 |
|
38 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
39 | const params = new URLSearchParams({ |
40 | grant_type: 'client_credentials', |
41 | client_id: auth.clientId, |
42 | client_secret: auth.clientSecret |
43 | }) |
44 |
|
45 | const response = await fetch(tokenUrl, { |
46 | method: 'POST', |
47 | headers: { |
48 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
49 | 'Content-Type': 'application/x-www-form-urlencoded' |
50 | }, |
51 | body: params.toString() |
52 | }) |
53 |
|
54 | if (!response.ok) { |
55 | const text = await response.text() |
56 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
57 | } |
58 |
|
59 | const data = await response.json() |
60 | return data.access_token |
61 | } |
62 |
|