1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Upsert 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 | body: { |
16 | id?: string |
17 | status?: { |
18 | startDate?: string |
19 | endDate?: string |
20 | value?: 'Active' | 'Inactive' | 'Deleted' | 'Unknown' |
21 | } |
22 | summary?: string |
23 | name?: string |
24 | assessments?: { |
25 | id?: string |
26 | name?: string |
27 | summary?: string |
28 | tests?: { |
29 | id?: string |
30 | name?: string |
31 | category?: string |
32 | description?: string |
33 | }[] |
34 | }[] |
35 | }, |
36 | testMode?: string |
37 | ) { |
38 | const url = new URL( |
39 | `https://dc1prodgwext.paylocity.com/apiHub/performanceManagement/v1/companies/${companyId}/assessmentPackages/${packageId}` |
40 | ) |
41 |
|
42 | const response = await fetch(url, { |
43 | method: 'PUT', |
44 | headers: { |
45 | ...(testMode ? { testMode: testMode } : {}), |
46 | 'Content-Type': 'application/json', |
47 | Authorization: |
48 | 'Bearer ' + |
49 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
50 | }, |
51 | body: JSON.stringify(body) |
52 | }) |
53 | if (!response.ok) { |
54 | const text = await response.text() |
55 | throw new Error(`${response.status} ${text}`) |
56 | } |
57 | return await response.json() |
58 | } |
59 |
|
60 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
61 | const params = new URLSearchParams({ |
62 | grant_type: 'client_credentials', |
63 | client_id: auth.clientId, |
64 | client_secret: auth.clientSecret |
65 | }) |
66 |
|
67 | const response = await fetch(tokenUrl, { |
68 | method: 'POST', |
69 | headers: { |
70 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
71 | 'Content-Type': 'application/x-www-form-urlencoded' |
72 | }, |
73 | body: params.toString() |
74 | }) |
75 |
|
76 | if (!response.ok) { |
77 | const text = await response.text() |
78 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
79 | } |
80 |
|
81 | const data = await response.json() |
82 | return data.access_token |
83 | } |
84 |
|