1 | |
2 |
|
3 | export type DynSelect_sequence = string |
4 |
|
5 | |
6 | export async function sequence(auth: RT.Outreach) { |
7 | const url = new URL("https://api.outreach.io/api/v2/sequences") |
8 | url.searchParams.append("sort", "name") |
9 | url.searchParams.append("page[size]", "100") |
10 | const response = await fetch(url, { |
11 | headers: { |
12 | Authorization: `Bearer ${auth.token}`, |
13 | Accept: "application/vnd.api+json", |
14 | }, |
15 | }) |
16 | const { data } = (await response.json()) as { |
17 | data: { id: number; attributes: { name: string } }[] |
18 | } |
19 | return data.map((s) => ({ value: String(s.id), label: s.attributes.name })) |
20 | } |
21 |
|
22 | export type DynSelect_mailbox = string |
23 |
|
24 | |
25 | export async function mailbox(auth: RT.Outreach) { |
26 | const url = new URL("https://api.outreach.io/api/v2/mailboxes") |
27 | url.searchParams.append("page[size]", "100") |
28 | const response = await fetch(url, { |
29 | headers: { |
30 | Authorization: `Bearer ${auth.token}`, |
31 | Accept: "application/vnd.api+json", |
32 | }, |
33 | }) |
34 | const { data } = (await response.json()) as { |
35 | data: { id: number; attributes: { email: string } }[] |
36 | } |
37 | return data.map((m) => ({ value: String(m.id), label: m.attributes.email })) |
38 | } |
39 |
|
40 | |
41 | * Add Prospect to Sequence |
42 | * Adds an existing prospect to a sequence, sending from the selected mailbox. |
43 | */ |
44 | export async function main( |
45 | auth: RT.Outreach, |
46 | prospect_id: number, |
47 | sequence: DynSelect_sequence, |
48 | mailbox: DynSelect_mailbox |
49 | ) { |
50 | const response = await fetch( |
51 | "https://api.outreach.io/api/v2/sequenceStates", |
52 | { |
53 | method: "POST", |
54 | headers: { |
55 | Authorization: `Bearer ${auth.token}`, |
56 | "Content-Type": "application/vnd.api+json", |
57 | Accept: "application/vnd.api+json", |
58 | }, |
59 | body: JSON.stringify({ |
60 | data: { |
61 | type: "sequenceState", |
62 | relationships: { |
63 | prospect: { data: { type: "prospect", id: prospect_id } }, |
64 | sequence: { data: { type: "sequence", id: Number(sequence) } }, |
65 | mailbox: { data: { type: "mailbox", id: Number(mailbox) } }, |
66 | }, |
67 | }, |
68 | }), |
69 | } |
70 | ) |
71 |
|
72 | if (!response.ok) { |
73 | throw new Error(`${response.status} ${await response.text()}`) |
74 | } |
75 |
|
76 | return await response.json() |
77 | } |
78 |
|