All files / app/shipping getShippableLineItems.ts

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  7x   7x     7x       37x   38x                           37x 37x   37x 99x               37x    
import { Cart, Consignment, PhysicalItem } from '@bigcommerce/checkout-sdk';
import { reduce } from 'lodash';
 
import findConsignment from './findConsignment';
import ShippableItem from './ShippableItem';
 
export default function getShippableLineItems(
    cart: Cart,
    consignments: Consignment[]
): ShippableItem[] {
    return reduce(
        (cart && cart.lineItems.physicalItems) || [],
        (result, item, i) => (
            !item.addedByPromotion ?
                result.concat(...splitItem(item, consignments, i)) :
                result
        ),
        [] as ShippableItem[]
    );
}
 
function splitItem(
    item: PhysicalItem,
    consignments: Consignment[],
    lineItemIndex: number
): ShippableItem[] {
    let splitItems: ShippableItem[] = [];
    const consignment = findConsignment(consignments, item.id as string);
 
    for (let i = 0; i < item.quantity; i++) {
        splitItems = splitItems.concat({
            ...item,
            key: `${item.variantId}-${item.productId}-${lineItemIndex}-${i}`,
            consignment,
            quantity: 1,
        });
    }
 
    return splitItems;
}