UNPKG

@chiper-inc/ecommerce-lib

Version:
203 lines 8.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Promotion = void 0; const event_1 = require("../event"); const item_1 = require("./item"); const price_1 = require("./price"); const product_1 = require("./product"); class Promotion extends item_1.Item { constructor({ stock, multipleQuantity, name, warehouseId, image, id, packagingType, products, termsUrl, banner, quantity, maxQuantity, }, rate) { super(item_1.ItemType.COMBO, { stock, multipleQuantity, name, image, id, packagingType, warehouseId, measurement: { quantity: 0, unit: "" } }, rate); this.products = products; this.termsUrl = termsUrl; this.banner = banner; this._maxQuantity = maxQuantity; this.quantity = quantity; } get stock() { if (this.products && this.products.length > 0 && this.products.every((p) => p.stock !== undefined)) { return Math.min(...this.products.map((p) => Math.floor(p.stock / p.multipleQuantity))); } return this._stock; } set stock(stock) { this._stock = stock; for (const product of this.products) { product.stock = stock * product.multipleQuantity; } if (this.quantity && this.stock < this.quantity) { this.emit(event_1.CartEvent.NEWS, { id: this.id, cardType: this.stock === 0 ? "unavailable" : "lowStock", productType: this.type, medium: this.image, name: this.name, packagingType: this.packagingType, previousTotal: this.regularPrice, quantity: this.quantity, stock: this.stock, total: this.regularPrice, warehouseId: this.warehouseId, }); } } get subtotal() { if (this.quantity == null) return null; return +this.products .reduce((total, product) => { var _a; return total + ((_a = product.subtotal) !== null && _a !== void 0 ? _a : 0); }, 0) .toFixed(2); } get total() { if (this.quantity == null) return null; return +this.products.reduce((total, product) => { var _a; return total + ((_a = product.total) !== null && _a !== void 0 ? _a : 0); }, 0).toFixed(2); } get totalDollars() { if (this.quantity == null) return null; return +(this.products.reduce((total, product) => { var _a; return total + ((_a = product.total) !== null && _a !== void 0 ? _a : 0); }, 0) * this.rate).toFixed(2); } increase() { this.quantity = this.quantity == null ? this.minQuantity : this.quantity + this.multipleQuantity; } decrease() { this.quantity = this.quantity == null ? this.minQuantity : this.quantity - this.multipleQuantity; } set quantity(quantity) { super.quantity = quantity; if (quantity != null) { this.products.forEach((product) => { product.quantity = quantity * product.multipleQuantity; }); } } get quantity() { return super.quantity; } get regularPrice() { return +this.products.reduce((total, product) => total + product.multipleQuantity * product.regularPrice, 0).toFixed(2); } get regularPriceDolar() { return +(this.products.reduce((total, product) => total + product.multipleQuantity * product.regularPrice, 0) * this.rate).toFixed(2); } get price() { return +this.products.reduce((total, product) => total + product.multipleQuantity * product.price, 0).toFixed(2); } get discountedPrice() { if (this.products.some((product) => product.discountedPrice !== undefined)) { return +this.products.reduce((total, product) => { var _a; return (total + product.multipleQuantity * ((_a = product.discountedPrice) !== null && _a !== void 0 ? _a : product.regularPrice)); }, 0).toFixed(2); } return undefined; } get chiperPrice() { return +this.products.reduce((total, product) => (product.multipleQuantity * (product.chiperPrice || product.regularPrice) + total), 0).toFixed(2); } get maxQuantity() { if (this._maxQuantity) return this._maxQuantity; return this.products.reduce((total, product) => { let max = product.maxQuantity; if (max === undefined) return total; max = +(max / product.multipleQuantity).toFixed(0); return total === undefined ? max : Math.min(total, max); }, undefined); } static fromShopCart({ products, stock, quantity, name, medium, id, warehouseId, packagingType, termsUrl, banner, maximumQuantity, }, rate) { return new Promotion({ products: products.map((product) => { var _a; return product_1.Product.fromShopCart(Object.assign(product, { multipleQuantity: product.quantity, stock: (_a = product.stock) !== null && _a !== void 0 ? _a : stock * product.quantity, }), rate); }), warehouseId, stock, quantity, name, image: medium, id, packagingType, termsUrl, banner, maxQuantity: maximumQuantity, measurement: { quantity: 0, unit: "" } }, rate); } static from({ products, quantity, stock, name, medium, id, warehouseId, packagingType, termsUrl, maximumQuantity, maxQuantity, banner, }, rate) { return new Promotion({ products: products.map((product) => { var _a, _b; return new product_1.Product(Object.assign(Object.assign({}, product), { prices: product.prices.map((p) => new price_1.Price(Object.assign(Object.assign({}, p), { expireDate: p.expireDate && new Date(p.expireDate) }))), multipleQuantity: (_a = product.multipleQuantity) !== null && _a !== void 0 ? _a : Math.max(product.quantity / quantity, 1), stock: (_b = product.stock) !== null && _b !== void 0 ? _b : stock * product.quantity }), rate); }), stock, quantity, name, image: medium, id, warehouseId, packagingType, termsUrl, maxQuantity: maxQuantity !== null && maxQuantity !== void 0 ? maxQuantity : maximumQuantity, banner, measurement: { quantity: 0, unit: "" } }, rate); } static fromCatalog({ detailsDescription, stock, name, medium, id, warehouseId, packagingType, termsUrl, banner, quantity, maxQuantity, }, rate) { return new Promotion({ products: detailsDescription.map((detail) => product_1.Product.fromPromoDetail(detail, rate)), stock, name, image: medium, id, warehouseId, packagingType, termsUrl, banner, quantity, maxQuantity, measurement: { quantity: 0, unit: "" } }, rate); } clone() { return new Promotion({ id: this.id, warehouseId: this.warehouseId, name: this.name, image: this.image, packagingType: this.packagingType, products: this.products, quantity: this.quantity, stock: this.stock, termsUrl: this.termsUrl, banner: this.banner, minQuantity: this.minQuantity, multipleQuantity: this.multipleQuantity, measurement: { quantity: 0, unit: "" } }, this.rate); } // create toJSON function toJSON() { return Object.assign(Object.assign({}, super.toJSON()), { products: this.products.map((product) => product.toJSON()), termsUrl: this.termsUrl, banner: this.banner, citrusAdId: "" }); } } exports.Promotion = Promotion; //# sourceMappingURL=promotion.js.map