0

Update Parent Group

by
Published Nov 5, 2024

Updates a Group's Parent.

Script motimate Verified

The script

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

6
export async function main(
7
	auth: Motimate,
8
	group_id: string,
9
	identifier_type: 'id' | 'external_id' | undefined,
10
	body: {
11
		parent_id?: number
12
		parent_external_id?: number
13
		update_tree_structure?: false | true
14
	}
15
) {
16
	const url = new URL(`https://motimateapp.com/public_api/groups/${group_id}/parent`)
17

18
	for (const [k, v] of [['identifier_type', identifier_type]]) {
19
		if (v !== undefined && v !== '' && k !== undefined) {
20
			url.searchParams.append(k, v)
21
		}
22
	}
23

24
	const response = await fetch(url, {
25
		method: 'PATCH',
26
		headers: {
27
			'Content-Type': 'application/json',
28
			Authorization: 'Bearer ' + auth.token
29
		},
30
		body: JSON.stringify(body)
31
	})
32

33
	if (!response.ok) {
34
		const text = await response.text()
35
		throw new Error(`${response.status} ${text}`)
36
	}
37

38
	return await response.text()
39
}
40