Edits history of script submission #22010 for ' Update a rule (zoho)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Zoho = {
      token: string;
    };
    /**
     * Update a rule
     * Make changes to the rule, add or modify it and update.
     */
    export async function main(
      auth: Zoho,
      rule_id: string,
      organization_id: string | undefined,
      body: {
        rule_name: string;
        target_account_id: number;
        apply_to: string;
        criteria_type: string;
        criterion: {
          criteria_id?: string;
          field?: string;
          comparator?: string;
          value?: string;
        }[];
        record_as: string;
        account_id?: number;
        customer_id?: number;
        tax_id?: string;
        reference_number?: string;
        vat_treatment?: string;
        tax_treatment?: string;
        is_reverse_charge_applied?: false | true;
        product_type?: string;
        tax_authority_id?: string;
        tax_exemption_id?: string;
      },
    ) {
      const url = new URL(
        `https://www.zohoapis.com/books/v3/bankaccounts/rules/${rule_id}`,
      );
      for (const [k, v] of [["organization_id", organization_id]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Zoho-oauthtoken " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago