Edits history of script submission #21210 for ' Create a rule (zoho)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Zoho = {
      token: string;
    };
    /**
     * Create a rule
     * Create a rule and apply it on deposit/withdrawal for bank accounts and on refund/charges for credit card accounts.
     */
    export async function main(
      auth: Zoho,
      organization_id: string | undefined,
      body: {
        rule_name: string;
        target_account_id: number;
        apply_to: string;
        criteria_type: string;
        criterion: { 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`);
      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: "POST",
        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