{"version":3,"sources":["webpack://casperEventHub/./src/basketTotal.ts","webpack://casperEventHub/./src/eCapi.ts","webpack://casperEventHub/./src/basketObserver.ts"],"names":["BasketTotal","addPredicates","primitiveConditions","conditions","isObject","Object","isString","Error","this","quantity","price","matchingCondition","primitiveCondition","getState","isValid","totals","then","value","Promise","getProductsTotals","filter","basketTotalsValues","body","result","productsDetails","oneOf","product","BasketObserver","recover","args","i","arguments","f","getUserDetails","userDetails","window","JSON","status","hasCookie","getProductsDetails","napiCatalog","productsProperties","productProperties","fields","categories","technologies","cupSizes","range","isStandingOrders","category","indexOf","processCategories","getProductsProperties","basketProducts","basketProduct","unitPrice","merged","getStandingOrders","getSubscriptions","subs","sub","subsDetails","marketSub","invoiceProduct","console","name","e","off","Rule"],"mappings":"qHAoFO,wE,mVAzEcA,E,8RAIZC,cAAP,YAAmD,WAC3CC,EAAkCC,EAAA,QACtC,mBACGC,gBAAuBC,eAAxB,QACAC,YAFF,MAIF,OAAIJ,SAsBF,MAAM,IAAIK,MAAV,sEArBAC,KAAA,gBACE,iBACKN,EAAA,KAAwB,YAAwB,QACzCO,EADyC,WAC/BC,EAD+B,QAE3CC,EAAN,GAYA,OAXA,MAAIF,IACFE,cAEF,MAAID,IACFC,kBAEMC,EAAR,gBACQA,EAAR,MACIP,sBAAJ,IACE,YAEF,Q,EAUGQ,S,eAAsC,kBAoC9C,EAnCkB,oBAAuB,EADK,QAoC9C,EApC8C,YAEjD,MAAO,CACLC,QAAS,WAAe,CADnB,IAELC,WAiCL,EACQC,EAAOA,EAAH,GAAX,GAEG,GAAWC,EAAf,OACCA,EAAQC,gBAARD,IAEMD,EAAOC,OAAH,GAAX,G,kCAPM,W,EA7BCE,kB,SACJC,Q,IAAAA,MAA2B,M,IAE3B,IAAMC,EAAoC,CACxCX,MADwC,EAExCD,SAAU,GAHY,OAsdrB,gBACN,KACC,OAAOO,EAAOA,EAAKM,KAAUA,IAE9B,IACC,IAAIC,EAASL,gBAAgBI,KAC7B,OAAON,E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R,E,oBAEnB,cAAsE,M,MACpE,qB,2HADoE,I,OAAA,G,EAAA,kB,wFAEpE,IACE,eAAoBQ,sBAP1B,cAOM,GACA,MAAO+B,IAJ2D,S,mGAQ/DC,IAAP,WACM3D,KAAJ,cACE2B,uBAdN,cAc4C3B,KAAtC2B,e,GAZsCiC","file":"BasketTotal.js","sourcesContent":["import BasketObserver from './basketObserver'\nimport { getProductsProperties, IProductProps } from './eCapi'\nimport {\n Possibilities,\n Conditions,\n Condition,\n IUnknownObject,\n Possibility,\n} from './types'\nimport { isObject, isString, allOf, oneOf } from './predicates'\n\nexport default class BasketTotal extends BasketObserver {\n private filter?: Condition\n\n //no more predicate condition support, and limited to one condition\n public addPredicates(conditions: Conditions): void {\n const primitiveConditions: Conditions = conditions.filter(\n (predicate) =>\n (isObject(predicate) && Object.keys(predicate).length) ||\n isString(predicate)\n )\n if (primitiveConditions.length === 1) {\n this.predicates.push(\n allOf(\n ...primitiveConditions.map((primitiveCondition) => {\n const { quantity, price } = primitiveCondition as IBasketTotals\n const matchingCondition: IBasketTotals = {}\n if (quantity != undefined) {\n matchingCondition.quantity = quantity\n }\n if (price != undefined) {\n matchingCondition.price = price\n }\n delete (primitiveCondition as IBasketTotals).quantity\n delete (primitiveCondition as IBasketTotals).price\n if (Object.keys(primitiveCondition).length > 0) {\n this.filter = primitiveCondition\n }\n return matchingCondition\n })\n )\n )\n } else {\n throw new Error(\n 'Error - Casper - basketTotalIs accepts only one argument/condition'\n )\n }\n }\n public async getState(): Promise<BasketTotalState> {\n const totals = await this.getProductsTotals(this.filter)\n return {\n isValid: this.validate(([totals] as unknown) as Possibilities),\n totals,\n }\n }\n async getProductsTotals(\n filter: Condition | null = null\n ): Promise<IBasketTotals> {\n const basketTotalsValues: IBasketTotals = {\n price: 0,\n quantity: 0,\n }\n const productsDetails: IProductProps[] = await getProductsProperties()\n productsDetails.forEach((product) => {\n if (\n (filter && oneOf(filter)([(product as unknown) as Possibility])) ||\n filter === null\n ) {\n if (basketTotalsValues.quantity !== undefined) {\n basketTotalsValues.quantity += product.quantity * product.unitQuantity\n }\n if (basketTotalsValues.price !== undefined) {\n basketTotalsValues.price += product.price * product.quantity\n }\n }\n })\n return basketTotalsValues\n }\n}\n\nexport interface IBasketTotals extends IUnknownObject {\n quantity?: number\n price?: number\n}\n\ninterface BasketTotalState {\n isValid: boolean\n totals: IBasketTotals\n}\n","import {\n Catalog,\n CartProducts,\n ICategory,\n ProductDetails,\n CustomerDetails,\n} from './types'\nimport { IStandingOrder } from './standingOrders'\nimport { CustomerSubscription, MergedSubscriptions } from './subscriptions'\n\nlet userDetails: CustomerDetails | null\n\nexport function clearCustomerDetails(): void {\n userDetails = null\n}\n\nexport async function getSubscriptions(): Promise<MergedSubscriptions[]> {\n try {\n const subs: CustomerSubscription[] = await window.napi\n .subscription()\n .getSubscriptions()\n const subsDetails = await window.napi.market().getSubscriptions()\n return (await Promise.all(\n subs.map(async (sub) => {\n let product\n if (sub.promotionalProductId) {\n product = await window.napi\n .catalog()\n .getProduct(sub.promotionalProductId)\n }\n const subDetails = subsDetails.subscriptionProfiles.find(\n (marketSub) => marketSub.id === sub.profileId\n )\n return { ...subDetails, ...sub, invoiceProduct: product }\n })\n )) as MergedSubscriptions[]\n } catch (e) {\n console.log('error with experience', e)\n return [] as MergedSubscriptions[]\n }\n}\n\nexport async function getStandingOrders(): Promise<IStandingOrder[]> {\n try {\n return await window.napi.standingOrders().getOrders('Responsive')\n } catch (e) {\n return [] as IStandingOrder[]\n }\n}\n\nfunction processCategories(categories: ICategory[]): IProductFields {\n const technologies = []\n const cupSizes = []\n let range: IProductFields['range'] = null\n let isStandingOrders = false\n for (const category of categories) {\n if (['original', 'vertuo'].indexOf(category.code) > -1) {\n technologies.push(category.code)\n } else if (\n category.code.indexOf('capsule-range') > -1 &&\n category.code.indexOf('-Mobile') === -1\n ) {\n range = category.code.replace('capsule-range-', '')\n } else if (category.code.indexOf('capsule-cupSize') > -1) {\n cupSizes.push(category.code.replace('capsule-cupSize-', ''))\n } else if (category.code === 'standing-orders') {\n isStandingOrders = true\n }\n }\n return {\n cupSizes,\n isStandingOrders,\n range,\n technologies,\n }\n}\n\nexport async function getProductsProperties(): Promise<IProductProps[]> {\n const napiCatalog: Catalog = window.napi.catalog()\n const basketProducts: CartProducts[] = await window.napi.cart().read()\n const productsDetails: IProductProps[] = []\n await Promise.all(\n basketProducts.map(async (basketProduct) => {\n try {\n const productDetail = await napiCatalog.getProduct(\n basketProduct.productId\n )\n const { unitPrice } = basketProduct\n const merged = {\n ...basketProduct,\n ...productDetail,\n }\n if (unitPrice !== 0 && merged.price === 0) {\n merged.price = unitPrice\n }\n productsDetails.push(merged)\n } catch (e) {\n return\n }\n })\n )\n return productsDetails\n}\n\nexport async function getProductsDetails(): Promise<IProduct[]> {\n const napiCatalog: Catalog = window.napi.catalog()\n const productsProperties: IProductProps[] = await getProductsProperties()\n const productsDetails: IProduct[] = []\n await Promise.all(\n productsProperties.map(async (productProperties) => {\n try {\n const categories: ICategory[] = await Promise.all(\n productProperties.supercategories.map(napiCatalog.getCategory)\n )\n const fields: IProductFields = processCategories(categories)\n productsDetails.push({\n ...productProperties,\n ...fields,\n })\n } catch (e) {\n return\n }\n })\n )\n return productsDetails\n}\n\nexport async function getUserDetails(): Promise<CustomerDetails> {\n if (userDetails == null) {\n try {\n userDetails = JSON.parse(\n JSON.stringify(await window.napi.customer().read())\n ) as CustomerDetails\n if (userDetails.preferredTechnology !== undefined) {\n userDetails.preferredTechnology = userDetails.preferredTechnology.slice(\n 30\n )\n }\n } catch (error) {\n userDetails = {\n status: 'anonymous',\n }\n if (\n window.localStorage.getItem('loggedTimestamp') ||\n window.sessionStorage.getItem('loggedTimestamp') ||\n hasCookie('ecapiLoginToken') ||\n hasCookie('autoLoginToken') ||\n hasCookie('nesLoginToken')\n ) {\n userDetails.status = 'not-logged-in'\n }\n }\n }\n return userDetails\n}\n\nfunction hasCookie(name: string) {\n return window.document.cookie.indexOf(name + '=') > -1\n}\n\nexport interface IProductProps extends ProductDetails, CartProducts {\n quantity: number\n price: number\n sku?: string\n unitPrice: number\n}\n\nexport interface IProduct extends IProductFields, ProductDetails, CartProducts {\n quantity: number\n price: number\n sku?: string\n unitPrice: number\n}\n\nexport interface IProductFields {\n range: string | null\n technologies: string[]\n cupSizes: string[]\n isStandingOrders: boolean\n}\n","import Rule from './rule'\nimport { ITargetingRulesEvaluationResult } from './experience'\nconst napiDataEvent = 'cart.update'\n\nexport default class BasketObserver extends Rule {\n private eventHandler?: () => void\n constructor(callback: () => Promise<ITargetingRulesEvaluationResult>) {\n super()\n try {\n this.eventHandler = window.napi.data().on(napiDataEvent, callback)\n } catch (e) {\n // prevent error\n }\n }\n public off(): void {\n if (this.eventHandler) {\n window.napi.data().off(napiDataEvent, this.eventHandler)\n }\n }\n}\n"],"sourceRoot":""}