Creates a new timesheet

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 a new timesheet
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	Xero_Tenant_Id: string,
12
	Idempotency_Key: string,
13
	body: {
14
		timesheetID?: string
15
		payrollCalendarID: string
16
		employeeID: string
17
		startDate: string
18
		endDate: string
19
		status?: 'Draft' | 'Approved' | 'Completed'
20
		totalHours?: number
21
		updatedDateUTC?: string
22
		timesheetLines?: {
23
			timesheetLineID?: string
24
			date: string
25
			earningsRateID: string
26
			trackingItemID?: string
27
			numberOfUnits: number
28
		}[]
29
	}
30
) {
31
	const url = new URL(`https://api.xero.com/payroll.xro/2.0/Timesheets`)
32

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