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

Creates a prospect. See docs here

Created by hugo697 198 days ago Viewed 3214 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
		title: string
11
		affiliation_id: number
12
		type_id: number
13
		status_id?: number
14
		comments?: string
15
		value?: number
16
		success?: 'yes' | 'no'
17
		staff_id?: number
18
		due_date?: string
19
		weighting?: number
20
		progress?: number
21
		probability_id?: number
22
	}
23
) {
24
	// Fetch the access token
25
	const accessTokenResponse = (await (
26
		await fetch(`https://${resource.deployment}.api.accelo.com/oauth2/v0/token`, {
27
			method: 'POST',
28
			headers: {
29
				'Content-Type': 'application/x-www-form-urlencoded',
30
				Authorization: `Basic ${Buffer.from(
31
					`${resource.clientId}:${resource.clientSecret}`
32
				).toString('base64')}`
33
			},
34
			body: new URLSearchParams({
35
				grant_type: 'client_credentials',
36
				scope: 'write(all)'
37
			})
38
		})
39
	).json()) as any
40
	const accessToken = accessTokenResponse.access_token
41

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

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