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