Creates leave records for a specific employee

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 leave records for a specific employee
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	EmployeeID: string,
12
	Xero_Tenant_Id: string,
13
	Idempotency_Key: string,
14
	body: {
15
		leaveID?: string
16
		leaveTypeID: string
17
		description: string
18
		startDate: string
19
		endDate: string
20
		periods?: {
21
			periodStartDate?: string
22
			periodEndDate?: string
23
			numberOfUnits?: number
24
			periodStatus?: 'Approved' | 'Completed'
25
		}[]
26
		updatedDateUTC?: string
27
	}
28
) {
29
	const url = new URL(`https://api.xero.com/payroll.xro/2.0/Employees/${EmployeeID}/Leave`)
30

31
	const response = await fetch(url, {
32
		method: 'POST',
33
		headers: {
34
			Accept: 'application/json',
35
			'Xero-Tenant-Id': Xero_Tenant_Id,
36
			'Idempotency-Key': Idempotency_Key,
37
			'Content-Type': 'application/json',
38
			Authorization: 'Bearer ' + auth.token
39
		},
40
		body: JSON.stringify(body)
41
	})
42
	if (!response.ok) {
43
		const text = await response.text()
44
		throw new Error(`${response.status} ${text}`)
45
	}
46
	return await response.json()
47
}
48