1 | |
2 |
|
3 | |
4 | * Create Prospect |
5 | * Creates a new prospect in Outreach, optionally linked to an account. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Outreach, |
9 | first_name: string | undefined, |
10 | last_name: string | undefined, |
11 | emails: string[] | undefined, |
12 | title: string | undefined, |
13 | company: string | undefined, |
14 | account_id: number | undefined, |
15 | additional_attributes: { [key: string]: any } | undefined |
16 | ) { |
17 | const attributes: { [key: string]: any } = { ...additional_attributes } |
18 | if (first_name !== undefined && first_name !== "") { |
19 | attributes.firstName = first_name |
20 | } |
21 | if (last_name !== undefined && last_name !== "") { |
22 | attributes.lastName = last_name |
23 | } |
24 | if (emails !== undefined && emails.length > 0) { |
25 | attributes.emails = emails |
26 | } |
27 | if (title !== undefined && title !== "") { |
28 | attributes.title = title |
29 | } |
30 | if (company !== undefined && company !== "") { |
31 | attributes.company = company |
32 | } |
33 |
|
34 | const data: { [key: string]: any } = { type: "prospect", attributes } |
35 | if (account_id !== undefined) { |
36 | data.relationships = { |
37 | account: { data: { type: "account", id: account_id } }, |
38 | } |
39 | } |
40 |
|
41 | const response = await fetch("https://api.outreach.io/api/v2/prospects", { |
42 | method: "POST", |
43 | headers: { |
44 | Authorization: `Bearer ${auth.token}`, |
45 | "Content-Type": "application/vnd.api+json", |
46 | Accept: "application/vnd.api+json", |
47 | }, |
48 | body: JSON.stringify({ data }), |
49 | }) |
50 |
|
51 | if (!response.ok) { |
52 | throw new Error(`${response.status} ${await response.text()}`) |
53 | } |
54 |
|
55 | return await response.json() |
56 | } |
57 |
|