Retrieves a specific employee's leave balances using a unique employee ID

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
 * Retrieves a specific employee's leave balances using a unique employee ID
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	EmployeeID: string,
12
	LeaveType: string | undefined,
13
	AsOfDate: string | undefined,
14
	Xero_Tenant_Id: string
15
) {
16
	const url = new URL(
17
		`https://api.xero.com/payroll.xro/2.0/Employees/${EmployeeID}/StatutoryLeaveBalance`
18
	)
19
	for (const [k, v] of [
20
		['LeaveType', LeaveType],
21
		['AsOfDate', AsOfDate]
22
	]) {
23
		if (v !== undefined && v !== '' && k !== undefined) {
24
			url.searchParams.append(k, v)
25
		}
26
	}
27
	const response = await fetch(url, {
28
		method: 'GET',
29
		headers: {
30
			Accept: 'application/json',
31
			'Xero-Tenant-Id': Xero_Tenant_Id,
32
			Authorization: 'Bearer ' + auth.token
33
		},
34
		body: undefined
35
	})
36
	if (!response.ok) {
37
		const text = await response.text()
38
		throw new Error(`${response.status} ${text}`)
39
	}
40
	return await response.json()
41
}
42