0

Update Moti

by
Published Nov 5, 2024

Updates a Moti.

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
	moti_id: string,
9
	identifier_type: 'id' | 'external_id' | undefined,
10
	body: {
11
		external_id?: string
12
		title?: string
13
		description?: string
14
		locked_order?: false | true
15
		folder_id?: number
16
		approver_id?: number
17
		approver_external_id?: number
18
		tags?: string[]
19
		category_ids?: string[]
20
		category_external_ids?: string[]
21
	}
22
) {
23
	const url = new URL(`https://motimateapp.com/public_api/motis/${moti_id}`)
24

25
	for (const [k, v] of [['identifier_type', identifier_type]]) {
26
		if (v !== undefined && v !== '' && k !== undefined) {
27
			url.searchParams.append(k, v)
28
		}
29
	}
30

31
	const response = await fetch(url, {
32
		method: 'PATCH',
33
		headers: {
34
			'Content-Type': 'application/json',
35
			Authorization: 'Bearer ' + auth.token
36
		},
37
		body: JSON.stringify(body)
38
	})
39

40
	if (!response.ok) {
41
		const text = await response.text()
42
		throw new Error(`${response.status} ${text}`)
43
	}
44

45
	return await response.json()
46
}
47