1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Email a purchase order |
7 | * Email a purchase order to the vendor. Input json string is not mandatory. If input json string is empty, mail will be send with default mail content. |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | purchaseorder_id: string, |
12 | organization_id: string | undefined, |
13 | attachments: string | undefined, |
14 | send_attachment: string | undefined, |
15 | file_name: string | undefined, |
16 | body: { |
17 | send_from_org_email_id?: false | true; |
18 | from_address_id?: number; |
19 | to_mail_ids: string[]; |
20 | cc_mail_ids?: string[]; |
21 | bcc_mail_ids?: string[]; |
22 | subject?: string; |
23 | body: string; |
24 | mail_documents?: { document_id?: number; file_name?: string }[]; |
25 | }, |
26 | ) { |
27 | const url = new URL( |
28 | `https://www.zohoapis.com/books/v3/purchaseorders/${purchaseorder_id}/email`, |
29 | ); |
30 | for (const [k, v] of [ |
31 | ["organization_id", organization_id], |
32 | ["attachments", attachments], |
33 | ["send_attachment", send_attachment], |
34 | ["file_name", file_name], |
35 | ]) { |
36 | if (v !== undefined && v !== "" && k !== undefined) { |
37 | url.searchParams.append(k, v); |
38 | } |
39 | } |
40 | const response = await fetch(url, { |
41 | method: "POST", |
42 | headers: { |
43 | "Content-Type": "application/json", |
44 | Authorization: "Zoho-oauthtoken " + auth.token, |
45 | }, |
46 | body: JSON.stringify(body), |
47 | }); |
48 | if (!response.ok) { |
49 | const text = await response.text(); |
50 | throw new Error(`${response.status} ${text}`); |
51 | } |
52 | return await response.json(); |
53 | } |
54 |
|