Order normalizer
normalizeOrder
: This function is used to map SAPOrder
intoSfOrder
, which includes order details data.normalizeOrderListItem
: This function maps SAPOrderHistory
into UnifiedSfOrderListItem
which includes only basic order details, used to display an data in an order list.
Parameters
normalizeOrder
Name | Type | Default value | Description |
---|---|---|---|
input | Order | SAP Order | |
ctx | NormalizerContext | Context needed for the normalizer. transformImageUrl is added to transform line items image urls |
normalizeOrderListItem
Name | Type | Default value | Description |
---|---|---|---|
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 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,
}),
});
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 {
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 } =
ctx.normalizers;
const subTotalWithoutTax = {
...subTotal,
value: subTotal.value! - totalTax.value!,
};
const billingAddress = paymentInfo?.billingAddress
? ctx.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,
};
});