Create Batch Job for Trigger Categories

Creates a job that performs a batch operation for the given trigger categories.

Script zendesk Verified

by hugo697 ยท 11/7/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 377 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * Create Batch Job for Trigger Categories
8
 * Creates a job that performs a batch operation for the given trigger categories.
9
 */
10
export async function main(
11
  auth: Zendesk,
12
  body: {
13
    job?: {
14
      action?: string;
15
      items?: {
16
        trigger_categories?: {
17
          id?: string;
18
          position?: string;
19
          [k: string]: unknown;
20
        }[];
21
        triggers?: {
22
          active?: string;
23
          category_id?: string;
24
          id?: string;
25
          position?: string;
26
          [k: string]: unknown;
27
        }[];
28
        [k: string]: unknown;
29
      };
30
      [k: string]: unknown;
31
    };
32
    [k: string]: unknown;
33
  }
34
) {
35
  const url = new URL(
36
    `https://${auth.subdomain}.zendesk.com/api/v2/trigger_categories/jobs`
37
  );
38

39
  const response = await fetch(url, {
40
    method: "POST",
41
    headers: {
42
      "Content-Type": "application/json",
43
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
44
    },
45
    body: JSON.stringify(body),
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53