Creates an employees

Script xero Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 515 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Creates an employees
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	Xero_Tenant_Id: string,
12
	Idempotency_Key: string,
13
	body: {
14
		employeeID?: string
15
		title?: string
16
		firstName: string
17
		lastName: string
18
		dateOfBirth: string
19
		address: {
20
			addressLine1: string
21
			addressLine2?: string
22
			city: string
23
			suburb?: string
24
			postCode: string
25
			countryName?: string
26
		}
27
		email?: string
28
		gender?: 'M' | 'F'
29
		phoneNumber?: string
30
		startDate?: string
31
		endDate?: string
32
		payrollCalendarID?: string
33
		updatedDateUTC?: string
34
		createdDateUTC?: string
35
		jobTitle?: string
36
		engagementType?: string
37
		fixedTermEndDate?: string
38
	}
39
) {
40
	const url = new URL(`https://api.xero.com/payroll.xro/2.0/Employees`)
41

42
	const response = await fetch(url, {
43
		method: 'POST',
44
		headers: {
45
			Accept: 'application/json',
46
			'Xero-Tenant-Id': Xero_Tenant_Id,
47
			'Idempotency-Key': Idempotency_Key,
48
			'Content-Type': 'application/json',
49
			Authorization: 'Bearer ' + auth.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.json()
58
}
59