Retrieves all time entries associated with a specific project

Allows you to retrieve the time entries associated with a specific project

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 all time entries associated with a specific project
7
 * Allows you to retrieve the time entries associated with a specific project
8
 */
9
export async function main(
10
	auth: Xero,
11
	projectId: string,
12
	userId: string | undefined,
13
	taskId: string | undefined,
14
	invoiceId: string | undefined,
15
	contactId: string | undefined,
16
	page: string | undefined,
17
	pageSize: string | undefined,
18
	states: string | undefined,
19
	isChargeable: string | undefined,
20
	dateAfterUtc: string | undefined,
21
	dateBeforeUtc: string | undefined,
22
	Xero_Tenant_Id: string
23
) {
24
	const url = new URL(`https://api.xero.com/projects.xro/2.0/Projects/${projectId}/Time`)
25
	for (const [k, v] of [
26
		['userId', userId],
27
		['taskId', taskId],
28
		['invoiceId', invoiceId],
29
		['contactId', contactId],
30
		['page', page],
31
		['pageSize', pageSize],
32
		['states', states],
33
		['isChargeable', isChargeable],
34
		['dateAfterUtc', dateAfterUtc],
35
		['dateBeforeUtc', dateBeforeUtc]
36
	]) {
37
		if (v !== undefined && v !== '' && k !== undefined) {
38
			url.searchParams.append(k, v)
39
		}
40
	}
41
	const response = await fetch(url, {
42
		method: 'GET',
43
		headers: {
44
			Accept: 'application/json',
45
			'Xero-Tenant-Id': Xero_Tenant_Id,
46
			Authorization: 'Bearer ' + auth.token
47
		},
48
		body: undefined
49
	})
50
	if (!response.ok) {
51
		const text = await response.text()
52
		throw new Error(`${response.status} ${text}`)
53
	}
54
	return await response.json()
55
}
56