Count promotions

Script adhook Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 519 days ago
1
//native
2
type Adhook = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Adhook,
8
	filter: string | undefined,
9
	subtenantId: string | undefined,
10
	groupId: string | undefined,
11
	tagIds: string | undefined,
12
	topicIds: string | undefined,
13
	channels: string | undefined,
14
	promotionStatus:
15
		| 'ALL'
16
		| 'DRAFT'
17
		| 'PLANNED'
18
		| 'IN_PROGRESS'
19
		| 'STOPPED'
20
		| 'IN_REVIEW'
21
		| 'COMPLETED'
22
		| undefined,
23
	promotionType:
24
		| 'ALL'
25
		| 'TRAFFIC'
26
		| 'CONVERSIONS'
27
		| 'RETARGETING'
28
		| 'SHOPPING'
29
		| 'APP'
30
		| 'ENGAGEMENT'
31
		| 'VIDEO_VIEWS'
32
		| 'CALLS'
33
		| 'REACH'
34
		| 'FOLLOWERS'
35
		| 'PROFILE_VIEWS'
36
		| 'LEADS'
37
		| 'PERFORMANCE_MAX'
38
		| undefined,
39
	adhookToken: string
40
) {
41
	const url = new URL(`https://app.adhook.io/v1/promotions/count`)
42

43
	for (const [k, v] of [
44
		['filter', filter],
45
		['subtenantId', subtenantId],
46
		['groupId', groupId],
47
		['tagIds', tagIds],
48
		['topicIds', topicIds],
49
		['channels', channels],
50
		['promotionStatus', promotionStatus],
51
		['promotionType', promotionType]
52
	]) {
53
		if (v !== undefined && v !== '' && k !== undefined) {
54
			url.searchParams.append(k, v)
55
		}
56
	}
57

58
	const response = await fetch(url, {
59
		method: 'GET',
60
		headers: {
61
			adhookToken: adhookToken,
62
			Authorization: `Bearer ${auth.token}`
63
		},
64
		body: undefined
65
	})
66

67
	if (!response.ok) {
68
		const text = await response.text()
69
		throw new Error(`${response.status} ${text}`)
70
	}
71

72
	return await response.json()
73
}
74