The Vue Storefront Essentials Course is now available! 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
inputOrderSAP Order
ctxNormalizerContextContext needed for the normalizer. transformImageUrl is added to transform line items image urls

normalizeOrderListItem

NameTypeDefault valueDescription
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 defineNormalizers function. The following example demonstrates how to extend SfOrder with an orderDiscounts field.

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

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

Source

order.ts
/* eslint-disable complexity */
import type { Order } from "@vsf-enterprise/sapcc-types";
import type { SfOrder } from "@vue-storefront/unified-data-model";
import { normalizeAddress, normalizeShippingMethod } from "@/normalizers/cart";
import { normalizeMoney } from "@/normalizers/money";
import { normalizeOrderLineItem } from "./orderLineItem";
import { NormalizerContext } from "@/normalizers/types";

export function normalizeOrder(input: Order, ctx: NormalizerContext): SfOrder {
  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 subTotalWithoutTax = {
    ...subTotal,
    value: subTotal.value! - totalTax.value!,
  };
  const billingAddress = paymentInfo?.billingAddress
    ? 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, ctx)),
    subtotalPrice: normalizeMoney(subTotalWithoutTax),
    totalShippingPrice: normalizeMoney(deliveryCost),
    totalTax: normalizeMoney(totalTax),
    totalPrice: normalizeMoney(totalPriceWithTax),
    shippingAddress: normalizeAddress(deliveryAddress),
    billingAddress,
    shippingMethod: normalizeShippingMethod(deliveryMode),
    paymentMethod,
  };
}