phx-react
Version:
PHX REACT
49 lines • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.caculateProductPrice = exports.caculateBundlePrice = void 0;
exports.formatCurrency = formatCurrency;
const constant_1 = require("./constant");
const caculateBundlePrice = (bundleProducts, price_type) => {
let minResult = 0;
let maxResult = 0;
const { value: reduceValue = 0, type = constant_1.BUNDLE_PRICE_BY_PRODUCT } = price_type;
bundleProducts.forEach((bundleProduct) => {
if (bundleProduct.store_bundle_product_attribute_values.length === 0)
return;
const listPrice = bundleProduct.store_bundle_product_attribute_values.map((item) => item.store_product_attribute_value.price);
minResult += Math.min(...listPrice);
maxResult += Math.max(...listPrice);
});
if (type === constant_1.BUNDLE_PRICE_REDUCE_BY_PERCENT) {
minResult = Math.floor(minResult - (minResult * reduceValue) / 100);
maxResult = Math.floor(maxResult - (maxResult * reduceValue) / 100);
}
if (type === constant_1.BUNDLE_PRICE_REDUCE_BY_MONEY) {
minResult = minResult - reduceValue;
maxResult = maxResult - reduceValue;
}
minResult = minResult < 0 ? 0 : minResult;
maxResult = maxResult < 0 ? 0 : maxResult;
if (minResult === maxResult)
return formatCurrency(minResult);
return `${minResult > 0 ? formatCurrency(minResult) : '0 đ'} - ${maxResult > 0 ? formatCurrency(maxResult) : '0 đ'}`;
};
exports.caculateBundlePrice = caculateBundlePrice;
function formatCurrency(amount) {
if (!amount || amount < 0)
return `0 đ`;
return `${amount.toLocaleString('en-US')} đ`;
}
const caculateProductPrice = (data) => {
var _a;
const { store_product_attributes } = data;
const aggregate = (_a = store_product_attributes === null || store_product_attributes === void 0 ? void 0 : store_product_attributes[0]) === null || _a === void 0 ? void 0 : _a.store_product_attribute_values_aggregate.aggregate;
const minPrice = aggregate.min.price || 0;
const maxPrice = aggregate.max.price || 0;
const price = minPrice === maxPrice || minPrice === 0
? `${formatCurrency(maxPrice)}`
: `${formatCurrency(minPrice)} - ${formatCurrency(maxPrice)}`;
return price;
};
exports.caculateProductPrice = caculateProductPrice;
//# sourceMappingURL=store-helper.js.map