Vue Storefront is now Alokai! Learn More
Order normalizer

Order normalizer

  • normalizeOrder: This function is used to map SFCC Order into SfOrder, which includes order details data.
  • normalizeOrderListItem: This function maps SFCC ProductItem into SfOrderListItem which includes only basic order details, used to display an data in an order list.

Parameters

normalizeOrder

NameTypeDefault valueDescription
inputOrderSFCC Order
ctxNormalizerContextContext needed for the normalizer. Context contain a currency field that contains a currency code

normalizeOrderListItem

NameTypeDefault valueDescription
inputProductItemSFCC Order List Element

Extending

The SfOrder is returned from the GetOrders Method. If the SfOrder structure doesn't contain the information you need for your Storefront, you can extend its logic using the defineNormalizers function. The following example demonstrates how to extend SfOrder with a shipments field.

import { normalizers as normalizersSFC, defineNormalizers } from "@vsf-enterprise/unified-api-sfcc";

const normalizers = defineNormalizers<typeof normalizersSFCC>()({
  ...normalizersSFCC,
  normalizeOrder: (order, context) => ({
    ...normalizersSFCC.normalizeOrder(order, context),
    shipments: order.shipments,
  }),
});

You can override the normalizeOrder, but it's also available to override the smaller normalizers such as normalizeAddress, normalizeShippingMethod.

Source

order.ts
/* eslint-disable complexity */
import { defineNormalizer } from "../defineNormalizer";

export const normalizeOrder = defineNormalizer.normalizeOrder((input, ctx) => {
  const {
    orderNo,
    creationDate,
    status,
    productItems,
    productTotal,
    taxTotal,
    shippingTotal,
    billingAddress,
    shipments,
  } = input;

  if (
    !orderNo ||
    !creationDate ||
    !productItems ||
    productItems?.length === 0 ||
    !productTotal ||
    !taxTotal ||
    shippingTotal === undefined ||
    shippingTotal < 0 ||
    !billingAddress ||
    !shipments ||
    shipments?.length === 0 ||
    !("shippingMethod" in shipments[0]!) ||
    !("shippingAddress" in shipments[0]!)
  ) {
    throw new Error("Missing required order fields");
  }

  const subTotalWithoutTax = productTotal - taxTotal;
  const { shippingAddress, shippingMethod } = shipments[0];
  const { normalizeMoney, normalizeShippingMethod, normalizeAddress, normalizeOrderLineItem } =
    ctx.normalizers;

  return {
    id: orderNo,
    orderDate: new Date(creationDate).toISOString(),
    status: status ?? "UNKNOWN",
    lineItems: productItems.map((entry) => normalizeOrderLineItem(entry)),
    subtotalPrice: normalizeMoney(subTotalWithoutTax),
    totalShippingPrice: normalizeMoney(shippingTotal),
    totalTax: normalizeMoney(taxTotal),
    totalPrice: normalizeMoney(productTotal),
    shippingAddress: normalizeAddress(shippingAddress!),
    billingAddress: normalizeAddress(billingAddress),
    shippingMethod: normalizeShippingMethod(shippingMethod!)!,
    paymentMethod: "CARD",
  };
});