1 | |
2 | |
3 | * Checks by Pay Period and User |
4 | * Delete checks by pay period Id and user Id. |
5 | */ |
6 | export async function main( |
7 | auth: RT.Paychex, |
8 | payperiodid: string | undefined, |
9 | deletebyuserid: string | undefined |
10 | ) { |
11 | const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token') |
12 | const url = new URL(`https://api.paychex.com/checks`) |
13 | for (const [k, v] of [ |
14 | ['payperiodid', payperiodid], |
15 | ['deletebyuserid', deletebyuserid] |
16 | ]) { |
17 | if (v !== undefined && v !== '') { |
18 | url.searchParams.append(k, v) |
19 | } |
20 | } |
21 | const response = await fetch(url, { |
22 | method: 'DELETE', |
23 | headers: { |
24 | Authorization: 'Bearer ' + accessToken |
25 | }, |
26 | body: undefined |
27 | }) |
28 | if (!response.ok) { |
29 | const text = await response.text() |
30 | throw new Error(`${response.status} ${text}`) |
31 | } |
32 | return await response.text() |
33 | } |
34 |
|
35 | async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> { |
36 | const params = new URLSearchParams({ |
37 | grant_type: 'client_credentials' |
38 | }) |
39 |
|
40 | const response = await fetch(tokenUrl, { |
41 | method: 'POST', |
42 | headers: { |
43 | Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`), |
44 | 'Content-Type': 'application/x-www-form-urlencoded' |
45 | }, |
46 | body: params.toString() |
47 | }) |
48 |
|
49 | if (!response.ok) { |
50 | const text = await response.text() |
51 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
52 | } |
53 |
|
54 | const data = await response.json() |
55 | return data.access_token |
56 | } |
57 |
|