Edits history of script submission #20624 for ' CreateLoyaltyReward (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * CreateLoyaltyReward
     * Creates a loyalty reward. In the process, the endpoint does following:
    
    - Uses the `reward_tier_id` in the request to determine the number of points
    to lock for this reward.
    - If the request includes `order_id`, it adds the reward and related discount to the order.
    
    After a reward is created, the points are locked and
    not available for the buyer to redeem another reward.
     */
    export async function main(
      auth: Square,
      body: {
        reward: {
          id?: string;
          status?: "ISSUED" | "REDEEMED" | "DELETED";
          loyalty_account_id: string;
          reward_tier_id: string;
          points?: number;
          order_id?: string;
          created_at?: string;
          updated_at?: string;
          redeemed_at?: string;
        };
        idempotency_key: string;
      },
    ) {
      const url = new URL(`https://connect.squareup.com/v2/loyalty/rewards`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + 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