1 | |
2 |
|
3 | |
4 | * Update Task |
5 | * Update a task. Only the fields you set are changed. `completed` defaults to `keep`; use `clear_assignee` or `clear_dates` to unassign the task or remove its start and due dates. `custom_fields` maps custom field gids to values (enum fields take the enum option gid, null clears a field). |
6 | */ |
7 | export async function main( |
8 | auth: RT.Asana, |
9 | task_gid: string, |
10 | name: string | undefined, |
11 | notes: string | undefined, |
12 | html_notes: string | undefined, |
13 | assignee: string | undefined, |
14 | due_on: string | undefined, |
15 | due_at: string | undefined, |
16 | start_on: string | undefined, |
17 | completed: "keep" | "complete" | "incomplete" = "keep", |
18 | clear_assignee: boolean | undefined, |
19 | clear_dates: boolean | undefined, |
20 | custom_fields: { [key: string]: any } | undefined |
21 | ) { |
22 | const data: { [key: string]: any } = { |
23 | name: name || undefined, |
24 | notes: notes || undefined, |
25 | html_notes: html_notes || undefined, |
26 | assignee: assignee || undefined, |
27 | due_on: due_on || undefined, |
28 | due_at: due_at || undefined, |
29 | start_on: start_on || undefined, |
30 | custom_fields: |
31 | custom_fields && Object.keys(custom_fields).length |
32 | ? custom_fields |
33 | : undefined, |
34 | } |
35 | if (completed === "complete") data.completed = true |
36 | if (completed === "incomplete") data.completed = false |
37 | if (clear_assignee) data.assignee = null |
38 | if (clear_dates) { |
39 | data.due_on = null |
40 | data.due_at = null |
41 | data.start_on = null |
42 | } |
43 |
|
44 | const response = await fetch( |
45 | `https://app.asana.com/api/1.0/tasks/${task_gid}`, |
46 | { |
47 | method: "PUT", |
48 | headers: { |
49 | Authorization: `Bearer ${auth.token}`, |
50 | "Content-Type": "application/json", |
51 | Accept: "application/json", |
52 | }, |
53 | body: JSON.stringify({ data }), |
54 | } |
55 | ) |
56 |
|
57 | if (!response.ok) { |
58 | throw new Error(`${response.status} ${await response.text()}`) |
59 | } |
60 |
|
61 | return await response.json() |
62 | } |
63 |
|