0
Creates new employees used in Xero payrun
One script reply has been approved by the moderators Verified
Created by hugo697 209 days ago Viewed 13444 times
0
Submitted by hugo697 Bun
Verified 209 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Creates new employees used in Xero payrun
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
		Employees?: {
16
			EmployeeID?: string
17
			Status?: 'ACTIVE' | 'ARCHIVED' | 'GDPRREQUEST' | 'DELETED'
18
			FirstName?: string
19
			LastName?: string
20
			ExternalLink?: {
21
				LinkType?: 'Facebook' | 'GooglePlus' | 'LinkedIn' | 'Twitter' | 'Website'
22
				Url?: string
23
				Description?: string
24
			}
25
			UpdatedDateUTC?: string
26
			StatusAttributeString?: string
27
			ValidationErrors?: { Message?: string }[]
28
		}[]
29
	}
30
) {
31
	const url = new URL(`https://api.xero.com/api.xro/2.0/Employees`)
32
	for (const [k, v] of [['summarizeErrors', summarizeErrors]]) {
33
		if (v !== undefined && v !== '' && k !== undefined) {
34
			url.searchParams.append(k, v)
35
		}
36
	}
37
	const response = await fetch(url, {
38
		method: 'PUT',
39
		headers: {
40
			Accept: 'application/json',
41
			'xero-tenant-id': xero_tenant_id,
42
			'Idempotency-Key': Idempotency_Key,
43
			'Content-Type': 'application/json',
44
			Authorization: 'Bearer ' + auth.token
45
		},
46
		body: JSON.stringify(body)
47
	})
48
	if (!response.ok) {
49
		const text = await response.text()
50
		throw new Error(`${response.status} ${text}`)
51
	}
52
	return await response.json()
53
}
54