1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Update a project using a custom field's unique value |
7 | * A custom field will have unique values if it's configured to not accept duplicate values. |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | organization_id: string | undefined, |
12 | X_Unique_Identifier_Key: string, |
13 | X_Unique_Identifier_Value: string, |
14 | body: { |
15 | project_name: string; |
16 | customer_id: string; |
17 | currency_id?: string; |
18 | description?: string; |
19 | billing_type: string; |
20 | rate?: string; |
21 | budget_type?: string; |
22 | budget_hours?: string; |
23 | budget_amount?: string; |
24 | cost_budget_amount?: number; |
25 | user_id: string; |
26 | tasks?: { |
27 | task_name: string; |
28 | description?: string; |
29 | rate?: string; |
30 | budget_hours?: string; |
31 | }[]; |
32 | users?: { |
33 | user_id?: string; |
34 | is_current_user?: false | true; |
35 | user_name?: string; |
36 | email?: string; |
37 | user_role?: string; |
38 | status?: string; |
39 | rate?: string; |
40 | budget_hours?: string; |
41 | total_hours?: string; |
42 | billed_hours?: string; |
43 | un_billed_hours?: string; |
44 | cost_rate?: number; |
45 | }[]; |
46 | }, |
47 | X_Upsert?: string, |
48 | ) { |
49 | const url = new URL(`https://www.zohoapis.com/books/v3/projects`); |
50 | for (const [k, v] of [["organization_id", organization_id]]) { |
51 | if (v !== undefined && v !== "" && k !== undefined) { |
52 | url.searchParams.append(k, v); |
53 | } |
54 | } |
55 | const response = await fetch(url, { |
56 | method: "PUT", |
57 | headers: { |
58 | "X-Unique-Identifier-Key": X_Unique_Identifier_Key, |
59 | "X-Unique-Identifier-Value": X_Unique_Identifier_Value, |
60 | ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}), |
61 | "Content-Type": "application/json", |
62 | Authorization: "Zoho-oauthtoken " + auth.token, |
63 | }, |
64 | body: JSON.stringify(body), |
65 | }); |
66 | if (!response.ok) { |
67 | const text = await response.text(); |
68 | throw new Error(`${response.status} ${text}`); |
69 | } |
70 | return await response.json(); |
71 | } |
72 |
|