Order normalizer
normalizeOrder: This function is used to map SAPOrderintoSfOrder, which includes order details data.normalizeOrderListItem: This function maps SAPOrderHistoryinto UnifiedSfOrderListItemwhich includes only basic order details, used to display an data in an order list.
Parameters
normalizeOrder
| Name | Type | Default value | Description |
|---|---|---|---|
context | NormalizerContext | context needed for the normalizer. | |
input | Order | SAP Order |
normalizeOrderListItem
| Name | Type | Default value | Description |
|---|---|---|---|
context | NormalizerContext | context needed for the normalizer. | |
input | OrderHistory | SAP 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 ||
!Array.isArray(entries) ||
!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,
};
});