1 | |
2 |
|
3 | |
4 | * Update Ticket |
5 | * Update a ticket and optionally add a comment to it (public reply, or an internal note when `internal_note` is on). Only the fields you fill are changed. Attach files by passing the tokens returned by Upload Ticket Attachments (needs a comment). Tags are handled by the Add/Set/Remove Ticket Tags actions; set any other field through `fields`. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Zendesk, |
9 | ticket_id: number, |
10 | comment: string | undefined, |
11 | comment_is_html: boolean, |
12 | internal_note: boolean, |
13 | upload_tokens: string[] | undefined, |
14 | subject: string | undefined, |
15 | status: "new" | "open" | "pending" | "hold" | "solved" | "closed" | undefined, |
16 | priority: "urgent" | "high" | "normal" | "low" | undefined, |
17 | type: "problem" | "incident" | "question" | "task" | undefined, |
18 | assignee_id: number | undefined, |
19 | assignee_email: string | undefined, |
20 | group_id: number | undefined, |
21 | custom_fields: { id: number; value: any }[] | undefined, |
22 | fields: { [key: string]: any } | undefined |
23 | ) { |
24 | const ticket: { [key: string]: any } = { |
25 | ...fields, |
26 | subject: subject || undefined, |
27 | status: status || undefined, |
28 | priority: priority || undefined, |
29 | type: type || undefined, |
30 | assignee_id: assignee_id ?? undefined, |
31 | assignee_email: assignee_email || undefined, |
32 | group_id: group_id ?? undefined, |
33 | custom_fields: custom_fields?.length ? custom_fields : undefined, |
34 | } |
35 | if (comment) { |
36 | ticket.comment = { |
37 | ...(comment_is_html ? { html_body: comment } : { body: comment }), |
38 | public: !internal_note, |
39 | uploads: upload_tokens?.length ? upload_tokens : undefined, |
40 | } |
41 | } |
42 |
|
43 | const response = await fetch( |
44 | `https://${auth.subdomain}.zendesk.com/api/v2/tickets/${ticket_id}`, |
45 | { |
46 | method: "PUT", |
47 | headers: { |
48 | Authorization: auth.token |
49 | ? `Bearer ${auth.token}` |
50 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
51 | "Content-Type": "application/json", |
52 | Accept: "application/json", |
53 | }, |
54 | body: JSON.stringify({ ticket }), |
55 | } |
56 | ) |
57 |
|
58 | if (!response.ok) { |
59 | throw new Error(`${response.status} ${await response.text()}`) |
60 | } |
61 |
|
62 | return await response.json() |
63 | } |
64 |
|