Creates linked transactions (billable expenses)

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 linked transactions (billable expenses)
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	xero_tenant_id: string,
12
	Idempotency_Key: string,
13
	body: {
14
		SourceTransactionID?: string
15
		SourceLineItemID?: string
16
		ContactID?: string
17
		TargetTransactionID?: string
18
		TargetLineItemID?: string
19
		LinkedTransactionID?: string
20
		Status?: 'APPROVED' | 'DRAFT' | 'ONDRAFT' | 'BILLED' | 'VOIDED'
21
		Type?: 'BILLABLEEXPENSE'
22
		UpdatedDateUTC?: string
23
		SourceTransactionTypeCode?: 'ACCPAY' | 'SPEND'
24
		ValidationErrors?: { Message?: string }[]
25
	}
26
) {
27
	const url = new URL(`https://api.xero.com/api.xro/2.0/LinkedTransactions`)
28

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