Vue Storefront is now Alokai! Learn More
Order normalizer

Order normalizer

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

Parameters

normalizeOrder

NameTypeDefault valueDescription
contextNormalizerContextcontext needed for the normalizer.
inputOrderSAP Order

normalizeOrderListItem

NameTypeDefault valueDescription
contextNormalizerContextcontext needed for the normalizer.
inputOrderHistorySAP 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 addCustomFields API. The following example demonstrates how to extend SfOrder with an orderDiscounts field.

export const unifiedApiExtension = createUnifiedExtension({
  normalizers: {
    addCustomFields: [
      {
        normalizeOrder: (context, order) => ({
          orderDiscounts: order.orderDiscounts,
        }),
      },
    ],
  },
  config: {
    ...
  },
});

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((context, input) => {
  const {
    code,
    created,
    status,
    entries,
    subTotal,
    deliveryCost,
    totalTax,
    totalPriceWithTax,
    deliveryAddress,
    deliveryMode,
    paymentInfo,
    costCenter,
  } = input;

  if (
    !code ||
    !created ||
    !subTotal ||
    !deliveryCost ||
    !totalTax ||
    !entries ||
    entries?.length === 0 ||
    !totalPriceWithTax ||
    !deliveryAddress ||
    !deliveryMode
  ) {
    throw new Error("Missing required order fields");
  }

  const { normalizeAddress, normalizeOrderLineItem, normalizeShippingMethod, normalizeMoney } =
    context.normalizers;
  const subTotalWithoutTax = {
    ...subTotal,
    value: subTotal.value! - totalTax.value!,
  };
  const billingAddress = paymentInfo?.billingAddress
    ? context.normalizers.normalizeAddress(paymentInfo.billingAddress)
    : null;
  const paymentMethod = costCenter ? "ACCOUNT" : "CARD";

  return {
    id: code,
    orderDate: new Date(created).toISOString(),
    status: status ?? "UNKNOWN",
    lineItems: entries.map((entry) => normalizeOrderLineItem(entry)),
    subtotalPrice: normalizeMoney(subTotalWithoutTax),
    totalShippingPrice: normalizeMoney(deliveryCost),
    totalTax: normalizeMoney(totalTax),
    totalPrice: normalizeMoney(totalPriceWithTax),
    shippingAddress: normalizeAddress(deliveryAddress),
    billingAddress,
    shippingMethod: normalizeShippingMethod(deliveryMode),
    paymentMethod,
  };
});