1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get a list of documents for a company |
8 | * **Summary Description** |
9 |
|
10 | The GET company documents resource will return a list of documents for a specified company ID. |
11 |
|
12 | */ |
13 | export async function main( |
14 | auth: Paylocity, |
15 | companyId: string, |
16 | uploadedDate: string | undefined, |
17 | uploadedDate_lessThanOrEqualTo: string | undefined, |
18 | uploadedDate_greaterThanOrEqualTo: string | undefined, |
19 | includeTotalCount: string | undefined, |
20 | limit: string | undefined, |
21 | offset: string | undefined |
22 | ) { |
23 | const url = new URL( |
24 | `https://dc1prodgwext.paylocity.com/documents/v1/companies/${companyId}/companyDocuments` |
25 | ) |
26 | for (const [k, v] of [ |
27 | ['uploadedDate', uploadedDate], |
28 | ['uploadedDate.lessThanOrEqualTo', uploadedDate_lessThanOrEqualTo], |
29 | ['uploadedDate.greaterThanOrEqualTo', uploadedDate_greaterThanOrEqualTo], |
30 | ['includeTotalCount', includeTotalCount], |
31 | ['limit', limit], |
32 | ['offset', offset] |
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 |
|