0
Get Deal
One script reply has been approved by the moderators Verified
Created by jeancofeneodo 899 days ago Viewed 9251 times
0
Submitted by adam186 Deno
Verified 716 days ago
1
import { Client } from "npm:@hubspot/api-client@^8.1.0";
2

3
/**
4
 * @param deal_id You may find it in the HubSpot App under the name of `Record ID`
5
 *
6
 * @param properties List of the properties to be returned in the response.
7
 *
8
 * @param properties_with_history List of the properties to be returned
9
 * along with their history of previous values.
10
 *
11
 * @param id_property The name of a property whose values are unique for this object type.
12
 */
13
type Hubspot = {
14
  token: string;
15
};
16
export async function main(
17
  auth: Hubspot,
18
  deal_id: string,
19
  properties?: string[],
20
  properties_with_history?: string[],
21
  associations?: string[],
22
  archived?: boolean,
23
  id_property?: string,
24
) {
25
  const client = new Client({
26
    accessToken: auth.token,
27
  });
28

29
  try {
30
    return await client.crm.deals.basicApi.getById(
31
      deal_id,
32
      getProp(properties),
33
      getProp(properties_with_history),
34
      getProp(associations),
35
      archived || undefined,
36
      id_property || undefined,
37
    );
38
  } catch (e) {
39
    throw Error(`
40
      ${e.code}\n
41
      Message: ${e.body.message || e.body}\n
42
    `);
43
  }
44
}
45

46
function getProp(prop?: string[]) {
47
  return Array.isArray(prop) && prop.length ? prop : undefined;
48
}
49