1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Add/Update Earning |
8 | * Add/Update Earning API sends new or updated employee earnings information directly to Paylocity Payroll/HR solution. |
9 | */ |
10 | export async function main( |
11 | auth: Paylocity, |
12 | companyId: string, |
13 | employeeId: string, |
14 | body: { |
15 | agency?: string |
16 | amount?: number |
17 | annualMaximum?: number |
18 | calculationCode?: string |
19 | costCenter1?: string |
20 | costCenter2?: string |
21 | costCenter3?: string |
22 | earningCode: string |
23 | effectiveDate?: string |
24 | endDate?: string |
25 | frequency?: string |
26 | goal?: number |
27 | hoursOrUnits?: number |
28 | isSelfInsured?: false | true |
29 | jobCode?: string |
30 | miscellaneousInfo?: string |
31 | paidTowardsGoal?: number |
32 | payPeriodMaximum?: number |
33 | payPeriodMinimum?: number |
34 | rate?: number |
35 | rateCode?: string |
36 | startDate: string |
37 | } |
38 | ) { |
39 | const url = new URL( |
40 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employees/${employeeId}/earnings` |
41 | ) |
42 |
|
43 | const response = await fetch(url, { |
44 | method: 'PUT', |
45 | headers: { |
46 | 'Content-Type': 'application/json', |
47 | Authorization: |
48 | 'Bearer ' + |
49 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
50 | }, |
51 | body: JSON.stringify(body) |
52 | }) |
53 | if (!response.ok) { |
54 | const text = await response.text() |
55 | throw new Error(`${response.status} ${text}`) |
56 | } |
57 | return await response.text() |
58 | } |
59 |
|
60 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
61 | const params = new URLSearchParams({ |
62 | grant_type: 'client_credentials', |
63 | client_id: auth.clientId, |
64 | client_secret: auth.clientSecret |
65 | }) |
66 |
|
67 | const response = await fetch(tokenUrl, { |
68 | method: 'POST', |
69 | headers: { |
70 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
71 | 'Content-Type': 'application/x-www-form-urlencoded' |
72 | }, |
73 | body: params.toString() |
74 | }) |
75 |
|
76 | if (!response.ok) { |
77 | const text = await response.text() |
78 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
79 | } |
80 |
|
81 | const data = await response.json() |
82 | return data.access_token |
83 | } |
84 |
|