UNPKG

@shopgate/engage

Version:
34 lines • 9.58 kB
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{i18n}from'@shopgate/engage/core';import{DIRECT_SHIP,ROPIS}from'@shopgate/engage/locations';/** * Converts order line items to cart items * @param {Array} lineItems The line items to be converted * @return {Array} */export var convertLineItemsToCartItems=function convertLineItemsToCartItems(){var lineItems=arguments.length>0&&arguments[0]!==undefined?arguments[0]:[];return lineItems.map(function(lineItem){var id=lineItem.code,quantity=lineItem.quantity,_lineItem$orderedQuan=lineItem.orderedQuantity,orderedQuantity=_lineItem$orderedQuan===void 0?null:_lineItem$orderedQuan,salePrice=lineItem.salePrice,lineItemPrice=lineItem.price,promoAmount=lineItem.promoAmount,extendedPrice=lineItem.extendedPrice,discountAmount=lineItem.discountAmount,unitDiscountAmount=lineItem.unitDiscountAmount,unitPromoAmount=lineItem.unitPromoAmount,product=lineItem.product,status=lineItem.status,subStatus=lineItem.subStatus,fulfillmentMethod=lineItem.fulfillmentMethod,fulfillmentLocationCode=lineItem.fulfillmentLocationCode,substitutionAllowed=lineItem.substitutionAllowed;var productCode=product.code,productPrice=product.price,productSalePrice=product.salePrice,productEffectivePrice=product.effectivePrice,name=product.name,image=product.image,unit=product.unit,_product$hasCatchWeig=product.hasCatchWeight,hasCatchWeight=_product$hasCatchWeig===void 0?false:_product$hasCatchWeig,options=product.options;var properties=[];var appliedDiscounts=[];var additionalInfo=[];var coupon=null;var messages=[];var fulfillmentMethods;var fulfillment;var featuredImageUrl=null;if(Array.isArray(options)){properties=options.map(function(option){var _option$value,_option$values,_option$values$;return{label:option.name,value:(option===null||option===void 0?void 0:(_option$value=option.value)===null||_option$value===void 0?void 0:_option$value.name)||(option===null||option===void 0?void 0:(_option$values=option.values)===null||_option$values===void 0?void 0:(_option$values$=_option$values[0])===null||_option$values$===void 0?void 0:_option$values$.name)};});}if(fulfillmentMethod!==DIRECT_SHIP){fulfillmentMethods=[fulfillmentMethod];fulfillment={method:fulfillmentMethod,location:{code:fulfillmentLocationCode}};}if(image){if(image.includes('?')){featuredImageUrl="".concat(image,"&width=440&height=440&format=jpeg&fill=fff");}else{featuredImageUrl="".concat(image,"?width=440&height=440&format=jpeg&fill=fff");}}return{id:id,quantity:quantity,orderedQuantity:orderedQuantity,coupon:coupon,messages:messages,type:'product',status:status,subStatus:subStatus,product:{id:productCode,featuredImageUrl:featuredImageUrl,name:name,properties:properties,appliedDiscounts:appliedDiscounts,additionalInfo:additionalInfo,unit:unit,hasCatchWeight:hasCatchWeight,price:{unit:productPrice,unitSpecial:productSalePrice,"default":lineItemPrice,special:salePrice||null,unitSale:productSalePrice,unitEffective:productEffectivePrice,info:''},fulfillmentMethods:fulfillmentMethods},unitPromoAmount:unitPromoAmount,unitDiscountAmount:unitDiscountAmount,price:lineItemPrice,extendedPrice:extendedPrice,promoAmount:promoAmount,discountAmount:discountAmount,fulfillment:fulfillment,fulfillmentLocationId:fulfillmentLocationCode,substitutionAllowed:substitutionAllowed};});};/** * Extracts the promotions without coupon from the order. * @param {Object} order An order object * @returns {Array} */var getPromotionsFromOrder=function getPromotionsFromOrder(){var order=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var _order$appliedPromoti=order.appliedPromotions,appliedPromotions=_order$appliedPromoti===void 0?[]:_order$appliedPromoti;return appliedPromotions.filter(function(_ref){var coupon=_ref.coupon;return!coupon;});};/** * Extracts the coupons from the order. * @param {Object} order An order object * @returns {Array} */var getCouponsFromOrder=function getCouponsFromOrder(){var order=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var _order$appliedPromoti2=order.appliedPromotions,appliedPromotions=_order$appliedPromoti2===void 0?[]:_order$appliedPromoti2,_order$coupons=order.coupons,coupons=_order$coupons===void 0?[]:_order$coupons,_order$lineItems=order.lineItems,lineItems=_order$lineItems===void 0?[]:_order$lineItems;return coupons.filter(function(coupon){// Filter coupons which are assigned to line items var code=coupon===null||coupon===void 0?void 0:coupon.code;var lineItemCoupon=lineItems.find(function(cartItem){var lineItemPromotions=(cartItem===null||cartItem===void 0?void 0:cartItem.appliedPromotions)||[];var match=lineItemPromotions.find(function(promotion){var _promotion$coupon;var lineItemCouponCode=promotion===null||promotion===void 0?void 0:(_promotion$coupon=promotion.coupon)===null||_promotion$coupon===void 0?void 0:_promotion$coupon.code;return lineItemCouponCode===code;});return!!match;});return!lineItemCoupon;}).map(function(coupon){var _coupon$promotion;var code=coupon===null||coupon===void 0?void 0:(_coupon$promotion=coupon.promotion)===null||_coupon$promotion===void 0?void 0:_coupon$promotion.code;var promotion=appliedPromotions.find(function(promotionEntry){return promotionEntry.code===code;});return _extends({},coupon,{promotion:_extends({},(coupon===null||coupon===void 0?void 0:coupon.promotion)||{},{},promotion)});});};/** * Generates the coupon lines from an order * @param {Object} order An order object * @returns {Array} */export var getCouponLinesFromOrder=function getCouponLinesFromOrder(){var order=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};return getCouponsFromOrder(order).map(function(coupon){var _coupon$promotion2,_coupon$promotion2$di,_coupon$promotion3;return{visible:true,type:'coupon',label:(coupon===null||coupon===void 0?void 0:coupon.code)?i18n.text('cart.coupon_label',{label:coupon.code}):'',value:(coupon===null||coupon===void 0?void 0:(_coupon$promotion2=coupon.promotion)===null||_coupon$promotion2===void 0?void 0:(_coupon$promotion2$di=_coupon$promotion2.discount)===null||_coupon$promotion2$di===void 0?void 0:_coupon$promotion2$di.absoluteAmount)||null,currencyCode:order.currencyCode,messages:(coupon===null||coupon===void 0?void 0:coupon.messages)||null,info:(coupon===null||coupon===void 0?void 0:(_coupon$promotion3=coupon.promotion)===null||_coupon$promotion3===void 0?void 0:_coupon$promotion3.promotionalText)||null};});};/** * Generates the promotion lines from an order * @param {Object} order An order object * @returns {Array} */export var getPromotionLinesFromOrder=function getPromotionLinesFromOrder(){var order=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};return getPromotionsFromOrder(order).map(function(promotion){var _promotion$discount;return{visible:true,type:'promotion',label:promotion===null||promotion===void 0?void 0:promotion.name,value:(promotion===null||promotion===void 0?void 0:(_promotion$discount=promotion.discount)===null||_promotion$discount===void 0?void 0:_promotion$discount.absoluteAmount)||0,currencyCode:order.currencyCode,messages:null,info:(promotion===null||promotion===void 0?void 0:promotion.promotionalText)||null};});};/** * Generates checkout tax lines from an order object. * @param {Object} order An order object * @returns {Array} */export var getCheckoutTaxLinesFromOrder=function getCheckoutTaxLinesFromOrder(){var order=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var hasDirectShipItems=!!((order===null||order===void 0?void 0:order.lineItems)||[]).find(function(lineItem){return(lineItem===null||lineItem===void 0?void 0:lineItem.fulfillmentMethod)===DIRECT_SHIP;});var hasSelectedShippingMethod=!!((order===null||order===void 0?void 0:order.addressSequences)||[]).find(function(address){var _address$orderSegment;return(address===null||address===void 0?void 0:address.type)==='shipping'&&!!(address===null||address===void 0?void 0:(_address$orderSegment=address.orderSegment)===null||_address$orderSegment===void 0?void 0:_address$orderSegment.selectedShippingMethod);});return[{visible:true,type:'subTotal',label:null,value:order.subTotal||0,currencyCode:order.currencyCode}].concat(getPromotionLinesFromOrder(order),getCouponLinesFromOrder(order),[{visible:hasDirectShipItems&&hasSelectedShippingMethod,type:'shippingTotal',label:null,value:order.shippingTotal||0,currencyCode:order.currencyCode},{visible:order.taxAmount>0,type:'tax',label:null,value:order.taxAmount||0,currencyCode:order.currencyCode},{visible:true,type:'total',label:null,value:order.total||0,currencyCode:order.currencyCode}]);};/** * Checks if an order is a reserve only order * @param {Object} order An order object * @returns {boolean} */export var isReserveOnlyOrder=function isReserveOnlyOrder(){var order=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var nonReserveItem=order.lineItems.find(function(lineItem){return lineItem.fulfillmentMethod!==ROPIS;});return!nonReserveItem;};/** * Checks if an order is a direct ship only order * @param {Object} order An order object * @returns {boolean} */export var isDirectShipOnlyOrder=function isDirectShipOnlyOrder(){var order=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var nonDirectShipItem=order.lineItems.find(function(lineItem){return lineItem.fulfillmentMethod!==DIRECT_SHIP;});return!nonDirectShipItem;};