1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Update Local Tax |
8 | * Update Local Tax sends updated local tax code information for the selected employee. |
9 | */ |
10 | export async function main( |
11 | auth: Paylocity, |
12 | companyId: string, |
13 | employeeId: string, |
14 | taxCode: string, |
15 | body: { |
16 | exemptions?: number |
17 | exemptions2?: number |
18 | filingStatus?: string |
19 | residentPSD?: string |
20 | taxCode?: string |
21 | workPSD?: string |
22 | } |
23 | ) { |
24 | const url = new URL( |
25 | `https://dc1prodgwext.paylocity.com/api/v1/companies/${companyId}/employees/${employeeId}/localTaxes/${taxCode}` |
26 | ) |
27 |
|
28 | const response = await fetch(url, { |
29 | method: 'PUT', |
30 | headers: { |
31 | 'Content-Type': 'application/json', |
32 | Authorization: |
33 | 'Bearer ' + |
34 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
35 | }, |
36 | body: JSON.stringify(body) |
37 | }) |
38 | if (!response.ok) { |
39 | const text = await response.text() |
40 | throw new Error(`${response.status} ${text}`) |
41 | } |
42 | return await response.text() |
43 | } |
44 |
|
45 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
46 | const params = new URLSearchParams({ |
47 | grant_type: 'client_credentials', |
48 | client_id: auth.clientId, |
49 | client_secret: auth.clientSecret |
50 | }) |
51 |
|
52 | const response = await fetch(tokenUrl, { |
53 | method: 'POST', |
54 | headers: { |
55 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
56 | 'Content-Type': 'application/x-www-form-urlencoded' |
57 | }, |
58 | body: params.toString() |
59 | }) |
60 |
|
61 | if (!response.ok) { |
62 | const text = await response.text() |
63 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
64 | } |
65 |
|
66 | const data = await response.json() |
67 | return data.access_token |
68 | } |
69 |
|