import { ProductItem } from '../modules/cart/models/cart.interface'; import * as constant from '@hdca/fe-angular-shared'; import { deliveryOrderGroup, pickUpOrderGroup } from './cart.utils'; /** * Remove all space(s) from postal code string * param {string} postalCode - postal code provided * return {string} - clean postal code */ export const cleanPostalCode = (postalCode: string): string => { if (postalCode) { const hasSpace = /\s/g.test(postalCode); return hasSpace ? postalCode.replace(/\s/g, '') : postalCode; } return null; }; /** * Handle to get Personalized ETA JSON request for SAP * *Note: the implementation right now is with only `SHIPTOHOME` and `BOSS` order group * type {{ * getProductCodeList: (entries, fulfillmentType: string) => Array; * hasPETAInOrder: (productList: ProductItem) => boolean; * getPETARequest: (productList: ProductItem, customerId: string, postalCode: string, storeName: string) => string; * cleanArticleId: (productCode: string, maxLength: number) => string; generatePETARequest: (itemObject, itemOptions) => string * }} */ export const petaRequest = { /** * Handle generate customized array object of both `SHIPTOHOME` and `BOSS` when PETA is flagged * param entries - cart entries for `SHIPTOHOME` and `BOSS` * param {string} fulfillmentType - order type for `SHIPTOHOME` and `BOSS` * return {Array} - customized array object */ getProductCodeList: function (entries, fulfillmentType: string): Array { const objList = []; entries.forEach((entry) => { objList.push({ productCode: entry.product.code, quantity: entry.quantity, fulfillmentType: fulfillmentType }); }); return objList; }, /** * To check whether the order contains PETA state * Note: Only for STH, BOPIS and BOSS items * param {ProductItem} productList - cart items * return {boolean} - true if existed else false */ hasPETAOrder: function (productList: ProductItem): boolean { const shipToHomeData = deliveryOrderGroup.getDeliverOrderObject(productList, constant.utils.SHIP_TO_YOU); const bossData = pickUpOrderGroup.getPickUpOrderGroupObject(productList, constant.utils.BOSS); const bopisData = pickUpOrderGroup.getPickUpOrderGroupObject(productList, constant.utils.BOPIS); const isSTHPETAEligible = (shipToHomeData && shipToHomeData.personalizedETADestination) ? true : false; const isBOPISPETAEligible = (bopisData && bopisData.personalizedETADestination) ? true : false; const isBOSSPETAEligible = (bossData && bossData.personalizedETADestination) ? true : false; return isSTHPETAEligible || isBOPISPETAEligible || isBOSSPETAEligible; }, /** * Handle to generate JSON request string for SAP * param {string} ProductId - cart items * param {string} customerId - customer logged in such as email from cookie * param {string} postalCode - customer postal code from cookie * param {string} storeName - store name for example: 7001 from cookie if there is no store `name` in `deliveryPointOfService` * return {string} - string format JSON for SAP request */ getPETARequestProduct: function (productId: string, customerId: string, storeName: string, sthPostalCode: string) { const productCodeList = [{ productCode: productId, quantity: 1 }]; const itemOptions = { customerId: customerId, postalCode: cleanPostalCode(sthPostalCode), storeName: storeName }; return this.generatePETARequest(productCodeList, itemOptions, productId); }, /** * Handle to generate JSON request string for SAP * param {ProductItem} productList - cart items * param {string} customerId - customer logged in such as email from cookie * param {string} postalCode - customer postal code from cookie * param {string} storeName - store name for example: 7001 from cookie if there is no store `name` in `deliveryPointOfService` * return {string} - string format JSON for SAP request */ getPETARequestCart: function (productList: ProductItem, customerId: string, storeName: string) { const shipToHomeData = deliveryOrderGroup.getDeliverOrderObject(productList, constant.utils.SHIP_TO_YOU); const bopisData = pickUpOrderGroup.getPickUpOrderGroupObject(productList, constant.utils.BOPIS); const bossData = pickUpOrderGroup.getPickUpOrderGroupObject(productList, constant.utils.BOSS); const isSTHPETAEligible = (shipToHomeData && shipToHomeData.personalizedETADestination) ? true : false; const isBOPISPETAEligible = (bopisData && bopisData.personalizedETADestination) ? true : false; const isBOSSPETAEligible = (bossData && bossData.personalizedETADestination) ? true : false; const shipToHomeProductCodes = isSTHPETAEligible ? this.getProductCodeList(shipToHomeData.entries, shipToHomeData.personalizedETADestination.fulfillmentType) : []; const bopisProductCodes = isBOPISPETAEligible ? this.getProductCodeList(bopisData.entries, bopisData.personalizedETADestination.fulfillmentType) : []; const bossProductCodes = isBOSSPETAEligible ? this.getProductCodeList(bossData.entries, bossData.personalizedETADestination.fulfillmentType) : []; const mergedProductCodeList = shipToHomeProductCodes.concat(bopisProductCodes).concat(bossProductCodes); const bopisPostalCode = isBOPISPETAEligible ? bopisData.personalizedETADestination.postalCode : ''; const bossPostalCode = isBOSSPETAEligible ? bossData.personalizedETADestination.postalCode : ''; const petaPostalCode = isSTHPETAEligible ? shipToHomeData.personalizedETADestination.postalCode : bopisPostalCode ? bopisPostalCode : bossPostalCode; const itemOptions = { customerId: customerId, postalCode: cleanPostalCode(petaPostalCode), storeName: isBOSSPETAEligible ? bossData.deliveryPointOfService.name : storeName }; return this.generatePETARequest(mergedProductCodeList, itemOptions, productList.code); }, /** * Handle to clean article id based on SAP format for example: `"ARTICLE_ID": "000000001000100630"` * param {string} productCode - product code from cart item * param {number} maxLength - maximum length based on SAP format for example this case: 18 * return {string} - clean format article id for example: `000000001000100630` */ cleanArticleId: function(productCode: string, maxLength: number): string { const pCode = productCode.length; const zeroLength = maxLength - pCode; const zero = new Array(zeroLength + 1).join('0'); return zero.concat(productCode); }, /** * Handle generate JSON string (for SAP request) filled up with required values * param itemObject - customized array object contained * param itemOptions - additional option object contained required values * param {string} cartCode - cart `code` * return {string} - final JSON string */ generatePETARequest: function (itemObject, itemOptions, cartCode): string { const customer = !!itemOptions.customerId ? itemOptions.customerId : '123456'; const cartItems = []; // return cart item format required const cartItemList = item => `{ "EXTERNAL_ID": "${item.productCode}-${itemOptions.storeName}-EA", "ARTICLE_ID": "${petaRequest.cleanArticleId(item.productCode, 18)}", "QUANTITY": "${item.quantity}", "UNIT_ISO": "EA", "SOURCE": "${itemOptions.storeName}" }`; // update cart item(s) array object `cartItems` itemObject.forEach((item) => { cartItems.push(cartItemList(item)); }); const xmlRequest = `{ "SOURCING": { "CHANNEL_ID": "YCAN", "EXEC_ALL_STRATEGIES": " ", "CART": { "EXTERNAL_ID": "${btoa(cartCode)}", "SHIPPING_METHOD": " ", "ADDITIONAL_INFO": [ { "NAME": "DAYS", "VALUE": "90" }, { "NAME": "CUSTOMER", "VALUE": "${customer}" }, { "NAME": "LOCALSTORE", "VALUE": "${itemOptions.storeName}" } ], "CART_ITEMS": [${cartItems}], "DELIVERY_ADDRESS": { "POST_CODE1": "${itemOptions.postalCode}", "POST_CODE2": "" } } } }`; return xmlRequest; } };