0

Get all deals

by
Published Apr 8, 2025
Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
/**
6
 * Get all deals
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  filters_attributes_deal_name_: string | undefined,
12
  filters_linkedCompaniesIds_: string | undefined,
13
  filters_linkedContactsIds_: string | undefined,
14
  modifiedSince: string | undefined,
15
  createdSince: string | undefined,
16
  offset: string | undefined,
17
  limit: string | undefined,
18
  sort: "asc" | "desc" | undefined,
19
) {
20
  const url = new URL(`https://api.brevo.com/v3/crm/deals`);
21
  for (const [k, v] of [
22
    ["filters[attributes.deal_name]", filters_attributes_deal_name_],
23
    ["filters[linkedCompaniesIds]", filters_linkedCompaniesIds_],
24
    ["filters[linkedContactsIds]", filters_linkedContactsIds_],
25
    ["modifiedSince", modifiedSince],
26
    ["createdSince", createdSince],
27
    ["offset", offset],
28
    ["limit", limit],
29
    ["sort", sort],
30
  ]) {
31
    if (v !== undefined && v !== "" && k !== undefined) {
32
      url.searchParams.append(k, v);
33
    }
34
  }
35
  const response = await fetch(url, {
36
    method: "GET",
37
    headers: {
38
      "api-key": auth.apiKey,
39
    },
40
    body: undefined,
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48