1 | |
2 |
|
3 | |
4 | * Update User |
5 | * Update a user by id; only the fields you fill are changed. `tags` replaces the user's tags. Set any other user field through `fields`, e.g. `{ "user_fields": { "plan": "gold" }, "suspended": true }`. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Zendesk, |
9 | user_id: number, |
10 | name: string | undefined, |
11 | email: string | undefined, |
12 | external_id: string | undefined, |
13 | role: "end-user" | "agent" | "admin" | undefined, |
14 | phone: string | undefined, |
15 | organization_id: number | undefined, |
16 | tags: string[] | undefined, |
17 | fields: { [key: string]: any } | undefined |
18 | ) { |
19 | const user = { |
20 | ...fields, |
21 | name: name || undefined, |
22 | email: email || undefined, |
23 | external_id: external_id || undefined, |
24 | role: role || undefined, |
25 | phone: phone || undefined, |
26 | organization_id: organization_id ?? undefined, |
27 | tags: tags?.length ? tags : undefined, |
28 | } |
29 |
|
30 | const response = await fetch( |
31 | `https://${auth.subdomain}.zendesk.com/api/v2/users/${user_id}`, |
32 | { |
33 | method: "PUT", |
34 | headers: { |
35 | Authorization: auth.token |
36 | ? `Bearer ${auth.token}` |
37 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
38 | "Content-Type": "application/json", |
39 | Accept: "application/json", |
40 | }, |
41 | body: JSON.stringify({ user }), |
42 | } |
43 | ) |
44 |
|
45 | if (!response.ok) { |
46 | throw new Error(`${response.status} ${await response.text()}`) |
47 | } |
48 |
|
49 | return await response.json() |
50 | } |
51 |
|