1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create an item |
7 | * Creates a new item in Zoho Inventory. |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | organization_id: string | undefined, |
12 | body: { |
13 | group_id?: string; |
14 | group_name?: string; |
15 | unit?: string; |
16 | documents?: string[]; |
17 | item_type?: string; |
18 | product_type?: string; |
19 | is_taxable?: false | true; |
20 | tax_id?: number; |
21 | description?: string; |
22 | purchase_account_id?: number; |
23 | inventory_account_id?: number; |
24 | attribute_name1?: string; |
25 | name: string; |
26 | rate?: number; |
27 | purchase_rate?: number; |
28 | reorder_level?: number; |
29 | locations?: { |
30 | location_id?: string; |
31 | initial_stock?: number; |
32 | initial_stock_rate?: number; |
33 | }[]; |
34 | vendor_id?: number; |
35 | vendor_name?: string; |
36 | sku?: string; |
37 | upc?: number; |
38 | ean?: number; |
39 | isbn?: string; |
40 | part_number?: string; |
41 | attribute_option_name1?: number; |
42 | purchase_description?: string; |
43 | item_tax_preferences?: { tax_id?: number; tax_specification?: string }[]; |
44 | hsn_or_sac?: string; |
45 | sat_item_key_code?: string; |
46 | unitkey_code?: string; |
47 | custom_fields?: { customfield_id?: number; value?: string }[]; |
48 | }, |
49 | ) { |
50 | const url = new URL(`https://www.zohoapis.com/inventory/v1/items`); |
51 | for (const [k, v] of [["organization_id", organization_id]]) { |
52 | if (v !== undefined && v !== "" && k !== undefined) { |
53 | url.searchParams.append(k, v); |
54 | } |
55 | } |
56 | const response = await fetch(url, { |
57 | method: "POST", |
58 | headers: { |
59 | "Content-Type": "application/json", |
60 | Authorization: "Zoho-oauthtoken " + auth.token, |
61 | }, |
62 | body: JSON.stringify(body), |
63 | }); |
64 | if (!response.ok) { |
65 | const text = await response.text(); |
66 | throw new Error(`${response.status} ${text}`); |
67 | } |
68 | return await response.json(); |
69 | } |
70 |
|