Creates employee leave type records

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 employee leave type records
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
		leaveTypeID: string
16
		scheduleOfAccrual:
17
			| 'BeginningOfCalendarYear'
18
			| 'OnAnniversaryDate'
19
			| 'EachPayPeriod'
20
			| 'OnHourWorked'
21
		hoursAccruedAnnually?: number
22
		maximumToAccrue?: number
23
		openingBalance?: number
24
		rateAccruedHourly?: number
25
		scheduleOfAccrualDate?: string
26
	}
27
) {
28
	const url = new URL(`https://api.xero.com/payroll.xro/2.0/Employees/${EmployeeID}/LeaveTypes`)
29

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