0
Get an academy's training analytics table by type
One script reply has been approved by the moderators Verified
Created by hugo697 16 days ago Viewed 0 times
0
Submitted by hugo697 Bun
Verified 16 days ago
1
//native
2
type Actimo = {
3
	apiKey: string
4
}
5

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

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

39
	const response = await fetch(url, {
40
		method: 'GET',
41
		headers: {
42
			'api-key': auth.apiKey
43
		},
44
		body: undefined
45
	})
46

47
	if (!response.ok) {
48
		const text = await response.text()
49
		throw new Error(`${response.status} ${text}`)
50
	}
51

52
	return await response.json()
53
}
54