Vue Storefront is now Alokai! Learn More
GetOrders

GetOrders

Implements GetOrders Unified Method.

Source

import { getNormalizers } from "@vsf-enterprise/unified-api-sapcc/udl";
import { assertAuthorized, defineApi } from "@vsf-enterprise/unified-api-sapcc";
import { ORDER_STATUSES } from "@vsf-enterprise/sapcc-types";


declare module "@vsf-enterprise/unified-api-sapcc" {
  interface GetOrdersExtendedArgs {
    /**
     * Response configuration. List of fields returned in the response body.
     */
    fields?: "BASIC" | "DEFAULT" | "FULL" | string | string[];
    /**
     * Sorting method applied to the return results.
     */
    sort?: string;

    /**
     * Order statuses to filter the returned results by.
     * For example, `statuses: CANCELLED,CHECKED_VALID`
     * would only return orders with status `CANCELLED` or `CHECKED_VALID`.
     */
    statuses?: keyof typeof ORDER_STATUSES | string;
  }
}


export const getOrders = defineApi.getOrders(async (context, args) => {
  assertAuthorized(context);
  const { currentPage = 1, pageSize = 20 } = args ?? {};

  const {
    data: { orders, pagination },
  } = await context.api.getUserOrderHistory({
    currentPage: currentPage - 1,
    pageSize,
  });
  const { normalizeOrderListItem, normalizePagination } = getNormalizers(context);

  const normalizedOrders = orders
    ? orders?.map((order) => normalizeOrderListItem(order)).filter(Boolean)
    : [];

  return {
    orders: normalizedOrders,
    pagination: normalizePagination(pagination!),
  };
});