1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Upsert Screening Package |
8 | * > 🚧 Partner Restricted |
9 | > All background check API endpoints are restricted to background check 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 | isActive?: false | true |
17 | packageSummary?: string |
18 | packageName?: string |
19 | services?: { serviceName?: string; serviceSummary?: string }[] |
20 | price?: { value?: number; currency?: string } |
21 | billingCode?: string |
22 | isInternational?: false | true |
23 | }, |
24 | testMode?: string |
25 | ) { |
26 | const url = new URL( |
27 | `https://dc1prodgwext.paylocity.com/compliance/v1/companies/${companyId}/backgroundCheck/screeningPackages/${packageId}` |
28 | ) |
29 |
|
30 | const response = await fetch(url, { |
31 | method: 'POST', |
32 | headers: { |
33 | ...(testMode ? { testMode: testMode } : {}), |
34 | 'Content-Type': 'application/json', |
35 | Authorization: |
36 | 'Bearer ' + |
37 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
38 | }, |
39 | body: JSON.stringify(body) |
40 | }) |
41 | if (!response.ok) { |
42 | const text = await response.text() |
43 | throw new Error(`${response.status} ${text}`) |
44 | } |
45 | return await response.json() |
46 | } |
47 |
|
48 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
49 | const params = new URLSearchParams({ |
50 | grant_type: 'client_credentials', |
51 | client_id: auth.clientId, |
52 | client_secret: auth.clientSecret |
53 | }) |
54 |
|
55 | const response = await fetch(tokenUrl, { |
56 | method: 'POST', |
57 | headers: { |
58 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
59 | 'Content-Type': 'application/x-www-form-urlencoded' |
60 | }, |
61 | body: params.toString() |
62 | }) |
63 |
|
64 | if (!response.ok) { |
65 | const text = await response.text() |
66 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
67 | } |
68 |
|
69 | const data = await response.json() |
70 | return data.access_token |
71 | } |
72 |
|