1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Update Recurring Earning Details for a specific past/present/future earning |
8 | * **Summary Description** |
9 |
|
10 | This function will allow the API user to update details for a particular earning code that occurs in the past/present/future. |
11 | */ |
12 | export async function main( |
13 | auth: Paylocity, |
14 | companyId: string, |
15 | employeeId: string, |
16 | earningCode: string, |
17 | resourceId: string, |
18 | body: { |
19 | effectiveFrom?: string |
20 | effectiveTo?: string |
21 | amount?: number |
22 | rate?: number |
23 | units?: number |
24 | selfInsured?: false | true |
25 | calculationCode?: string |
26 | frequency?: string |
27 | agency?: string |
28 | miscellaneousInfo?: string |
29 | rateCode?: string |
30 | distribution?: { |
31 | jobCode?: string |
32 | costCenters?: { level?: number; code?: string }[] |
33 | } |
34 | limits?: { |
35 | goal?: number |
36 | paidToDate?: number |
37 | payPeriodMinimum?: number |
38 | payPeriodMaximum?: number |
39 | annualMaximum?: number |
40 | } |
41 | } |
42 | ) { |
43 | const url = new URL( |
44 | `https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/employees/${employeeId}/earnings/${earningCode}/${resourceId}` |
45 | ) |
46 |
|
47 | const response = await fetch(url, { |
48 | method: 'PUT', |
49 | headers: { |
50 | 'Content-Type': 'application/json', |
51 | Authorization: |
52 | 'Bearer ' + |
53 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
54 | }, |
55 | body: JSON.stringify(body) |
56 | }) |
57 | if (!response.ok) { |
58 | const text = await response.text() |
59 | throw new Error(`${response.status} ${text}`) |
60 | } |
61 | return await response.text() |
62 | } |
63 |
|
64 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
65 | const params = new URLSearchParams({ |
66 | grant_type: 'client_credentials', |
67 | client_id: auth.clientId, |
68 | client_secret: auth.clientSecret |
69 | }) |
70 |
|
71 | const response = await fetch(tokenUrl, { |
72 | method: 'POST', |
73 | headers: { |
74 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
75 | 'Content-Type': 'application/x-www-form-urlencoded' |
76 | }, |
77 | body: params.toString() |
78 | }) |
79 |
|
80 | if (!response.ok) { |
81 | const text = await response.text() |
82 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
83 | } |
84 |
|
85 | const data = await response.json() |
86 | return data.access_token |
87 | } |
88 |
|