Edits history of script submission #22591 for ' Create user (zammad)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export async function main(
      zammad_host: string,
      zammad_token: string,
      user_agent?: string,
      firstname: string,
      lastname: string,
      email: string,
      login?: string,
      organisation?: string,
      roles?: string[],
    ) {
      // https://docs.zammad.org/en/latest/api/user.html#create
      const resp = await fetch(`${zammad_host}/api/v1/users`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${zammad_token}`,
          "User-Agent": user_agent ?? "Public windmill.dev script",
        },
        body: JSON.stringify({
          firstname,
          lastname,
          email,
          ...(login && { login }),
          ...(organisation && { organisation }),
          ...(roles && { roles }),
        }),
      });
      // HTTP 422 means that the user already exists.
      if (!resp.ok && resp.status !== 422) {
        throw Error(`Failed to create user: Error HTTP${resp.status}`);
      }
      return await resp.json();
    }
    

    Submitted by hugo989 6 days ago