1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get Job Code [Single] |
8 | * **Summary Description** |
9 |
|
10 | The GET Single Job Code API endpoint allows users to fetch specific Job Code details from a client's Paylocity instance. To use this endpoint, the Paylocity Company ID and Job Code must be specified |
11 |
|
12 | **Use Cases** |
13 |
|
14 | - This API endpoint will allow an API user to retrieve a Job Code record for a specific company ID and Job Code ID |
15 |
|
16 | */ |
17 | export async function main(auth: Paylocity, companyId: string, jobCode: string, testMode?: string) { |
18 | const url = new URL( |
19 | `https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/jobs/${jobCode}` |
20 | ) |
21 |
|
22 | const response = await fetch(url, { |
23 | method: 'GET', |
24 | headers: { |
25 | ...(testMode ? { testMode: testMode } : {}), |
26 | Authorization: |
27 | 'Bearer ' + |
28 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
29 | }, |
30 | body: undefined |
31 | }) |
32 | if (!response.ok) { |
33 | const text = await response.text() |
34 | throw new Error(`${response.status} ${text}`) |
35 | } |
36 | return await response.json() |
37 | } |
38 |
|
39 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
40 | const params = new URLSearchParams({ |
41 | grant_type: 'client_credentials', |
42 | client_id: auth.clientId, |
43 | client_secret: auth.clientSecret |
44 | }) |
45 |
|
46 | const response = await fetch(tokenUrl, { |
47 | method: 'POST', |
48 | headers: { |
49 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
50 | 'Content-Type': 'application/x-www-form-urlencoded' |
51 | }, |
52 | body: params.toString() |
53 | }) |
54 |
|
55 | if (!response.ok) { |
56 | const text = await response.text() |
57 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
58 | } |
59 |
|
60 | const data = await response.json() |
61 | return data.access_token |
62 | } |
63 |
|