1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Add Employee to Onboarding |
8 | * Onboarding API sends employee data into Paylocity Onboarding to help ensure an easy and accurate hiring process for subsequent completion into Paylocity Payroll/HR solution. |
9 | */ |
10 | export async function main( |
11 | auth: Paylocity, |
12 | companyId: string, |
13 | body: { |
14 | employeeId?: string |
15 | address1?: string |
16 | address2?: string |
17 | autoPayType?: string |
18 | baseRate?: number |
19 | city?: string |
20 | costCenter1?: string |
21 | costCenter2?: string |
22 | costCenter3?: string |
23 | defaultHours?: number |
24 | employeeStatus?: string |
25 | employmentType?: string |
26 | federalFilingStatus?: string |
27 | firstName: string |
28 | sex?: string |
29 | hireDate?: string |
30 | homePhone?: string |
31 | lastName: string |
32 | maritalStatus?: string |
33 | personalMobilePhone?: string |
34 | payFrequency?: string |
35 | personalEmailAddress?: string |
36 | payType?: string |
37 | ratePer?: string |
38 | salary?: number |
39 | state?: string |
40 | ssn?: string |
41 | stateFilingStatus?: string |
42 | suiState?: string |
43 | taxForm?: string |
44 | taxState?: string |
45 | userName?: string |
46 | workEmailAddress?: string |
47 | zip?: string |
48 | } |
49 | ) { |
50 | const url = new URL( |
51 | `https://dc1prodgwext.paylocity.com/api/v1/companies/${companyId}/onboarding/employees` |
52 | ) |
53 |
|
54 | const response = await fetch(url, { |
55 | method: 'POST', |
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 |
|