Updates a specific item

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 specific item
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	ItemID: string,
12
	unitdp: string | undefined,
13
	xero_tenant_id: string,
14
	Idempotency_Key: string,
15
	body: {
16
		Items?: {
17
			Code: string
18
			InventoryAssetAccountCode?: string
19
			Name?: string
20
			IsSold?: false | true
21
			IsPurchased?: false | true
22
			Description?: string
23
			PurchaseDescription?: string
24
			PurchaseDetails?: {
25
				UnitPrice?: number
26
				AccountCode?: string
27
				COGSAccountCode?: string
28
				TaxType?: string
29
			}
30
			SalesDetails?: {
31
				UnitPrice?: number
32
				AccountCode?: string
33
				COGSAccountCode?: string
34
				TaxType?: string
35
			}
36
			IsTrackedAsInventory?: false | true
37
			TotalCostPool?: number
38
			QuantityOnHand?: number
39
			UpdatedDateUTC?: string
40
			ItemID?: string
41
			StatusAttributeString?: string
42
			ValidationErrors?: { Message?: string }[]
43
		}[]
44
	}
45
) {
46
	const url = new URL(`https://api.xero.com/api.xro/2.0/Items/${ItemID}`)
47
	for (const [k, v] of [['unitdp', unitdp]]) {
48
		if (v !== undefined && v !== '' && k !== undefined) {
49
			url.searchParams.append(k, v)
50
		}
51
	}
52
	const response = await fetch(url, {
53
		method: 'POST',
54
		headers: {
55
			Accept: 'application/json',
56
			'xero-tenant-id': xero_tenant_id,
57
			'Idempotency-Key': Idempotency_Key,
58
			'Content-Type': 'application/json',
59
			Authorization: 'Bearer ' + auth.token
60
		},
61
		body: JSON.stringify(body)
62
	})
63
	if (!response.ok) {
64
		const text = await response.text()
65
		throw new Error(`${response.status} ${text}`)
66
	}
67
	return await response.json()
68
}
69