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 | * Get Associated Objects |
45 | * Fetch the records of one object type associated with a CRM record, e.g. the emails, meetings, notes or deals of a contact. Returns up to `limit` (max 100) records with the properties you list. Both object types need their read scope on the connection (emails need sales-email-read). |
46 | */ |
47 | export async function main( |
48 | auth: RT.Hubspot, |
49 | object_type: DynSelect_object_type, |
50 | object_id: string, |
51 | to_object_type: DynSelect_object_type, |
52 | properties: string[] | undefined, |
53 | limit: number | undefined |
54 | ) { |
55 | const url = new URL( |
56 | `https://api.hubapi.com/crm/v4/objects/${object_type}/${object_id}/associations/${to_object_type}` |
57 | ) |
58 | url.searchParams.append("limit", String(limit ?? 100)) |
59 | const associations = await fetch(url, { |
60 | headers: { |
61 | Authorization: `Bearer ${auth.token}`, |
62 | Accept: "application/json", |
63 | }, |
64 | }) |
65 | if (!associations.ok) { |
66 | throw new Error(`${associations.status} ${await associations.text()}`) |
67 | } |
68 | const { results } = (await associations.json()) as { |
69 | results: { toObjectId: string | number }[] |
70 | } |
71 | if (!results.length) return [] |
72 |
|
73 | const body: { [key: string]: any } = { |
74 | inputs: results.map((r) => ({ id: String(r.toObjectId) })), |
75 | } |
76 | if (properties?.length) body.properties = properties |
77 |
|
78 | const response = await fetch( |
79 | `https://api.hubapi.com/crm/v3/objects/${to_object_type}/batch/read`, |
80 | { |
81 | method: "POST", |
82 | headers: { |
83 | Authorization: `Bearer ${auth.token}`, |
84 | "Content-Type": "application/json", |
85 | Accept: "application/json", |
86 | }, |
87 | body: JSON.stringify(body), |
88 | } |
89 | ) |
90 |
|
91 | if (!response.ok) { |
92 | throw new Error(`${response.status} ${await response.text()}`) |
93 | } |
94 |
|
95 | return ((await response.json()) as { results: any[] }).results |
96 | } |
97 |
|