0
Create Company
One script reply has been approved by the moderators Verified

Creates a company. See docs here

Created by hugo697 124 days ago Viewed 3174 times
0
Submitted by hugo697 Bun
Verified 124 days ago
1
type Accelo = {
2
	clientId: string
3
	clientSecret: string
4
	deployment: string
5
}
6

7
export async function main(
8
	resource: Accelo,
9
	data: {
10
		name: string
11
		parent_id?: number
12
		status_id?: number
13
		custom_id?: string
14
		website?: string
15
		phone?: string
16
		fax?: string
17
		comments?: string
18
		standing?: string
19
	}
20
) {
21
	// Fetch the access token
22
	const accessTokenResponse = (await (
23
		await fetch(`https://${resource.deployment}.api.accelo.com/oauth2/v0/token`, {
24
			method: 'POST',
25
			headers: {
26
				'Content-Type': 'application/x-www-form-urlencoded',
27
				Authorization: `Basic ${Buffer.from(
28
					`${resource.clientId}:${resource.clientSecret}`
29
				).toString('base64')}`
30
			},
31
			body: new URLSearchParams({
32
				grant_type: 'client_credentials',
33
				scope: 'write(all)'
34
			})
35
		})
36
	).json()) as any
37
	const accessToken = accessTokenResponse.access_token
38

39
	const form = new URLSearchParams()
40
	Object.entries(data).forEach(([key, value]) => value && form.append(key, value + ''))
41

42
	return (
43
		await fetch(`https://${resource.deployment}.api.accelo.com/api/v0/companies`, {
44
			method: 'POST',
45
			headers: {
46
				'Content-Type': 'application/x-www-form-urlencoded',
47
				Authorization: `Bearer ${accessToken}`
48
			},
49
			body: form
50
		})
51
	).json()
52
}
53