Edits history of script submission #20796 for ' SearchLoyaltyEvents (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * SearchLoyaltyEvents
     * Searches for loyalty events.
    
    A Square loyalty program maintains a ledger of events that occur during the lifetime of a
    buyer's loyalty account. Each change in the point balance
    (for example, points earned, points redeemed, and points expired) is
    recorded in the ledger. Using this endpoint, you can search the ledger for events.
    
    Search results are sorted by `created_at` in descending order.
     */
    export async function main(
      auth: Square,
      body: {
        query?: {
          filter?: {
            loyalty_account_filter?: { loyalty_account_id: string };
            type_filter?: {
              types:
                | "ACCUMULATE_POINTS"
                | "CREATE_REWARD"
                | "REDEEM_REWARD"
                | "DELETE_REWARD"
                | "ADJUST_POINTS"
                | "EXPIRE_POINTS"
                | "OTHER"
                | "ACCUMULATE_PROMOTION_POINTS"[];
            };
            date_time_filter?: {
              created_at: { start_at?: string; end_at?: string };
            };
            location_filter?: { location_ids: string[] };
            order_filter?: { order_id: string };
          };
        };
        limit?: number;
        cursor?: string;
      },
    ) {
      const url = new URL(`https://connect.squareup.com/v2/loyalty/events/search`);
    
      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