1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Email contact |
7 | * Send email to contact. |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | contact_id: string, |
12 | organization_id: string | undefined, |
13 | send_customer_statement: string | undefined, |
14 | body: { |
15 | to_mail_ids: string[]; |
16 | subject: string; |
17 | body: string; |
18 | attachments?: string; |
19 | }, |
20 | ) { |
21 | const url = new URL( |
22 | `https://www.zohoapis.com/inventory/v1/contacts/${contact_id}/email`, |
23 | ); |
24 | for (const [k, v] of [ |
25 | ["organization_id", organization_id], |
26 | ["send_customer_statement", send_customer_statement], |
27 | ]) { |
28 | if (v !== undefined && v !== "" && k !== undefined) { |
29 | url.searchParams.append(k, v); |
30 | } |
31 | } |
32 | const response = await fetch(url, { |
33 | method: "POST", |
34 | headers: { |
35 | "Content-Type": "application/json", |
36 | Authorization: "Zoho-oauthtoken " + auth.token, |
37 | }, |
38 | body: JSON.stringify(body), |
39 | }); |
40 | if (!response.ok) { |
41 | const text = await response.text(); |
42 | throw new Error(`${response.status} ${text}`); |
43 | } |
44 | return await response.json(); |
45 | } |
46 |
|