0
Get promotions excel export
One script reply has been approved by the moderators Verified
Created by hugo697 14 days ago Viewed 1203 times
0
Submitted by hugo697 Bun
Verified 14 days ago
1
//native
2
type Adhook = {
3
	token: string
4
}
5

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

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

67
	const response = await fetch(url, {
68
		method: 'GET',
69
		headers: {
70
			Authorization: `Bearer ${auth.token}`
71
		},
72
		body: undefined
73
	})
74

75
	if (!response.ok) {
76
		const text = await response.text()
77
		throw new Error(`${response.status} ${text}`)
78
	}
79

80
	return await response.text()
81
}
82