1 | |
2 |
|
3 | |
4 | * Create Subtask |
5 | * Create a subtask under an existing task. Subtasks are not added to the parent's projects unless `projects` is set. Dates: `due_on`/`start_on` as YYYY-MM-DD, or `due_at` as an ISO 8601 date-time. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Asana, |
9 | task_gid: string, |
10 | name: string, |
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 | projects: string[] | undefined, |
18 | tags: string[] | undefined, |
19 | followers: string[] | undefined, |
20 | completed: boolean | undefined, |
21 | custom_fields: { [key: string]: any } | undefined |
22 | ) { |
23 | const data = { |
24 | name, |
25 | notes: notes || undefined, |
26 | html_notes: html_notes || undefined, |
27 | assignee: assignee || undefined, |
28 | due_on: due_on || undefined, |
29 | due_at: due_at || undefined, |
30 | start_on: start_on || undefined, |
31 | projects: projects?.length ? projects : undefined, |
32 | tags: tags?.length ? tags : undefined, |
33 | followers: followers?.length ? followers : undefined, |
34 | completed: completed || undefined, |
35 | custom_fields: |
36 | custom_fields && Object.keys(custom_fields).length |
37 | ? custom_fields |
38 | : undefined, |
39 | } |
40 |
|
41 | const response = await fetch( |
42 | `https://app.asana.com/api/1.0/tasks/${task_gid}/subtasks`, |
43 | { |
44 | method: "POST", |
45 | headers: { |
46 | Authorization: `Bearer ${auth.token}`, |
47 | "Content-Type": "application/json", |
48 | Accept: "application/json", |
49 | }, |
50 | body: JSON.stringify({ data }), |
51 | } |
52 | ) |
53 |
|
54 | if (!response.ok) { |
55 | throw new Error(`${response.status} ${await response.text()}`) |
56 | } |
57 |
|
58 | return await response.json() |
59 | } |
60 |
|