Updates a timesheet

Update properties on a single 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
 * Updates a timesheet
7
 * Update properties on a single timesheet
8
 */
9
export async function main(
10
	auth: Xero,
11
	TimesheetID: string,
12
	Xero_Tenant_Id: string,
13
	Idempotency_Key: string,
14
	body: {
15
		EmployeeID: string
16
		StartDate: string
17
		EndDate: string
18
		Status?: 'DRAFT' | 'PROCESSED' | 'APPROVED' | 'REJECTED' | 'REQUESTED'
19
		Hours?: number
20
		TimesheetID?: string
21
		TimesheetLines?: {
22
			EarningsRateID?: string
23
			TrackingItemID?: string
24
			NumberOfUnits?: number[]
25
			UpdatedDateUTC?: string
26
		}[]
27
		UpdatedDateUTC?: string
28
		ValidationErrors?: { Message?: string }[]
29
	}[]
30
) {
31
	const url = new URL(`https://api.xero.com/payroll.xro/1.0/Timesheets/${TimesheetID}`)
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