1 | |
2 |
|
3 | |
4 | * Create Task |
5 | * Creates a manual task, optionally tied to a prospect and assigned to an owner. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Outreach, |
9 | action: "action_item" | "call" | "email" | "in_person", |
10 | due_at: string, |
11 | note: string | undefined, |
12 | prospect_id: number | undefined, |
13 | owner_id: number | undefined |
14 | ) { |
15 | const attributes: { [key: string]: any } = { action, dueAt: due_at } |
16 | if (note !== undefined && note !== "") { |
17 | attributes.note = note |
18 | } |
19 |
|
20 | const relationships: { [key: string]: any } = {} |
21 | if (prospect_id !== undefined) { |
22 | |
23 | relationships.subject = { data: { type: "prospect", id: prospect_id } } |
24 | } |
25 | if (owner_id !== undefined) { |
26 | relationships.owner = { data: { type: "user", id: owner_id } } |
27 | } |
28 |
|
29 | const response = await fetch("https://api.outreach.io/api/v2/tasks", { |
30 | method: "POST", |
31 | headers: { |
32 | Authorization: `Bearer ${auth.token}`, |
33 | "Content-Type": "application/vnd.api+json", |
34 | Accept: "application/vnd.api+json", |
35 | }, |
36 | body: JSON.stringify({ data: { type: "task", attributes, relationships } }), |
37 | }) |
38 |
|
39 | if (!response.ok) { |
40 | throw new Error(`${response.status} ${await response.text()}`) |
41 | } |
42 |
|
43 | return await response.json() |
44 | } |
45 |
|