Updates or creates a single manual journal

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 or creates a single manual journal
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	summarizeErrors: string | undefined,
12
	xero_tenant_id: string,
13
	Idempotency_Key: string,
14
	body: {
15
		pagination?: {
16
			page?: number
17
			pageSize?: number
18
			pageCount?: number
19
			itemCount?: number
20
		}
21
		Warnings?: { Message?: string }[]
22
		ManualJournals?: {
23
			Narration: string
24
			JournalLines?: {
25
				LineAmount?: number
26
				AccountCode?: string
27
				AccountID?: string
28
				Description?: string
29
				TaxType?: string
30
				Tracking?: {
31
					TrackingCategoryID?: string
32
					TrackingOptionID?: string
33
					Name?: string
34
					Option?: string
35
					Status?: 'ACTIVE' | 'ARCHIVED' | 'DELETED'
36
					Options?: {
37
						TrackingOptionID?: string
38
						Name?: string
39
						Status?: 'ACTIVE' | 'ARCHIVED' | 'DELETED'
40
						TrackingCategoryID?: string
41
					}[]
42
				}[]
43
				TaxAmount?: number
44
				IsBlank?: false | true
45
			}[]
46
			Date?: string
47
			LineAmountTypes?: 'Exclusive' | 'Inclusive' | 'NoTax'
48
			Status?: 'ARCHIVED' | 'DELETED' | 'DRAFT' | 'POSTED' | 'VOIDED'
49
			Url?: string
50
			ShowOnCashBasisReports?: false | true
51
			HasAttachments?: never
52
			UpdatedDateUTC?: string
53
			ManualJournalID?: string
54
			StatusAttributeString?: string
55
			Warnings?: { Message?: string }[]
56
			ValidationErrors?: { Message?: string }[]
57
			Attachments?: {
58
				AttachmentID?: string
59
				FileName?: string
60
				Url?: string
61
				MimeType?: string
62
				ContentLength?: number
63
				IncludeOnline?: false | true
64
			}[]
65
		}[]
66
	}
67
) {
68
	const url = new URL(`https://api.xero.com/api.xro/2.0/ManualJournals`)
69
	for (const [k, v] of [['summarizeErrors', summarizeErrors]]) {
70
		if (v !== undefined && v !== '' && k !== undefined) {
71
			url.searchParams.append(k, v)
72
		}
73
	}
74
	const response = await fetch(url, {
75
		method: 'POST',
76
		headers: {
77
			Accept: 'application/json',
78
			'xero-tenant-id': xero_tenant_id,
79
			'Idempotency-Key': Idempotency_Key,
80
			'Content-Type': 'application/json',
81
			Authorization: 'Bearer ' + auth.token
82
		},
83
		body: JSON.stringify(body)
84
	})
85
	if (!response.ok) {
86
		const text = await response.text()
87
		throw new Error(`${response.status} ${text}`)
88
	}
89
	return await response.json()
90
}
91