1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get Employee [Single] |
8 | * **Summary Description** |
9 |
|
10 | The GET Single Employee API endpoint allows users to fetch specific employee details from a client's Paylocity instance. |
11 | */ |
12 | export async function main( |
13 | auth: Paylocity, |
14 | companyId: string, |
15 | employeeId: string, |
16 | include: string | undefined, |
17 | testMode?: string |
18 | ) { |
19 | const url = new URL( |
20 | `https://dc1prodgwext.paylocity.com/coreHr/v1/companies/${companyId}/employees/${employeeId}` |
21 | ) |
22 | for (const [k, v] of [['include', include]]) { |
23 | if (v !== undefined && v !== '' && k !== undefined) { |
24 | url.searchParams.append(k, v) |
25 | } |
26 | } |
27 | const response = await fetch(url, { |
28 | method: 'GET', |
29 | headers: { |
30 | ...(testMode ? { testMode: testMode } : {}), |
31 | Authorization: |
32 | 'Bearer ' + |
33 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
34 | }, |
35 | body: undefined |
36 | }) |
37 | if (!response.ok) { |
38 | const text = await response.text() |
39 | throw new Error(`${response.status} ${text}`) |
40 | } |
41 | return await response.json() |
42 | } |
43 |
|
44 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
45 | const params = new URLSearchParams({ |
46 | grant_type: 'client_credentials', |
47 | client_id: auth.clientId, |
48 | client_secret: auth.clientSecret |
49 | }) |
50 |
|
51 | const response = await fetch(tokenUrl, { |
52 | method: 'POST', |
53 | headers: { |
54 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
55 | 'Content-Type': 'application/x-www-form-urlencoded' |
56 | }, |
57 | body: params.toString() |
58 | }) |
59 |
|
60 | if (!response.ok) { |
61 | const text = await response.text() |
62 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
63 | } |
64 |
|
65 | const data = await response.json() |
66 | return data.access_token |
67 | } |
68 |
|