1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Update Job Code |
8 | * **Summary Description** |
9 |
|
10 | The PUT Job Code endpoint enables users to update precise values regarding Job Codes from the Paylocity instance of a client. |
11 | */ |
12 | export async function main( |
13 | auth: Paylocity, |
14 | companyId: string, |
15 | jobCode: string, |
16 | body: { |
17 | description?: string |
18 | isActive?: false | true |
19 | isCertified?: false | true |
20 | payEntry?: { |
21 | shift?: string |
22 | rateCode?: string |
23 | rate?: number |
24 | addingRateConstant?: number |
25 | multiplyingRateConstant?: number |
26 | workerComputedCode?: string |
27 | tax?: { |
28 | state?: string |
29 | local1?: string |
30 | local2?: string |
31 | local3?: string |
32 | } |
33 | } |
34 | address?: { |
35 | line1?: string |
36 | line2?: string |
37 | city?: string |
38 | state?: string |
39 | zip?: string |
40 | county?: string |
41 | country?: string |
42 | } |
43 | payrollBasedJournal?: { |
44 | jobTitleCode?: string |
45 | facilityId?: string |
46 | stateId?: string |
47 | } |
48 | } |
49 | ) { |
50 | const url = new URL( |
51 | `https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/jobs/${jobCode}` |
52 | ) |
53 |
|
54 | const response = await fetch(url, { |
55 | method: 'PUT', |
56 | headers: { |
57 | 'Content-Type': 'application/json', |
58 | Authorization: |
59 | 'Bearer ' + |
60 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
61 | }, |
62 | body: JSON.stringify(body) |
63 | }) |
64 | if (!response.ok) { |
65 | const text = await response.text() |
66 | throw new Error(`${response.status} ${text}`) |
67 | } |
68 | return await response.text() |
69 | } |
70 |
|
71 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
72 | const params = new URLSearchParams({ |
73 | grant_type: 'client_credentials', |
74 | client_id: auth.clientId, |
75 | client_secret: auth.clientSecret |
76 | }) |
77 |
|
78 | const response = await fetch(tokenUrl, { |
79 | method: 'POST', |
80 | headers: { |
81 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
82 | 'Content-Type': 'application/x-www-form-urlencoded' |
83 | }, |
84 | body: params.toString() |
85 | }) |
86 |
|
87 | if (!response.ok) { |
88 | const text = await response.text() |
89 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
90 | } |
91 |
|
92 | const data = await response.json() |
93 | return data.access_token |
94 | } |
95 |
|