@shopgate/engage
Version:
Shopgate's ENGAGE library.
15 lines • 3.95 kB
JavaScript
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import groupBy from'lodash/groupBy';import{hasNewServices}from'@shopgate/engage/core/helpers';import{ROPIS,BOPIS}from"../locations";/**
* @param {Array} cartItems The cart items to sort.
* @returns {Array}
*/export function sortCartItems(cartItems){var grouped=groupBy(cartItems,'type');var sorted=Object.keys(grouped).reduce(function(acc,key){return[].concat(acc,grouped[key].sort(function(a,b){return Math.sign(!!b.fulfillment-!!a.fulfillment);}));},[]);var ropeItem=sorted.filter(function(item){return item.fulfillment&&[ROPIS,BOPIS].includes(item.fulfillment.method);}).sort(function(a,b){if(a.fulfillment.location.code<b.fulfillment.location.code)return 1;if(a.fulfillment.location.code>b.fulfillment.location.code)return-1;return 0;});var directItem=sorted.filter(function(item){return!item.fulfillment||![ROPIS,BOPIS].includes(item.fulfillment.method);});var merged=[].concat(ropeItem,directItem);// Group splitted line items - try to keep the original order intact as much as possible
var groupedByBaseLineItemId=groupBy(merged,function(e){return e.baseLineItemId||e.id;});merged=merged.reduce(function(acc,_ref){var id=_ref.id;var group=groupedByBaseLineItemId===null||groupedByBaseLineItemId===void 0?void 0:groupedByBaseLineItemId[id];if(group){return[].concat(acc,group);}return acc;},[]);var enhanced=merged.map(function(item){if(!item.fulfillment||![ROPIS,BOPIS].includes(item.fulfillment.method)){return _extends({},item,{fulfillmentLocationId:null,fulfillmentMethod:null});}return _extends({},item,{fulfillmentLocationId:item.fulfillment.location.code,fulfillmentMethod:item.fulfillment.method});});return enhanced;}/**
* @param {Object} cartItem A cart item
* @returns {Object}
*/export var createCartItemPrices=function createCartItemPrices(){var cartItem=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};/**
* When PWA doesn't run with new services and uses a "shopgate.cart.getCart.v1" pipeline from one
* of the available cart extensions, we return a price array based on the response of those
* pipelines.
*/if(!hasNewServices()){var _cartItem$product3=cartItem.product,_cartItem$product4=_cartItem$product3===void 0?{}:_cartItem$product3,_cartItem$product2$pr2=_cartItem$product4.price,_price=_cartItem$product2$pr2===void 0?{}:_cartItem$product2$pr2;return{price:[],subtotal:typeof(_price===null||_price===void 0?void 0:_price.special)==='number'&&_price.special!==_price["default"]?[{price:_price["default"]},{price:_price.special}]:[{price:_price["default"]}]};}/**
* When running with the new services, the "shopgate.cart.getCart.v1" pipeline returns entities
* with different price properties. Those are handled here.
*/var _cartItem$product5=cartItem.product,product=_cartItem$product5===void 0?{}:_cartItem$product5,quantity=cartItem.quantity,price=cartItem.price,promoAmount=cartItem.promoAmount,extendedPrice=cartItem.extendedPrice,unitPromoAmount=cartItem.unitPromoAmount,unitDiscountAmount=cartItem.unitDiscountAmount;var _ref2=(product===null||product===void 0?void 0:product.price)||{},unit=_ref2.unit,unitSale=_ref2.unitSale,unitEffective=_ref2.unitEffective;var prices={price:[{price:unit}],subtotal:[]};if(unitSale&&unitSale!==unit){prices.price.push({price:unitSale});prices.subtotal.push({price:unit*quantity});}prices.subtotal.push({price:price});if(unitPromoAmount){prices.price.push({price:unitEffective+unitPromoAmount,isPromo:true});prices.subtotal.push({price:price+promoAmount,isPromo:true});}if(unitDiscountAmount){prices.price.push({price:unitEffective+unitPromoAmount+unitDiscountAmount,isCoupon:true});prices.subtotal.push({price:extendedPrice,isCoupon:true});}return prices;};