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