1 | |
2 | |
3 | * Custom Field |
4 | * Delete CustomField at the company level. |
5 | */ |
6 | export async function main(auth: RT.Paychex, companyId: string, customfieldid: string) { |
7 | const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token') |
8 | const url = new URL( |
9 | `https://api.paychex.com/companies/${companyId}/customfields/${customfieldid}` |
10 | ) |
11 |
|
12 | const response = await fetch(url, { |
13 | method: 'DELETE', |
14 | headers: { |
15 | Authorization: 'Bearer ' + accessToken |
16 | }, |
17 | body: undefined |
18 | }) |
19 | if (!response.ok) { |
20 | const text = await response.text() |
21 | throw new Error(`${response.status} ${text}`) |
22 | } |
23 | return await response.text() |
24 | } |
25 |
|
26 | async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> { |
27 | const params = new URLSearchParams({ |
28 | grant_type: 'client_credentials' |
29 | }) |
30 |
|
31 | const response = await fetch(tokenUrl, { |
32 | method: 'POST', |
33 | headers: { |
34 | Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`), |
35 | 'Content-Type': 'application/x-www-form-urlencoded' |
36 | }, |
37 | body: params.toString() |
38 | }) |
39 |
|
40 | if (!response.ok) { |
41 | const text = await response.text() |
42 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
43 | } |
44 |
|
45 | const data = await response.json() |
46 | return data.access_token |
47 | } |
48 |
|