0
Update Contact
One script reply has been approved by the moderators Verified

Updates a contact. See docs here

Created by hugo697 198 days ago Viewed 3255 times
0
Submitted by hugo697 Bun
Verified 198 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
		contact_id: number
11
		firstname?: string
12
		surname?: string
13
		middlename?: string
14
		username?: string
15
		password?: string
16
		title?: string
17
		comments?: string
18
		status?: number
19
		standing?: string
20
	}
21
) {
22
	// Fetch the access token
23
	const accessTokenResponse = (await (
24
		await fetch(`https://${resource.deployment}.api.accelo.com/oauth2/v0/token`, {
25
			method: 'POST',
26
			headers: {
27
				'Content-Type': 'application/x-www-form-urlencoded',
28
				Authorization: `Basic ${Buffer.from(
29
					`${resource.clientId}:${resource.clientSecret}`
30
				).toString('base64')}`
31
			},
32
			body: new URLSearchParams({
33
				grant_type: 'client_credentials',
34
				scope: 'write(all)'
35
			})
36
		})
37
	).json()) as any
38
	const accessToken = accessTokenResponse.access_token
39

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

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