0

Update User

by
Published Nov 5, 2024

Updates a User.

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
	user_id: string,
9
	identifier_type: 'id' | 'external_id' | undefined,
10
	body: {
11
		external_id?: string
12
		email?: string
13
		first_name?: string
14
		last_name?: string
15
		phone_number?: string
16
		imported?: false | true
17
		organization_role?:
18
			| 'chief_editor'
19
			| 'normal_user'
20
			| 'organization_admin'
21
			| 'pulse_editor'
22
			| 'training_editor'
23
		preferred_language?:
24
			| 'cs'
25
			| 'da'
26
			| 'de'
27
			| 'en'
28
			| 'es'
29
			| 'fi'
30
			| 'fr'
31
			| 'hr'
32
			| 'hu'
33
			| 'it'
34
			| 'lt'
35
			| 'lv'
36
			| 'nb'
37
			| 'nl'
38
			| 'nn'
39
			| 'pl'
40
			| 'pt'
41
			| 'ro'
42
			| 'ru'
43
			| 'se'
44
			| 'sq'
45
			| 'sr'
46
			| 'sv'
47
			| 'tr'
48
		status?: 'active' | 'inactive' | 'manually_activated' | 'manually_deactivated'
49
	}
50
) {
51
	const url = new URL(`https://motimateapp.com/public_api/users/${user_id}`)
52

53
	for (const [k, v] of [['identifier_type', identifier_type]]) {
54
		if (v !== undefined && v !== '' && k !== undefined) {
55
			url.searchParams.append(k, v)
56
		}
57
	}
58

59
	const response = await fetch(url, {
60
		method: 'PATCH',
61
		headers: {
62
			'Content-Type': 'application/json',
63
			Authorization: 'Bearer ' + auth.token
64
		},
65
		body: JSON.stringify(body)
66
	})
67

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

73
	return await response.json()
74
}
75