Search promotions
One script reply has been approved by the moderators Verified
Created by hugo697 445 days ago
Submitted by hugo697 Bun
Verified 445 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
	skip: string | undefined,
10
	limit: string | undefined,
11
	start: string | undefined,
12
	end: string | undefined,
13
	subtenantId: string | undefined,
14
	groupId: string | undefined,
15
	tagIds: string | undefined,
16
	topicIds: string | undefined,
17
	channels: string | undefined,
18
	promotionStatus:
19
		| 'ALL'
20
		| 'DRAFT'
21
		| 'PLANNED'
22
		| 'IN_PROGRESS'
23
		| 'STOPPED'
24
		| 'IN_REVIEW'
25
		| 'COMPLETED'
26
		| undefined,
27
	promotionType:
28
		| 'ALL'
29
		| 'TRAFFIC'
30
		| 'CONVERSIONS'
31
		| 'RETARGETING'
32
		| 'SHOPPING'
33
		| 'APP'
34
		| 'ENGAGEMENT'
35
		| 'VIDEO_VIEWS'
36
		| 'CALLS'
37
		| 'REACH'
38
		| 'FOLLOWERS'
39
		| 'PROFILE_VIEWS'
40
		| 'LEADS'
41
		| 'PERFORMANCE_MAX'
42
		| undefined,
43
	orderKey: string | undefined,
44
	reverseOrder: string | undefined,
45
	adhookToken: string
46
) {
47
	const url = new URL(`https://app.adhook.io/v1/promotions`)
48

49
	for (const [k, v] of [
50
		['filter', filter],
51
		['skip', skip],
52
		['limit', limit],
53
		['start', start],
54
		['end', end],
55
		['subtenantId', subtenantId],
56
		['groupId', groupId],
57
		['tagIds', tagIds],
58
		['topicIds', topicIds],
59
		['channels', channels],
60
		['promotionStatus', promotionStatus],
61
		['promotionType', promotionType],
62
		['orderKey', orderKey],
63
		['reverseOrder', reverseOrder]
64
	]) {
65
		if (v !== undefined && v !== '' && k !== undefined) {
66
			url.searchParams.append(k, v)
67
		}
68
	}
69

70
	const response = await fetch(url, {
71
		method: 'GET',
72
		headers: {
73
			adhookToken: adhookToken,
74
			Authorization: `Bearer ${auth.token}`
75
		},
76
		body: undefined
77
	})
78

79
	if (!response.ok) {
80
		const text = await response.text()
81
		throw new Error(`${response.status} ${text}`)
82
	}
83

84
	return await response.json()
85
}
86