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 Association |
45 | * Associate two CRM records. Leave `association_types` empty for the default (unlabeled) association, or pass labels from List Association Labels, e.g. [{"associationCategory": "USER_DEFINED", "associationTypeId": 36}]. Object types other than contacts, companies and deals need their own scope on the connection (e.g. tickets, crm.objects.line_items.read, crm.objects.custom.read). |
46 | */ |
47 | export async function main( |
48 | auth: RT.Hubspot, |
49 | from_object_type: DynSelect_object_type, |
50 | from_object_id: string, |
51 | to_object_type: DynSelect_object_type, |
52 | to_object_id: string, |
53 | association_types: |
54 | | { |
55 | associationCategory: |
56 | | "HUBSPOT_DEFINED" |
57 | | "USER_DEFINED" |
58 | | "INTEGRATOR_DEFINED" |
59 | associationTypeId: number |
60 | }[] |
61 | | undefined |
62 | ) { |
63 | const labeled = association_types?.length |
64 | const response = await fetch( |
65 | `https://api.hubapi.com/crm/v4/objects/${from_object_type}/${from_object_id}/associations/${labeled ? "" : "default/"}${to_object_type}/${to_object_id}`, |
66 | labeled |
67 | ? { |
68 | method: "PUT", |
69 | headers: { |
70 | Authorization: `Bearer ${auth.token}`, |
71 | "Content-Type": "application/json", |
72 | Accept: "application/json", |
73 | }, |
74 | body: JSON.stringify(association_types), |
75 | } |
76 | : { |
77 | method: "PUT", |
78 | headers: { |
79 | Authorization: `Bearer ${auth.token}`, |
80 | Accept: "application/json", |
81 | }, |
82 | } |
83 | ) |
84 |
|
85 | if (!response.ok) { |
86 | throw new Error(`${response.status} ${await response.text()}`) |
87 | } |
88 |
|
89 | return await response.json() |
90 | } |
91 |
|