@chiper-inc/ecommerce-lib
Version:
Chiper Inc Ecommerce Lib
137 lines • 5.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Item = exports.ItemType = void 0;
const event_1 = require("../event");
const helpers_1 = require("./helpers");
const itemQuantityError_1 = require("./itemQuantityError");
var ItemType;
(function (ItemType) {
ItemType["PRODUCT"] = "PRODUCT";
ItemType["COMBO"] = "COMBO";
})(ItemType = exports.ItemType || (exports.ItemType = {}));
class Item extends event_1.EventSender {
constructor(type, { stock, multipleQuantity, name, image, id, warehouseId, packagingType, minQuantity, measurement }, rate, isBackend, isOffline) {
super();
this.isOffline = isOffline;
this.isBackend = isBackend;
this.id = id;
this.warehouseId = warehouseId;
this.type = type;
this.name = name;
this._stock = stock;
this.image = image;
let complementPackaging = '';
if (measurement && measurement.quantity && measurement.unit) {
complementPackaging = `${measurement.quantity} ${measurement.unit}`;
complementPackaging = packagingType.includes(complementPackaging) ? '' : ` ${complementPackaging}`;
}
this.packagingType = `${packagingType}${complementPackaging}`;
this.minQuantity = minQuantity !== null && minQuantity !== void 0 ? minQuantity : 1;
this.multipleQuantity = multipleQuantity !== null && multipleQuantity !== void 0 ? multipleQuantity : 1;
this.rate = rate;
}
get stock() {
return this._stock;
}
set stock(stock) {
this._stock = stock;
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
});
}
}
set quantity(quantity) {
var _a;
if (quantity == null) {
this._quantity = undefined;
return;
}
if (quantity < this.minQuantity && !this.isOffline) {
this.emit(event_1.CartEvent.BELOW_MIN_QUANTITY, {
id: this.id,
quantity: quantity,
minQuantity: this.minQuantity,
});
throw new itemQuantityError_1.ItemQuantityError(`Quantity can't be less than minQuantity(${this.minQuantity})`, this.minQuantity, "MinQuantity");
}
if (quantity > this.stock && !this.isOffline) {
const newQuantity = (0, helpers_1.getMaximumValue)(this.stock, (_a = this.maxQuantity) !== null && _a !== void 0 ? _a : this.stock, this.multipleQuantity);
this.emit(event_1.CartEvent.OUT_OF_STOCK, {
id: this.id,
quantity: quantity,
stock: this.stock,
});
throw new itemQuantityError_1.ItemQuantityError(`Quantity can't be more than stock(${this.stock})`, newQuantity, "Stock");
}
if (this.maxQuantity && quantity > this.maxQuantity && !this.isOffline) {
const newQuantity = (0, helpers_1.getMaximumValue)(this.stock, this.maxQuantity, this.multipleQuantity);
if (this.isBackend === true) {
this.quantity = newQuantity;
}
else {
this.emit(event_1.CartEvent.EXCEEDS_MAX_QUANTITY, {
id: this.id,
quantity: quantity,
maxQuantity: this.maxQuantity,
});
throw new itemQuantityError_1.ItemQuantityError(`Quantity can't be more than maxQuantity(${this.maxQuantity})`, newQuantity, "MaxQuantity");
}
}
if (quantity % this.multipleQuantity !== 0 && !this.isOffline) {
const newQuantity = (0, helpers_1.roundToMultipleQuantity)(quantity, this.multipleQuantity, this.stock);
this.emit(event_1.CartEvent.NOT_MULTIPLE_QUANTITY, {
id: this.id,
quantity: quantity,
multipleQuantity: this.multipleQuantity,
});
throw new itemQuantityError_1.ItemQuantityError(`Quantity can't be different of a multipleQuantity(${this.multipleQuantity})`, newQuantity, "MultipleQuantity");
}
this._quantity = quantity;
}
get quantity() {
return this._quantity;
}
/** `discount` is the difference between `regularPrice` and `discountedPrice` in %*/
get discount() {
return this.discountedPrice
? (0, helpers_1.getDiscount)(this.regularPrice, this.discountedPrice)
: 0;
}
// create toJson method
toJSON() {
return {
id: this.id,
warehouseId: this.warehouseId,
type: this.type,
name: this.name,
stock: this.stock,
quantity: this.quantity,
image: this.image,
medium: this.image,
packagingType: this.packagingType,
minQuantity: this.minQuantity,
multipleQuantity: this.multipleQuantity,
total: this.total,
subtotal: this.subtotal,
maxQuantity: this.maxQuantity,
maximumQuantity: this.maxQuantity,
regularPrice: this.regularPrice,
discountedPrice: this.discountedPrice,
totalDollars: this.totalDollars,
regularPriceDolar: this.regularPriceDolar
};
}
}
exports.Item = Item;
//# sourceMappingURL=item.js.map