1 | |
2 |
|
3 | export type DynSelect_object_type = string |
4 |
|
5 | |
6 | export async function object_type(auth: RT.Hubspot) { |
7 | const standard = [ |
8 | "contacts", |
9 | "companies", |
10 | "deals", |
11 | "tickets", |
12 | "line_items", |
13 | "products", |
14 | "quotes", |
15 | "leads", |
16 | "notes", |
17 | "tasks", |
18 | "meetings", |
19 | "calls", |
20 | "emails", |
21 | "communications", |
22 | ].map((o) => ({ value: o, label: o })) |
23 | const response = await fetch("https://api.hubapi.com/crm/v3/schemas", { |
24 | headers: { |
25 | Authorization: `Bearer ${auth.token}`, |
26 | Accept: "application/json", |
27 | }, |
28 | }) |
29 | |
30 | if (!response.ok) return standard |
31 | const { results } = (await response.json()) as { |
32 | results: { fullyQualifiedName: string; labels: { plural: string } }[] |
33 | } |
34 | return [ |
35 | ...standard, |
36 | ...results.map((s) => ({ |
37 | value: s.fullyQualifiedName, |
38 | label: `${s.labels.plural} (custom)`, |
39 | })), |
40 | ] |
41 | } |
42 |
|
43 | |
44 | * Create Engagement |
45 | * Log an engagement (note, task, meeting, call, email or communication) and optionally attach it to a record. `properties` uses the engagement's internal names (e.g. hs_call_body, hs_email_subject); `hs_timestamp` defaults to now. |
46 | */ |
47 | export async function main( |
48 | auth: RT.Hubspot, |
49 | engagement_type: |
50 | | "notes" |
51 | | "tasks" |
52 | | "meetings" |
53 | | "calls" |
54 | | "emails" |
55 | | "communications", |
56 | properties: { [key: string]: any }, |
57 | to_object_type: DynSelect_object_type | undefined, |
58 | to_object_id: string | undefined |
59 | ) { |
60 | const response = await fetch( |
61 | `https://api.hubapi.com/crm/v3/objects/${engagement_type}`, |
62 | { |
63 | method: "POST", |
64 | headers: { |
65 | Authorization: `Bearer ${auth.token}`, |
66 | "Content-Type": "application/json", |
67 | Accept: "application/json", |
68 | }, |
69 | body: JSON.stringify({ |
70 | properties: { hs_timestamp: new Date().toISOString(), ...properties }, |
71 | }), |
72 | } |
73 | ) |
74 |
|
75 | if (!response.ok) { |
76 | throw new Error(`${response.status} ${await response.text()}`) |
77 | } |
78 |
|
79 | const created = await response.json() |
80 | if (!to_object_type || !to_object_id) return created |
81 |
|
82 | const association = await fetch( |
83 | `https://api.hubapi.com/crm/v4/objects/${engagement_type}/${created.id}/associations/default/${to_object_type}/${to_object_id}`, |
84 | { |
85 | method: "PUT", |
86 | headers: { |
87 | Authorization: `Bearer ${auth.token}`, |
88 | Accept: "application/json", |
89 | }, |
90 | } |
91 | ) |
92 | if (!association.ok) { |
93 | throw new Error(`${association.status} ${await association.text()}`) |
94 | } |
95 |
|
96 | return { ...created, association: await association.json() } |
97 | } |
98 |
|