b23-lib 3.3.1 → 3.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Classes/Cart.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {a}from'../chunk-3TUSUSXQ.mjs';import'../chunk-B2VFJRLS.mjs';import'../chunk-HX32EJ3F.mjs';import'../chunk-G6LH7V5B.mjs';import'../chunk-SOZHGHMY.mjs';import'../chunk-RQ2FFWUW.mjs';import {b}from'../chunk-MHLRJ4IJ.mjs';import'../chunk-FCRJAAPY.mjs';import'../chunk-2B2XI6FG.mjs';import'../chunk-LI5JQ6UC.mjs';import'../chunk-QK4XJ3GB.mjs';import {a as a$1}from'../chunk-R5DM7WCD.mjs';import'../chunk-UXZBULDS.mjs';var
|
|
1
|
+
import {a}from'../chunk-3TUSUSXQ.mjs';import'../chunk-B2VFJRLS.mjs';import'../chunk-HX32EJ3F.mjs';import'../chunk-G6LH7V5B.mjs';import'../chunk-SOZHGHMY.mjs';import'../chunk-RQ2FFWUW.mjs';import {b}from'../chunk-MHLRJ4IJ.mjs';import'../chunk-FCRJAAPY.mjs';import'../chunk-2B2XI6FG.mjs';import'../chunk-LI5JQ6UC.mjs';import'../chunk-QK4XJ3GB.mjs';import {a as a$1}from'../chunk-R5DM7WCD.mjs';import'../chunk-UXZBULDS.mjs';var p=(i=>(i.ACTIVE="ACTIVE",i.FROZEN="FROZEN",i.MERGED="MERGED",i.ORDERED="ORDERED",i))(p||{}),h={expiresAtInSeconds:120*24*60*60},c=class extends a{state;expireAt;config;constructor(t,e=new Date,r=h){super(t,e),this.state=t.state,this.expireAt=t.expireAt&&typeof t.expireAt=="number"?t.expireAt:Math.floor(e.getTime()/1e3)+r.expiresAtInSeconds,this.config=r;}getState(){return this.state}getExpireAt(){return this.expireAt}isActive(){let t=Math.ceil(Date.now()/1e3);return this.state==="ACTIVE"&&this.expireAt>t}clearCartItems(){this.lineItems=[],this.charges=[],this.coupons=[],this.shippingDetails=null,this.calculateTotals();}validateLineItems(t){this.lineItems=this.lineItems.map(e=>{try{e.updateProductData(t[e.getProductKey()],this.country,this.currency);}catch(r){console.error(`Error recalculating line item ${e.getId()}:`,r),e.clearLineItem();}return e}).filter(e=>e.getId()),this.calculateTotals();}addLineItem(t,e){let r=t.getProductKey(),i=t.getSelectionAttributes(),n=b.generateSelectionAttributesKey(i),s=this.lineItems.findIndex(l=>l.getProductKey()===r&&b.generateSelectionAttributesKey(l.getSelectionAttributes())===n);return s>=0?(t.addSubItems(this.lineItems[s].getSubItems(),e),this.lineItems[s]=t):this.lineItems.push(t),this.calculateTotals(),s>=0?s:this.lineItems.length-1}updateLineItem(t,e,r){let i=this.lineItems,n=i.findIndex(s=>s.getId()===t);if(n<0)throw new a$1(t);return i[n].addSubItems([{size:e,quantity:r}],!1),this.calculateTotals(),n}removeLineItem(t){let r=this.lineItems.findIndex(i=>i.getId()===t);if(r<0)throw new a$1(t);return this.lineItems.splice(r,1),this.charges=this.charges.filter(i=>i.getLineItemId()!==t),this.calculateTotals(),r}getDetails(){return {...super.getDetails(),state:this.getState(),expireAt:this.getExpireAt()}}};export{p as CartState,h as DEFAULT_CART_CONFIG,c as default};//# sourceMappingURL=Cart.mjs.map
|
|
2
2
|
//# sourceMappingURL=Cart.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Classes/Cart.ts"],"names":["CartState","DEFAULT_CART_CONFIG","CartModel","BaseShoppingContainerModel","data","date","config","nowSeconds","products","lineItem","error","newLineItem","productKey","selectionAttributes","selectionAttributeKey","ProductModel","index","item","lineItemId","size","quantity","lineItems","LineItemNotFoundError","charge"],"mappings":"qaAKO,IAAKA,OACVA,CAAA,CAAA,MAAA,CAAS,
|
|
1
|
+
{"version":3,"sources":["../../src/Classes/Cart.ts"],"names":["CartState","DEFAULT_CART_CONFIG","CartModel","BaseShoppingContainerModel","data","date","config","nowSeconds","products","lineItem","error","newLineItem","addQuantity","productKey","selectionAttributes","selectionAttributeKey","ProductModel","index","item","lineItemId","size","quantity","lineItems","LineItemNotFoundError","charge"],"mappings":"qaAKO,IAAKA,OACVA,CAAA,CAAA,MAAA,CAAS,SACTA,CAAA,CAAA,MAAA,CAAS,SACTA,CAAA,CAAA,MAAA,CAAS,QACTA,CAAAA,CAAAA,CAAA,QAAU,SAJAA,CAAAA,CAAAA,CAAAA,EAAAA,CAAAA,EAAA,IAwBCC,CAAkC,CAAA,CAC7C,mBAAoB,GAAM,CAAA,EAAA,CAAK,EAAK,CAAA,EACtC,EAEqBC,CAArB,CAAA,cAAuCC,CAA2B,CACtD,KAAA,CACA,SACA,MAEV,CAAA,WAAA,CAAYC,CAAsBC,CAAAA,CAAAA,CAAa,IAAI,IAAQC,CAAAA,CAAAA,CAAqBL,EAAqB,CACnG,KAAA,CAAMG,EAAMC,CAAI,CAAA,CAChB,IAAK,CAAA,KAAA,CAAQD,EAAK,KAClB,CAAA,IAAA,CAAK,SAAWA,CAAK,CAAA,QAAA,EAAY,OAAOA,CAAK,CAAA,QAAA,EAAa,QAAWA,CAAAA,CAAAA,CAAK,SAAW,IAAK,CAAA,KAAA,CAAMC,EAAK,OAAQ,EAAA,CAAI,GAAI,CAAIC,CAAAA,CAAAA,CAAO,kBAChI,CAAA,IAAA,CAAK,OAASA,EAChB,CAMO,UAAsB,CAC3B,OAAO,KAAK,KACd,CAMO,WAAsB,EAAA,CAC3B,OAAO,IAAK,CAAA,QACd,CAMO,QAAoB,EAAA,CACzB,IAAMC,CAAa,CAAA,IAAA,CAAK,IAAK,CAAA,IAAA,CAAK,KAAQ,CAAA,GAAI,EAC9C,OAAO,IAAA,CAAK,QAAU,QAAqB,EAAA,IAAA,CAAK,QAAWA,CAAAA,CAC7D,CAKO,cAAiB,EAAA,CACtB,KAAK,SAAY,CAAA,GACjB,IAAK,CAAA,OAAA,CAAU,EAAC,CAChB,KAAK,OAAU,CAAA,GACf,IAAK,CAAA,eAAA,CAAkB,KACv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AoBC,EAAcC,CAAkB,CAAA,CACxE,IAAMC,CAAY,CAAA,IAAA,CAAK,UACnBL,CAAQK,CAAAA,CAAAA,CAAU,UAAWJ,CAASA,EAAAA,CAAAA,CAAK,OAAYC,GAAAA,CAAU,EAErE,GAAIF,CAAAA,CAAQ,CACV,CAAA,MAAM,IAAIM,GAAsBJ,CAAAA,CAAU,EAG5C,OAAAG,CAAAA,CAAUL,CAAK,CAAE,CAAA,WAAA,CAAY,CAAC,CAAE,KAAAG,CAAM,CAAA,QAAA,CAAAC,CAAS,CAAC,CAAA,CAAG,EAAK,CACxD,CAAA,IAAA,CAAK,eAAgB,EAAA,CAEdJ,CACT,CAQO,cAAA,CAAeE,EAAoB,CAExC,IAAMF,EADY,IAAK,CAAA,SAAA,CACC,SAAWC,CAAAA,CAAAA,EAASA,EAAK,KAAM,EAAA,GAAMC,CAAU,CAEvE,CAAA,GAAIF,EAAQ,CACV,CAAA,MAAM,IAAIM,GAAAA,CAAsBJ,CAAU,CAG5C,CAAA,OAAA,IAAA,CAAK,UAAU,MAAOF,CAAAA,CAAAA,CAAO,CAAC,CAC9B,CAAA,IAAA,CAAK,OAAU,CAAA,IAAA,CAAK,QAAQ,MAAOO,CAAAA,CAAAA,EAAUA,EAAO,aAAc,EAAA,GAAML,CAAU,CAClF,CAAA,IAAA,CAAK,eAAgB,EAAA,CAEdF,CACT,CAEA,UAAA,EAAuB,CACrB,OAAO,CACL,GAAG,KAAM,CAAA,UAAA,GACT,KAAO,CAAA,IAAA,CAAK,UACZ,CAAA,QAAA,CAAU,KAAK,WAAY,EAC7B,CACF,CACF","file":"Cart.mjs","sourcesContent":["import { LineItemNotFoundError } from \"./Error\";\r\nimport LineItemModel from \"./LineItem\";\r\nimport ProductModel from \"./Product\";\r\nimport BaseShoppingContainerModel, { BaseShoppingContainerAttributes, BaseShoppingContainerData } from \"./ShoppingContainer\";\r\n\r\nexport enum CartState {\r\n ACTIVE = \"ACTIVE\",\r\n FROZEN = \"FROZEN\",\r\n MERGED = \"MERGED\",\r\n ORDERED = \"ORDERED\"\r\n}\r\n\r\n/**\r\n * Input attributes for creating or updating a CartModel.\r\n */\r\nexport type CartAttributes = BaseShoppingContainerAttributes & {\r\n state: CartState;\r\n expireAt: number;\r\n};\r\n\r\nexport type CartData = BaseShoppingContainerData & {\r\n state: CartState;\r\n expireAt: number;\r\n};\r\n\r\nexport type CartConfig = {\r\n expiresAtInSeconds: number;\r\n}\r\n\r\nexport const DEFAULT_CART_CONFIG: CartConfig = {\r\n expiresAtInSeconds: 120 * 24 * 60 * 60\r\n}\r\n\r\nexport default class CartModel extends BaseShoppingContainerModel {\r\n protected state: CartState;\r\n protected expireAt: number;\r\n protected config: CartConfig;\r\n\r\n constructor(data: CartAttributes, date: Date = new Date(), config: CartConfig = DEFAULT_CART_CONFIG) {\r\n super(data, date);\r\n this.state = data.state;\r\n this.expireAt = data.expireAt && typeof data.expireAt === 'number' ? data.expireAt : Math.floor(date.getTime() / 1000) + config.expiresAtInSeconds;\r\n this.config = config;\r\n }\r\n\r\n /**\r\n * Gets the current state of the cart (e.g., ACTIVE, ORDERED).\r\n * @returns The CartState enum value.\r\n */\r\n public getState(): CartState {\r\n return this.state;\r\n }\r\n\r\n /**\r\n * Gets the timestamp when the cart expires.\r\n * @returns The expiration timestamp in seconds (Unix epoch).\r\n */\r\n public getExpireAt(): number {\r\n return this.expireAt;\r\n }\r\n\r\n /**\r\n * Checks if the cart is currently active and not expired.\r\n * @returns True if active and not expired, false otherwise.\r\n */\r\n public isActive(): boolean {\r\n const nowSeconds = Math.ceil(Date.now() / 1000);\r\n return this.state === CartState.ACTIVE && (this.expireAt > nowSeconds);\r\n }\r\n\r\n /**\r\n * Clears all line items, coupons, and shipping details from the cart and resets totals.\r\n */\r\n public clearCartItems() {\r\n this.lineItems = [];\r\n this.charges = [];\r\n this.coupons = [];\r\n this.shippingDetails = null;\r\n this.calculateTotals();\r\n }\r\n\r\n /**\r\n * Validates line items against the current product catalog.\r\n * Updates product data for each item and removes invalid items.\r\n * @param products - A record map of ProductModels keyed by product ID/key.\r\n */\r\n public validateLineItems(products: Record<string, ProductModel>): void {\r\n this.lineItems = this.lineItems.map(lineItem => {\r\n try {\r\n lineItem.updateProductData(products[lineItem.getProductKey()], this.country, this.currency);\r\n } catch (error) {\r\n console.error(`Error recalculating line item ${lineItem.getId()}:`, error);\r\n lineItem.clearLineItem();\r\n }\r\n return lineItem;\r\n }).filter(lineItem => lineItem.getId());\r\n\r\n this.calculateTotals();\r\n }\r\n\r\n /**\r\n * Adds a new line item or updates the quantity of an existing one.\r\n * @param newLineItem The LineItemModel to add.\r\n * @param addQuantity If true, adds the quantity from `subItem` to the existing quantity.\r\n * If false, replaces the existing quantity with the one from `subItem`.\r\n * @returns The index of the added/updated line item in the internal array.\r\n */\r\n public addLineItem(newLineItem: LineItemModel, addQuantity: boolean) {\r\n const productKey = newLineItem.getProductKey();\r\n const selectionAttributes = newLineItem.getSelectionAttributes();\r\n const selectionAttributeKey = ProductModel.generateSelectionAttributesKey(selectionAttributes);\r\n let index = this.lineItems.findIndex(\r\n (item) => item.getProductKey() === productKey && ProductModel.generateSelectionAttributesKey(item.getSelectionAttributes()) === selectionAttributeKey\r\n );\r\n\r\n if (index >= 0) {\r\n newLineItem.addSubItems(this.lineItems[index].getSubItems(), addQuantity);\r\n this.lineItems[index] = newLineItem;\r\n } else {\r\n this.lineItems.push(newLineItem);\r\n }\r\n\r\n this.calculateTotals();\r\n return index >= 0 ? index : this.lineItems.length - 1;\r\n }\r\n\r\n /**\r\n * Updates the quantity for a specific size within a line item.\r\n * @param lineItemId The ID of the LineItemModel to update.\r\n * @param size The size identifier within the line item.\r\n * @param quantity The new quantity for the size (0 to remove).\r\n * @returns The index of the updated line item.\r\n * @throws {Error} If the line item is not found.\r\n */\r\n public updateLineItem(lineItemId: string, size: string, quantity: number) {\r\n const lineItems = this.lineItems;\r\n let index = lineItems.findIndex((item) => item.getId() === lineItemId);\r\n\r\n if (index < 0) {\r\n throw new LineItemNotFoundError(lineItemId);\r\n }\r\n\r\n lineItems[index].addSubItems([{ size, quantity }], false);\r\n this.calculateTotals();\r\n\r\n return index;\r\n }\r\n\r\n /**\r\n * Removes a line item completely from the cart.\r\n * @param lineItemId The ID of the LineItemModel to remove.\r\n * @returns The index the item previously occupied.\r\n * @throws {Error} If the line item is not found.\r\n */\r\n public removeLineItem(lineItemId: string) {\r\n const lineItems = this.lineItems;\r\n const index = lineItems.findIndex((item) => item.getId() === lineItemId);\r\n\r\n if (index < 0) {\r\n throw new LineItemNotFoundError(lineItemId);\r\n }\r\n\r\n this.lineItems.splice(index, 1);\r\n this.charges = this.charges.filter(charge => charge.getLineItemId() !== lineItemId);\r\n this.calculateTotals();\r\n\r\n return index;\r\n }\r\n\r\n getDetails(): CartData {\r\n return {\r\n ...super.getDetails(),\r\n state: this.getState(),\r\n expireAt: this.getExpireAt(),\r\n }\r\n }\r\n}\r\n"]}
|