@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
90 lines (88 loc) • 3.88 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/solution/BookingByStep/utils/stock.ts
var stock_exports = {};
__export(stock_exports, {
checkProductStock: () => checkProductStock
});
module.exports = __toCommonJS(stock_exports);
function checkProductStock({
productData,
product_variant_id,
quantity,
bundle,
currentCartItems
}) {
let mainProductConfig = productData;
if (product_variant_id && productData.variant && Array.isArray(productData.variant)) {
const variant = productData.variant.find((v) => v.id === product_variant_id);
if (variant) {
mainProductConfig = variant;
}
}
const isMainProductTrackingEnabled = mainProductConfig.is_track === 1 || mainProductConfig.is_track === true;
const isMainProductOverSoldDisabled = mainProductConfig.over_sold === 0;
if (isMainProductTrackingEnabled && isMainProductOverSoldDisabled) {
const existingQuantity = currentCartItems.reduce((total, cartItem) => {
var _a, _b, _c;
const isSameProduct = ((_a = cartItem._productOrigin) == null ? void 0 : _a.id) === productData.id;
const isSameVariant = !product_variant_id && !((_b = cartItem._productOrigin) == null ? void 0 : _b.product_variant_id) || ((_c = cartItem._productOrigin) == null ? void 0 : _c.product_variant_id) === product_variant_id;
if (isSameProduct && isSameVariant) {
return total + (cartItem.num || 0);
}
return total;
}, 0);
const totalQuantity = existingQuantity + quantity;
const stockQuantity = mainProductConfig.stock_quantity;
if (stockQuantity !== void 0 && stockQuantity !== null && totalQuantity > stockQuantity) {
return { success: false, errorCode: "not_enough_stock" };
}
}
if (bundle && Array.isArray(bundle)) {
for (const bundleItem of bundle) {
const bundleProductId = bundleItem.bundle_product_id;
const bundleStockQuantity = bundleItem.stock_quantity;
const bundleRequiredQuantity = (bundleItem.num || 1) * quantity;
const isBundleTrackingEnabled = bundleItem.is_track === 1 || bundleItem.is_track === true;
const isBundleOverSoldDisabled = bundleItem.over_sold === 0;
if (!isBundleTrackingEnabled || !isBundleOverSoldDisabled)
continue;
if (bundleStockQuantity === void 0 || bundleStockQuantity === null)
continue;
const existingBundleQuantity = currentCartItems.reduce((total, cartItem) => {
if (!cartItem._bundleOrigin || !Array.isArray(cartItem._bundleOrigin))
return total;
cartItem._bundleOrigin.forEach((cartBundleItem) => {
if (cartBundleItem.bundle_product_id === bundleProductId) {
total += (cartBundleItem.num || 1) * (cartItem.num || 1);
}
});
return total;
}, 0);
const totalBundleQuantity = existingBundleQuantity + bundleRequiredQuantity;
if (totalBundleQuantity > bundleStockQuantity) {
return { success: false, errorCode: "not_enough_stock" };
}
}
}
return { success: true };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
checkProductStock
});