Get academy list analytics tables by type

Script actimo Verified

by hugo697 ยท 11/5/2024

The script

Submitted by hugo697 Bun
Verified 572 days ago
1
//native
2
type Actimo = {
3
	apiKey: string
4
}
5

6
export async function main(
7
	auth: Actimo,
8
	type: 'recipients' | 'groups' | 'departments' | 'categories',
9
	category: string | undefined,
10
	department: string | undefined,
11
	direction: string | undefined,
12
	group: string | undefined,
13
	limit: string | undefined,
14
	offset: string | undefined,
15
	order: string | undefined,
16
	search: string | undefined
17
) {
18
	const url = new URL(`https://actimo.com/api/v1/academy/analytics/tables/${type}`)
19

20
	for (const [k, v] of [
21
		['category', category],
22
		['department', department],
23
		['direction', direction],
24
		['group', group],
25
		['limit', limit],
26
		['offset', offset],
27
		['order', order],
28
		['search', search]
29
	]) {
30
		if (v !== undefined && v !== '' && k !== undefined) {
31
			url.searchParams.append(k, v)
32
		}
33
	}
34

35
	const response = await fetch(url, {
36
		method: 'GET',
37
		headers: {
38
			'api-key': auth.apiKey
39
		},
40
		body: undefined
41
	})
42

43
	if (!response.ok) {
44
		const text = await response.text()
45
		throw new Error(`${response.status} ${text}`)
46
	}
47

48
	return await response.json()
49
}
50