UNPKG

@chiper-inc/ecommerce-lib

Version:
142 lines 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MaxQuantityHelper = exports.Measurement = exports.ItemQuantityError = exports.Cart = exports.Promotion = exports.Product = exports.Item = exports.Price = void 0; const price_1 = require("./price"); Object.defineProperty(exports, "Price", { enumerable: true, get: function () { return price_1.Price; } }); const item_1 = require("./item"); Object.defineProperty(exports, "Item", { enumerable: true, get: function () { return item_1.Item; } }); const product_1 = require("./product"); Object.defineProperty(exports, "Product", { enumerable: true, get: function () { return product_1.Product; } }); Object.defineProperty(exports, "Measurement", { enumerable: true, get: function () { return product_1.Measurement; } }); const promotion_1 = require("./promotion"); Object.defineProperty(exports, "Promotion", { enumerable: true, get: function () { return promotion_1.Promotion; } }); const itemQuantityError_1 = require("./itemQuantityError"); Object.defineProperty(exports, "ItemQuantityError", { enumerable: true, get: function () { return itemQuantityError_1.ItemQuantityError; } }); const helpers_1 = require("./helpers"); Object.defineProperty(exports, "MaxQuantityHelper", { enumerable: true, get: function () { return helpers_1.MaxQuantityHelper; } }); class Cart { constructor({ items, storeId, warehouseId, status, locationId, rate }) { this.news = []; this._items = items; this.storeId = storeId; this.warehouseId = warehouseId; this.status = status; this.locationId = locationId; this._rate = 1; if (rate) { this._rate = rate; } } get rate() { return this._rate; } get items() { return this._items; } get totalDollars() { return +this.items .reduce((total, item) => { var _a; return total + ((_a = item.totalDollars) !== null && _a !== void 0 ? _a : 0); }, 0) .toFixed(2); } get total() { return +this.items .reduce((total, item) => { var _a; return total + ((_a = item.total) !== null && _a !== void 0 ? _a : 0); }, 0) .toFixed(2); } get subtotal() { return +this.items .reduce((subtotal, item) => { var _a; return subtotal + ((_a = item.subtotal) !== null && _a !== void 0 ? _a : 0); }, 0) .toFixed(2); } removeItem(id) { this._items = this._items.filter((item) => item.id !== id); } addItem(item) { this._items.push(item); } get margin() { const profit = +this.items .reduce((total, item) => ((item.price - item.chiperPrice) * (item.quantity || 0) + total), 0); return +(profit / this.subtotal).toFixed(2); } getCartForWarehouse(warehouseId) { return new Cart({ items: this.itemsByWarehouseId(warehouseId), storeId: this.storeId, warehouseId: warehouseId, status: this.status, locationId: this.locationId, }); } itemsByWarehouseId(warehouseId) { return this._items.filter((item) => item.warehouseId === warehouseId); } /* eslint-disable @typescript-eslint/no-explicit-any */ static from({ items, storeId, warehouseId, status, locationId }, rate, isBackend, isOffline) { return new Cart({ items: items.map((itemRaw) => { let item; switch (itemRaw.type) { case item_1.ItemType.PRODUCT: { item = product_1.Product.from(itemRaw, rate, isBackend, isOffline); break; } case item_1.ItemType.COMBO: item = promotion_1.Promotion.from(itemRaw, rate); break; default: throw new Error("invalid productType: " + itemRaw.productType); } return item; }), storeId, warehouseId, status, locationId, rate }); } static fromShopCart({ carts, storeId, warehouseId, status, locationId, rate }) { return new Cart({ items: carts[0].items.map((itemRaw) => { let item; switch (itemRaw.productType) { case item_1.ItemType.PRODUCT: { item = product_1.Product.fromShopCart(Object.assign({}, itemRaw, { prices: itemRaw.products[0].prices }), rate); break; } case item_1.ItemType.COMBO: item = promotion_1.Promotion.fromShopCart(itemRaw, rate); break; default: throw new Error("invalid productType: " + itemRaw.productType); } return item; }), storeId, warehouseId, status, locationId, rate }); } // toJSON function toJSON() { return { items: this.items.map((item) => item.toJSON()), storeId: this.storeId, warehouseId: this.warehouseId, status: this.status, locationId: this.locationId, total: this.total, totalDollars: this.totalDollars, subtotal: this.subtotal, news: this.news, rate: this.rate }; } } exports.Cart = Cart; //# sourceMappingURL=index.js.map