1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Create an employee pay rates search |
8 | * > 🚧 Beta Phase |
9 | > |
10 | > This resource is in closed beta. Paylocity Technology Partners interested may contact the marketplace team to explore early adopter opportunities at [marketplaceapisupport@paylocity.com](mailto:marketplaceapisupport@paylocity.com) |
11 |
|
12 | **Summary Description** |
13 |
|
14 | Performs a pay rates search for the specified company according to the search parameters passed in the request body. |
15 | */ |
16 | export async function main( |
17 | auth: Paylocity, |
18 | companyId: string, |
19 | body: { |
20 | effectiveDateFlag?: 'History' | 'Future' | 'All' |
21 | employeeIds?: string[] |
22 | } |
23 | ) { |
24 | const url = new URL( |
25 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employeePayRateSearches` |
26 | ) |
27 |
|
28 | const response = await fetch(url, { |
29 | method: 'POST', |
30 | headers: { |
31 | 'Content-Type': 'application/json', |
32 | Authorization: |
33 | 'Bearer ' + |
34 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
35 | }, |
36 | body: JSON.stringify(body) |
37 | }) |
38 | if (!response.ok) { |
39 | const text = await response.text() |
40 | throw new Error(`${response.status} ${text}`) |
41 | } |
42 | return await response.json() |
43 | } |
44 |
|
45 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
46 | const params = new URLSearchParams({ |
47 | grant_type: 'client_credentials', |
48 | client_id: auth.clientId, |
49 | client_secret: auth.clientSecret |
50 | }) |
51 |
|
52 | const response = await fetch(tokenUrl, { |
53 | method: 'POST', |
54 | headers: { |
55 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
56 | 'Content-Type': 'application/x-www-form-urlencoded' |
57 | }, |
58 | body: params.toString() |
59 | }) |
60 |
|
61 | if (!response.ok) { |
62 | const text = await response.text() |
63 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
64 | } |
65 |
|
66 | const data = await response.json() |
67 | return data.access_token |
68 | } |
69 |
|