1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Upsert Callback |
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 | body: { |
14 | callbackDetails?: { |
15 | successCallbackUrl?: string |
16 | errorCallbackUrl?: string |
17 | jobCreationCallbackUrl?: string |
18 | } |
19 | callerDetails?: { callerName?: string } |
20 | apiKey?: string |
21 | orderLevel?: 'Assessment' | 'Test' |
22 | }, |
23 | testMode?: string |
24 | ) { |
25 | const url = new URL( |
26 | `https://dc1prodgwext.paylocity.com/apiHub/performanceManagement/v1/companies/b6001/assessmentOrders/callback` |
27 | ) |
28 |
|
29 | const response = await fetch(url, { |
30 | method: 'PUT', |
31 | headers: { |
32 | ...(testMode ? { testMode: testMode } : {}), |
33 | 'Content-Type': 'application/json', |
34 | Authorization: |
35 | 'Bearer ' + |
36 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
37 | }, |
38 | body: JSON.stringify(body) |
39 | }) |
40 | if (!response.ok) { |
41 | const text = await response.text() |
42 | throw new Error(`${response.status} ${text}`) |
43 | } |
44 | return await response.json() |
45 | } |
46 |
|
47 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
48 | const params = new URLSearchParams({ |
49 | grant_type: 'client_credentials', |
50 | client_id: auth.clientId, |
51 | client_secret: auth.clientSecret |
52 | }) |
53 |
|
54 | const response = await fetch(tokenUrl, { |
55 | method: 'POST', |
56 | headers: { |
57 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
58 | 'Content-Type': 'application/x-www-form-urlencoded' |
59 | }, |
60 | body: params.toString() |
61 | }) |
62 |
|
63 | if (!response.ok) { |
64 | const text = await response.text() |
65 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
66 | } |
67 |
|
68 | const data = await response.json() |
69 | return data.access_token |
70 | } |
71 |
|