UNPKG

@shopgate/engage

Version:
28 lines 5.34 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 snakeCase from'lodash/snakeCase';import{makeGetMerchantSettings,i18n,getPlatform,getUserAgent}from'@shopgate/engage/core';import appConfig from'@shopgate/pwa-common/helpers/config';import{getCurrency,getCartItems,getSubTotal,getGrandTotal}from'@shopgate/pwa-common-commerce/cart/selectors';import{getProductDataById}from'@shopgate/engage/product/selectors/product';import{getPreferredLocation,getExternalCustomerNumberForOrder}from"../selectors";import{ROPIS}from"../constants";/** * Creates the address sequence. * @param {Object} formValues The reserve form values. * @param {Function} getState The redux getState function. * @returns {Array} */function createAddressSequence(formValues,getState){var _makeGetMerchantSetti=makeGetMerchantSettings()(getState()),_makeGetMerchantSetti2=_makeGetMerchantSetti.country,country=_makeGetMerchantSetti2===void 0?appConfig.marketId:_makeGetMerchantSetti2;return[{type:'billing',firstName:formValues.firstName,lastName:formValues.lastName,phone:formValues.cellPhone,emailAddress:formValues.email,country:country},{type:'pickup',firstName:formValues.firstName2,lastName:formValues.lastName2,phone:formValues.cellPhone2,emailAddress:formValues.email2,country:country}];}/** * Retrieves the featured image for a product. If the product doesn't have a featured image * and is a child product, the featured image of the base product is retrieved. * @param {Object} product The current product. * @param {Function} getState The redux getState function. * @returns {string} */function getProductImage(product,getState){var image=product.featuredImageBaseUrl||product.featuredImageUrl;if(!image&&product.baseProductId){var baseProduct=getProductDataById(getState(),{productId:product.id});if(baseProduct){image=product.featuredImageBaseUrl||baseProduct.featuredImageUrl;}}return image;}/** * Creates a single order line item (from quick reserve). * @param {Object} product The current product. * @param {Function} getState The redux getState function. * @returns {Array} */function createSingleProductItems(product,getState){var location=getPreferredLocation(getState());return[{code:product.id,quantity:1,fulfillmentMethod:ROPIS,fulfillmentLocationCode:location.code,price:product.price.unitPrice,shipToAddressSequenceIndex:1,currencyCode:product.price.currency,product:_extends({code:product.id,name:product.name,image:getProductImage(product,getState),price:product.price.unitPrice,currencyCode:product.price.currency},product.characteristics&&{options:product.characteristics.map(function(characteristic){return{code:String(characteristic.id),name:characteristic.label,value:{code:snakeCase(characteristic.value),name:characteristic.value}};})})}];}/** * Creates the order line items. * @param {Function} getState The redux getState function. * @returns {Object[]} */function createCartLineItems(getState){var items=getCartItems(getState());var currencyCode=getCurrency(getState());return items.map(function(item){return{code:item.id,quantity:item.quantity,fulfillmentMethod:item.fulfillment.method,fulfillmentLocationCode:item.fulfillment.location.code,price:item.product.price["default"],shipToAddressSequenceIndex:1,currencyCode:currencyCode,product:_extends({code:item.product.id,name:item.product.name,image:item.product.featuredImageBaseUrl||item.product.featuredImageUrl,price:item.product.price.unit,currencyCode:currencyCode},item.product.properties&&{options:item.product.properties.map(function(prop){return{code:snakeCase(prop.label),name:prop.label,value:{code:snakeCase(prop.value),name:prop.value}};})})};});}/** * Creates a fulfillment order out of the reserve form values and the current product or the cart. * @param {Object} formValues The reserve form values. * @param {Object} product The current product. * @param {Function} getState The redux getState function. * @returns {Object} */function createOrder(formValues,product,getState){var state=getState();var userAgent=getUserAgent();var platform='engage';var os=getPlatform(state);var externalCustomerNumber=getExternalCustomerNumberForOrder(state);// If no individual product was submitted, we handle the cart. if(product===null){var grandTotal=getGrandTotal(state);return{localeCode:i18n.getLang().toLowerCase(),currencyCode:getCurrency(state),addressSequences:createAddressSequence(formValues,getState),primaryBillToAddressSequenceIndex:0,primaryShipToAddressSequenceIndex:1,lineItems:createCartLineItems(getState),subTotal:getSubTotal(state)||grandTotal,total:grandTotal,externalCustomerNumber:externalCustomerNumber,userAgent:userAgent,platform:platform,os:os};}return{localeCode:i18n.getLang().toLowerCase(),currencyCode:product.price.currency,addressSequences:createAddressSequence(formValues,getState),primaryBillToAddressSequenceIndex:0,primaryShipToAddressSequenceIndex:1,lineItems:createSingleProductItems(product,getState),subTotal:product.price.unitPrice,total:product.price.unitPrice,externalCustomerNumber:externalCustomerNumber,userAgent:userAgent,platform:platform,os:os};}export default createOrder;