Edits history of script submission #20651 for ' DeleteLoyaltyReward (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * DeleteLoyaltyReward
     * Deletes a loyalty reward by doing the following:
    
    - Returns the loyalty points back to the loyalty account.
    - If an order ID was specified when the reward was created
    (see [CreateLoyaltyReward]($e/Loyalty/CreateLoyaltyReward)),
    it updates the order by removing the reward and related
    discounts.
    
    You cannot delete a reward that has reached the terminal state (REDEEMED).
     */
    export async function main(auth: Square, reward_id: string) {
      const url = new URL(
        `https://connect.squareup.com/v2/loyalty/rewards/${reward_id}`,
      );
    
      const response = await fetch(url, {
        method: "DELETE",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago