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