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