Vue Storefront is now Alokai! Learn More
GetOrders

GetOrders

Implements GetOrders Unified Method.

Source

import { assertAuthorized, defineApi } from "@vsf-enterprise/unified-api-sfcc";
import type { Order } from "@vsf-enterprise/sfcc-types";
import { getNormalizers } from "@vsf-enterprise/unified-api-sfcc/udl";


declare module "@vsf-enterprise/unified-api-sfcc" {
  interface GetOrdersExtendedArgs {
    /**
     * Whether to include orders from all sites where this customer exists (through a shared customer list)
     * or only for the currently configured site
     */
    crossSites?: boolean;
    /**
     * If provided, only orders that were created after this point in time will be returned.
     * ISO8601 date time format: yyyy-MM-dd'T'HH:mmZ
     * @example 2012-10-31T08:46:00Z
     */
    from?: string;
    /**
     * If provided, only orders that were created before this point in time will be returned.
     * ISO8601 date time format: yyyy-MM-dd'T'HH:mmZ
     * @example 2012-10-31T08:46:00Z
     */
    until?: string;
    /**
     * If provided, only orders with this status will be returned
     */
    status?: "created" | "new" | "open" | "completed" | "cancelled" | "replaced" | "failed";
  }
}


export const getOrders = defineApi.getOrders(async (context, args) => {
  await assertAuthorized(context);

  const { normalizeOrderListItem, normalizePagination } = getNormalizers(context);

  const { currentPage = 1, pageSize = 20 } = args ?? {};
  const {
    data: orders = [],
    limit,
    offset,
    total,
  } = await context.api.getCustomerOrders({
    offset: (currentPage - 1) * pageSize,
    limit: pageSize,
  });

  return {
    orders: orders.map((order: Order) => normalizeOrderListItem(order)).filter(Boolean),
    pagination: normalizePagination({
      limit,
      total,
      offset,
    }),
  };
});