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 Note |
45 | * Add a note, optionally attached to a contact, company, deal, ticket or other record. `note_body` accepts HTML; `timestamp` (ISO 8601) defaults to now. |
46 | */ |
47 | export async function main( |
48 | auth: RT.Hubspot, |
49 | note_body: string, |
50 | to_object_type: DynSelect_object_type | undefined, |
51 | to_object_id: string | undefined, |
52 | timestamp: string | undefined, |
53 | owner_id: string | undefined |
54 | ) { |
55 | const properties: { [key: string]: any } = { |
56 | hs_note_body: note_body, |
57 | hs_timestamp: timestamp || new Date().toISOString(), |
58 | hubspot_owner_id: owner_id, |
59 | } |
60 | for (const key of Object.keys(properties)) { |
61 | if (properties[key] == null || properties[key] === "") |
62 | delete properties[key] |
63 | } |
64 |
|
65 | const response = await fetch("https://api.hubapi.com/crm/v3/objects/notes", { |
66 | method: "POST", |
67 | headers: { |
68 | Authorization: `Bearer ${auth.token}`, |
69 | "Content-Type": "application/json", |
70 | Accept: "application/json", |
71 | }, |
72 | body: JSON.stringify({ properties }), |
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/notes/${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 |
|