1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Add/update additional rates |
8 | * Sends new or updated employee additional rates 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 | changeReason?: string |
16 | costCenter1?: string |
17 | costCenter2?: string |
18 | costCenter3?: string |
19 | effectiveDate?: string |
20 | endCheckDate?: string |
21 | job?: string |
22 | rate?: number |
23 | rateCode?: string |
24 | rateNotes?: string |
25 | ratePer?: string |
26 | shift?: string |
27 | } |
28 | ) { |
29 | const url = new URL( |
30 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employees/${employeeId}/additionalRates` |
31 | ) |
32 |
|
33 | const response = await fetch(url, { |
34 | method: 'PUT', |
35 | headers: { |
36 | 'Content-Type': 'application/json', |
37 | Authorization: |
38 | 'Bearer ' + |
39 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
40 | }, |
41 | body: JSON.stringify(body) |
42 | }) |
43 | if (!response.ok) { |
44 | const text = await response.text() |
45 | throw new Error(`${response.status} ${text}`) |
46 | } |
47 | return await response.text() |
48 | } |
49 |
|
50 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
51 | const params = new URLSearchParams({ |
52 | grant_type: 'client_credentials', |
53 | client_id: auth.clientId, |
54 | client_secret: auth.clientSecret |
55 | }) |
56 |
|
57 | const response = await fetch(tokenUrl, { |
58 | method: 'POST', |
59 | headers: { |
60 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
61 | 'Content-Type': 'application/x-www-form-urlencoded' |
62 | }, |
63 | body: params.toString() |
64 | }) |
65 |
|
66 | if (!response.ok) { |
67 | const text = await response.text() |
68 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
69 | } |
70 |
|
71 | const data = await response.json() |
72 | return data.access_token |
73 | } |
74 |
|