1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Add/update sensitive data |
8 | * Sends new or updated employee sensitive data 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 | disability?: { |
16 | disability?: string |
17 | disabilityClassifications?: { classification?: string }[] |
18 | hasDisability?: string |
19 | } |
20 | ethnicity?: { |
21 | ethnicity?: string |
22 | ethnicRacialIdentities?: { description?: string }[] |
23 | } |
24 | gender?: { |
25 | displayPronouns?: false | true |
26 | genderIdentityDescription?: string |
27 | identifyAsLegalGender?: string |
28 | legalGender?: string |
29 | pronouns?: string |
30 | sexualOrientation?: string |
31 | } |
32 | veteran?: { isVeteran?: string; veteran?: string } |
33 | } |
34 | ) { |
35 | const url = new URL( |
36 | `https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/employees/${employeeId}/sensitivedata` |
37 | ) |
38 |
|
39 | const response = await fetch(url, { |
40 | method: 'PUT', |
41 | headers: { |
42 | 'Content-Type': 'application/json', |
43 | Authorization: |
44 | 'Bearer ' + |
45 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
46 | }, |
47 | body: JSON.stringify(body) |
48 | }) |
49 | if (!response.ok) { |
50 | const text = await response.text() |
51 | throw new Error(`${response.status} ${text}`) |
52 | } |
53 | return await response.text() |
54 | } |
55 |
|
56 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
57 | const params = new URLSearchParams({ |
58 | grant_type: 'client_credentials', |
59 | client_id: auth.clientId, |
60 | client_secret: auth.clientSecret |
61 | }) |
62 |
|
63 | const response = await fetch(tokenUrl, { |
64 | method: 'POST', |
65 | headers: { |
66 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
67 | 'Content-Type': 'application/x-www-form-urlencoded' |
68 | }, |
69 | body: params.toString() |
70 | }) |
71 |
|
72 | if (!response.ok) { |
73 | const text = await response.text() |
74 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
75 | } |
76 |
|
77 | const data = await response.json() |
78 | return data.access_token |
79 | } |
80 |
|