Edits history of script submission #23837 for ' Create or Update User (zendesk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create or Update User
     * 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" }`.
     */
    export async function main(
      auth: RT.Zendesk,
      name: string | undefined,
      email: string | undefined,
      external_id: string | undefined,
      role: "end-user" | "agent" | "admin" | undefined,
      phone: string | undefined,
      organization_id: number | undefined,
      tags: string[] | undefined,
      fields: { [key: string]: any } | undefined
    ) {
      const user = {
        ...fields,
        name: name || undefined,
        email: email || undefined,
        external_id: external_id || undefined,
        role: role || undefined,
        phone: phone || undefined,
        organization_id: organization_id ?? undefined,
        tags: tags?.length ? tags : undefined,
      }
    
      const response = await fetch(
        `https://${auth.subdomain}.zendesk.com/api/v2/users/create_or_update`,
        {
          method: "POST",
          headers: {
            Authorization: auth.token
              ? `Bearer ${auth.token}`
              : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ user }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago