0
Create user
One script reply has been approved by the moderators Verified

Creates a user in Zammad. This is most commonly a customer of yours.
Also succeeds, if the user already exists.

Created by jaller94 600 days ago Viewed 5030 times
0
Submitted by jaller94 Deno
Verified 600 days ago
1
export async function main(
2
  zammad_host: string,
3
  zammad_token: string,
4
  user_agent?: string,
5
  firstname: string,
6
  lastname: string,
7
  email: string,
8
  login?: string,
9
  organisation?: string,
10
  roles?: string[],
11
) {
12
  // https://docs.zammad.org/en/latest/api/user.html#create
13
  const resp = await fetch(`${zammad_host}/api/v1/users`, {
14
    method: "POST",
15
    headers: {
16
      Authorization: `Bearer ${zammad_token}`,
17
      "User-Agent": user_agent ?? "Public windmill.dev script",
18
    },
19
    body: JSON.stringify({
20
      firstname,
21
      lastname,
22
      email,
23
      ...(login && { login }),
24
      ...(organisation && { organisation }),
25
      ...(roles && { roles }),
26
    }),
27
  });
28
  // HTTP 422 means that the user already exists.
29
  if (!resp.ok && resp.status !== 422) {
30
    throw Error(`Failed to create user: Error HTTP${resp.status}`);
31
  }
32
  return await resp.json();
33
}
34