1 | |
2 | type Pipedrive = { |
3 | apiToken: string; |
4 | }; |
5 | |
6 | * Search deals |
7 | * Searches all deals by title, notes and/or custom fields. This endpoint is a wrapper of /v1/itemSearch with a narrower OAuth scope. Found deals can be filtered by the person ID and the organization ID. |
8 | */ |
9 | export async function main( |
10 | auth: Pipedrive, |
11 | term: string | undefined, |
12 | fields: "custom_fields" | "notes" | "title" | undefined, |
13 | exact_match: string | undefined, |
14 | person_id: string | undefined, |
15 | organization_id: string | undefined, |
16 | status: "open" | "won" | "lost" | undefined, |
17 | include_fields: "deal.cc_email" | undefined, |
18 | limit: string | undefined, |
19 | cursor: string | undefined, |
20 | ) { |
21 | const url = new URL(`https://api.pipedrive.com/api/v2/deals/search`); |
22 | for (const [k, v] of [ |
23 | ["term", term], |
24 | ["fields", fields], |
25 | ["exact_match", exact_match], |
26 | ["person_id", person_id], |
27 | ["organization_id", organization_id], |
28 | ["status", status], |
29 | ["include_fields", include_fields], |
30 | ["limit", limit], |
31 | ["cursor", cursor], |
32 | ]) { |
33 | if (v !== undefined && v !== "" && k !== undefined) { |
34 | url.searchParams.append(k, v); |
35 | } |
36 | } |
37 | const response = await fetch(url, { |
38 | method: "GET", |
39 | headers: { |
40 | "x-api-token": auth.apiToken, |
41 | }, |
42 | body: undefined, |
43 | }); |
44 | if (!response.ok) { |
45 | const text = await response.text(); |
46 | throw new Error(`${response.status} ${text}`); |
47 | } |
48 | return await response.json(); |
49 | } |
50 |
|