1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Returns employee shifts |
8 | * > 🚧 Beta Phase |
9 | > |
10 | > This resource is in closed beta. |
11 | */ |
12 | export async function main( |
13 | auth: Paylocity, |
14 | companyId: string, |
15 | employeeId: string, |
16 | include: string | undefined, |
17 | filter: string | undefined, |
18 | includeTotalCount: string | undefined, |
19 | limit: string | undefined, |
20 | offset: string | undefined, |
21 | sort: string | undefined |
22 | ) { |
23 | const url = new URL( |
24 | `https://dc1prodgwext.paylocity.com/apiHub/scheduling/v1/companies/${companyId}/employees/${employeeId}/shifts` |
25 | ) |
26 | for (const [k, v] of [ |
27 | ['include', include], |
28 | ['filter', filter], |
29 | ['includeTotalCount', includeTotalCount], |
30 | ['limit', limit], |
31 | ['offset', offset], |
32 | ['sort', sort] |
33 | ]) { |
34 | if (v !== undefined && v !== '' && k !== undefined) { |
35 | url.searchParams.append(k, v) |
36 | } |
37 | } |
38 | const response = await fetch(url, { |
39 | method: 'GET', |
40 | headers: { |
41 | Authorization: |
42 | 'Bearer ' + |
43 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
44 | }, |
45 | body: undefined |
46 | }) |
47 | if (!response.ok) { |
48 | const text = await response.text() |
49 | throw new Error(`${response.status} ${text}`) |
50 | } |
51 | return await response.json() |
52 | } |
53 |
|
54 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
55 | const params = new URLSearchParams({ |
56 | grant_type: 'client_credentials', |
57 | client_id: auth.clientId, |
58 | client_secret: auth.clientSecret |
59 | }) |
60 |
|
61 | const response = await fetch(tokenUrl, { |
62 | method: 'POST', |
63 | headers: { |
64 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
65 | 'Content-Type': 'application/x-www-form-urlencoded' |
66 | }, |
67 | body: params.toString() |
68 | }) |
69 |
|
70 | if (!response.ok) { |
71 | const text = await response.text() |
72 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
73 | } |
74 |
|
75 | const data = await response.json() |
76 | return data.access_token |
77 | } |
78 |
|