@chiper-inc/ecommerce-lib
Version:
Chiper Inc Ecommerce Lib
344 lines • 13.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Product = exports.Measurement = void 0;
const item_1 = require("./item");
const price_1 = require("./price");
const event_1 = require("../event");
class Measurement {
constructor(unit, quantity) {
this.unit = unit;
this.quantity = quantity;
}
toJSON() {
return {
unit: this.unit,
quantity: this.quantity,
};
}
}
exports.Measurement = Measurement;
class Product extends item_1.Item {
constructor({ stock, quantity, multipleQuantity, name, image, id, warehouseId, packagingType, prices, measurement, sku, sponsored, referenceId, ledgerAccount, description, lowStock, brandName, minQuantity, maxQuantity, retailPrice, referencePromotionId, chiperPrice, oldPrice, clusterCode, isKvi, partnerId, citrusAdId }, rate, isBackend, isOffline, offlinePrices) {
super(item_1.ItemType.PRODUCT, {
stock,
multipleQuantity,
name,
image,
warehouseId,
id,
packagingType,
minQuantity,
measurement
}, rate, isBackend, isOffline);
this.citrusAdId = citrusAdId;
if (offlinePrices) {
this._prices = offlinePrices;
}
else {
this._prices = prices;
}
this.retailPrice = retailPrice;
this.measurement = measurement;
this.sku = sku;
this.sponsored = sponsored;
this.referenceId = referenceId;
this.ledgerAccount = ledgerAccount;
this.description = description;
this.lowStock = lowStock;
this.brandName = brandName;
this.minQuantity = minQuantity;
this._maxQuantity = maxQuantity;
this.quantity = quantity;
this.referencePromotionId = referencePromotionId;
this._chiperPrice = chiperPrice;
this.oldPrice = oldPrice;
this.clusterCode = clusterCode;
this.isKvi = isKvi;
this.partnerId = partnerId;
}
get prices() {
return this._prices;
}
set prices(prices) {
const currentPrice = this.oldPrice || this.prices[0].total;
const newPrice = prices[0].total;
this._prices = prices;
this.quantity = this._quantity; // update prices quantities
if (newPrice > currentPrice) {
let dif = newPrice - currentPrice;
this.emit(event_1.CartEvent.NEWS, {
id: this.id,
cardType: "changePrice",
productType: this.type,
medium: this.image,
name: this.name,
packagingType: this.packagingType,
previousTotal: currentPrice,
quantity: this.quantity,
stock: this.stock,
total: newPrice,
warehouseId: this.warehouseId,
meta: (dif < 0) ? "INCREASE" : "DECREASED"
});
}
}
get maxQuantity() {
let sum = 0;
for (const price of this.prices) {
if (!price.maxQuantity && price.maxQuantity !== 0) {
return undefined;
}
sum += price.maxQuantity;
}
return sum;
}
get total() {
if (this.quantity == null)
return null;
return +this.prices
.reduce((total, price) => { var _a; return total + ((_a = price.extendedTotal) !== null && _a !== void 0 ? _a : 0); }, 0)
.toFixed(2);
}
get subtotal() {
if (this.quantity == null)
return null;
return +this.prices
.reduce((total, price) => { var _a; return total + ((_a = price.extendedSubtotal) !== null && _a !== void 0 ? _a : 0); }, 0)
.toFixed(2);
}
/**
* Set the quantity and updates the prices quantities
*
* @param quantity the quantity
*/
set quantity(quantity) {
var _a;
super.quantity = quantity;
if (quantity === null || !((_a = this.prices) === null || _a === void 0 ? void 0 : _a.length))
return;
for (const [idx, price] of this.prices.entries()) {
if (price.isExpired || quantity === undefined) {
price.quantity = 0;
continue;
}
const before = price.quantity;
price.quantity = price.maxQuantity
? Math.min(price.maxQuantity, quantity)
: quantity;
if (idx > 0 && before === 0 && price.quantity > 0) {
this.emit(event_1.CartEvent.PRICE_SCALE_UP, this.prices[idx - 1], price);
}
quantity -= price.quantity;
}
}
get quantity() {
return super.quantity;
}
static from({ id, name, stock, warehouseId, medium, packagingType, multipleQuantity, prices, quantity, measurement, sku, sponsored, citrusAdId, referenceId, ledgerAccount, description, lowStock, brandName, minQuantity, maximumQuantity, chiperPrice, oldPrice, clusterCode, isKvi, partnerId, }, rate, isBackend, isOffline, offlinePrices) {
const pricesToInsert = (!offlinePrices) ? prices : offlinePrices;
return new Product({
id,
warehouseId,
name,
stock,
image: medium,
packagingType,
minQuantity,
retailPrice: pricesToInsert[0].customerTotal,
multipleQuantity,
prices: pricesToInsert.map((p) => new price_1.Price(Object.assign(Object.assign({}, p), { expireDate: p.expireDate && new Date(p.expireDate) }))),
quantity,
measurement,
sku,
sponsored,
citrusAdId,
referenceId,
ledgerAccount,
description,
lowStock,
brandName,
maxQuantity: maximumQuantity,
chiperPrice,
oldPrice,
clusterCode,
isKvi,
partnerId,
}, rate, isBackend, isOffline, offlinePrices);
}
static fromShopCart({ multipleQuantity, prices, discountedTotal, managerPrice, discountedSubtotal, stock, quantity, medium, id, warehouseId, packagingType, sku, sponsored, citrusAdId, referenceId, ledgerAccount, description, lowStock, customerTotal, brandName, minQuantity, customerMeasurement, customerMeasurementUnit, name, }, rate) {
if (prices) {
multipleQuantity =
multipleQuantity || prices[0].values[0].multipleQuantity;
if (discountedTotal) {
prices[0].values[0].discountedPrice = managerPrice;
prices[0].values[0].discountedSubtotal = discountedSubtotal;
prices[0].values[0].discountedTotal = discountedTotal;
}
}
return new Product({
name,
warehouseId,
prices: price_1.Price.fromPricing(prices),
stock,
quantity,
multipleQuantity,
maxQuantity: prices[0].maximumQuantity,
retailPrice: customerTotal,
image: medium,
id,
packagingType,
measurement: new Measurement(customerMeasurementUnit !== null && customerMeasurementUnit !== void 0 ? customerMeasurementUnit : "", customerMeasurement !== null && customerMeasurement !== void 0 ? customerMeasurement : 1),
sku,
sponsored,
citrusAdId,
referenceId,
ledgerAccount,
description,
lowStock,
brandName,
minQuantity
}, rate);
}
static fromCatalog({ stock, multipleQuantity, name, medium, id, warehouseId, packagingType, prices, primaryPackaging, sku, sponsored, referenceId, ledgerAccount, description, customerPrice, lowStock = false, brandName, minQuantity = 1, maxQuantity, maximumQuantity, discountedMaximumQuantity, scheduleEndDate, quantity, isKvi = false, partnerId, citrusAdId }, rate) {
return new Product({
stock,
multipleQuantity,
name,
image: medium,
id,
warehouseId,
packagingType,
prices: price_1.Price.fromCatalogue({
prices,
maximumQuantity,
discountedMaximumQuantity,
scheduleEndDate,
}),
retailPrice: customerPrice,
measurement: primaryPackaging
? new Measurement(primaryPackaging.measurementUnit, primaryPackaging.measurement)
: new Measurement("", 1),
sku,
sponsored,
referenceId,
ledgerAccount,
description,
lowStock,
brandName,
minQuantity,
maxQuantity: maxQuantity !== null && maxQuantity !== void 0 ? maxQuantity : maximumQuantity,
quantity,
isKvi,
partnerId,
citrusAdId
}, rate);
}
static fromPromoDetail({ id, multipleQuantity, sku, displayName, measurementUnit, salesUnit, packagingType, prices, warehouseId, stock, image, medium, partnerId }, rate) {
return new Product({
stock,
multipleQuantity,
name: displayName,
image: image || medium,
id,
warehouseId,
packagingType,
prices: prices.map((p) => new price_1.Price(p)),
measurement: new Measurement(measurementUnit, salesUnit),
sku,
referenceId: 0,
ledgerAccount: "0",
description: displayName,
lowStock: false,
brandName: "Unknown",
minQuantity: 1,
maxQuantity: undefined,
retailPrice: 0,
sponsored: false,
citrusAdId: undefined,
isKvi: false,
partnerId,
}, rate);
}
get discountExpirationDate() {
const price = this.prices.find((price) => price.discount);
return price === null || price === void 0 ? void 0 : price.expireDate;
}
get regularPrice() {
const price = this.prices.find((price) => !price.discount);
if (price === undefined)
throw new Error("Product has no regular price");
return price.total;
}
get totalDollars() {
if (this.quantity == null)
return null;
return +(this.prices
.reduce((total, price) => { var _a; return total + ((_a = price.extendedTotal) !== null && _a !== void 0 ? _a : 0); }, 0) * this.rate)
.toFixed(2);
}
get regularPriceDolar() {
const price = this.prices.find((price) => !price.discount);
if (price === undefined)
throw new Error("Product has no regular price");
return price.total * this.rate;
}
get discountedPrice() {
var _a;
return (_a = this.prices.find((p) => p.discount !== null && p.discount !== undefined && !p.isExpired)) === null || _a === void 0 ? void 0 : _a.total;
}
/** price without taxes */
get price() {
const price = this.prices.find((price) => !price.discount);
if (price === undefined)
throw new Error("Product has no regular price");
return price.subtotal;
}
get chiperPrice() {
return this._chiperPrice || this.regularPrice;
}
increase() {
this.quantity =
this.quantity == null
? this.minQuantity
: this.quantity + this.multipleQuantity;
}
decrease() {
this.quantity =
this.quantity == null
? this.minQuantity
: this.quantity - this.multipleQuantity;
}
clone() {
return new Product({
id: this.id,
warehouseId: this.warehouseId,
name: this.name,
brandName: this.brandName,
description: this.description,
prices: this.prices,
retailPrice: this.retailPrice,
ledgerAccount: this.ledgerAccount,
lowStock: this.lowStock,
maxQuantity: this.maxQuantity,
measurement: this.measurement,
minQuantity: this.minQuantity,
multipleQuantity: this.multipleQuantity,
packagingType: this.packagingType,
quantity: this.quantity,
referenceId: this.referenceId,
sku: this.sku,
image: this.image,
stock: this.stock,
sponsored: this.sponsored,
isKvi: this.isKvi,
partnerId: this.partnerId,
citrusAdId: this.citrusAdId
}, this.rate);
}
// create toJSON method
toJSON() {
return Object.assign(Object.assign({}, super.toJSON()), { brandName: this.brandName, description: this.description, prices: this.prices.map((prices) => prices.toJSON(this.retailPrice)), retailPrice: this.retailPrice, ledgerAccount: this.ledgerAccount, lowStock: this.lowStock, maxQuantity: this.maxQuantity, maximumQuantity: this.maxQuantity, measurement: this.measurement, referenceId: this.referenceId, sku: this.sku, sponsored: this.sponsored ? true : false, citrusAdId: this.citrusAdId ? this.citrusAdId : "", clusterCode: this.clusterCode, isKvi: this.isKvi, partnerId: this.partnerId });
}
}
exports.Product = Product;
//# sourceMappingURL=product.js.map