1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get a list of documents filtered by employee for a company |
8 | * **Summary Description** |
9 |
|
10 | This resource allows for looking up documents with additional filters such as employee, upload date, and max number of records to return. |
11 |
|
12 | */ |
13 | export async function main( |
14 | auth: Paylocity, |
15 | companyId: string, |
16 | employeeId: string | undefined, |
17 | uploadedDate: string | undefined, |
18 | uploadedDate_lessThanOrEqualTo: string | undefined, |
19 | uploadedDate_greaterThanOrEqualTo: string | undefined, |
20 | includeTotalCount: string | undefined, |
21 | limit: string | undefined, |
22 | offset: string | undefined |
23 | ) { |
24 | const url = new URL( |
25 | `https://dc1prodgwext.paylocity.com/documents/v1/companies/${companyId}/employeeDocuments` |
26 | ) |
27 | for (const [k, v] of [ |
28 | ['employeeId', employeeId], |
29 | ['uploadedDate', uploadedDate], |
30 | ['uploadedDate.lessThanOrEqualTo', uploadedDate_lessThanOrEqualTo], |
31 | ['uploadedDate.greaterThanOrEqualTo', uploadedDate_greaterThanOrEqualTo], |
32 | ['includeTotalCount', includeTotalCount], |
33 | ['limit', limit], |
34 | ['offset', offset] |
35 | ]) { |
36 | if (v !== undefined && v !== '' && k !== undefined) { |
37 | url.searchParams.append(k, v) |
38 | } |
39 | } |
40 | const response = await fetch(url, { |
41 | method: 'GET', |
42 | headers: { |
43 | Authorization: |
44 | 'Bearer ' + |
45 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
46 | }, |
47 | body: undefined |
48 | }) |
49 | if (!response.ok) { |
50 | const text = await response.text() |
51 | throw new Error(`${response.status} ${text}`) |
52 | } |
53 | return await response.json() |
54 | } |
55 |
|
56 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
57 | const params = new URLSearchParams({ |
58 | grant_type: 'client_credentials', |
59 | client_id: auth.clientId, |
60 | client_secret: auth.clientSecret |
61 | }) |
62 |
|
63 | const response = await fetch(tokenUrl, { |
64 | method: 'POST', |
65 | headers: { |
66 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
67 | 'Content-Type': 'application/x-www-form-urlencoded' |
68 | }, |
69 | body: params.toString() |
70 | }) |
71 |
|
72 | if (!response.ok) { |
73 | const text = await response.text() |
74 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
75 | } |
76 |
|
77 | const data = await response.json() |
78 | return data.access_token |
79 | } |
80 |
|