Creates leave type 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 type 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
		leaveTypeID?: string
16
		scheduleOfAccrual?:
17
			| 'AnnuallyAfter6Months'
18
			| 'OnAnniversaryDate'
19
			| 'PercentageOfGrossEarnings'
20
			| 'NoAccruals'
21
		hoursAccruedAnnually?: number
22
		maximumToAccrue?: number
23
		openingBalance?: number
24
		rateAccruedHourly?: number
25
		percentageOfGrossEarnings?: number
26
		includeHolidayPayEveryPay?: false | true
27
		showAnnualLeaveInAdvance?: false | true
28
		annualLeaveTotalAmountPaid?: number
29
		scheduleOfAccrualDate?: string
30
	}
31
) {
32
	const url = new URL(`https://api.xero.com/payroll.xro/2.0/Employees/${EmployeeID}/LeaveTypes`)
33

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